Commit 27ab485b authored by duanledexianxianxian's avatar duanledexianxianxian 😁

Refactoring code.

parent dc061d6a
...@@ -3,9 +3,7 @@ package com.kim.apidoc.core; ...@@ -3,9 +3,7 @@ package com.kim.apidoc.core;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.kim.apidoc.core.common.helper.FileHelper; import com.kim.apidoc.core.common.helper.FileHelper;
import com.kim.apidoc.core.render.AsciiDocRender;
import com.kim.apidoc.core.render.MarkdownRender; import com.kim.apidoc.core.render.MarkdownRender;
import com.kim.apidoc.core.render.PostmanRender;
import com.kim.apidoc.core.render.ProjectRender; import com.kim.apidoc.core.render.ProjectRender;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
...@@ -54,8 +52,8 @@ public class Context { ...@@ -54,8 +52,8 @@ public class Context {
*/ */
@Setter @Setter
private List<ProjectRender> renders = Lists.newArrayList( private List<ProjectRender> renders = Lists.newArrayList(
new AsciiDocRender(), //new AsciiDocRender(),
new PostmanRender(), //new PostmanRender(),
new MarkdownRender()); new MarkdownRender());
/** /**
......
package com.kim.apidoc.core.common.description; package com.kim.apidoc.core.common.description;
import com.kim.apidoc.core.schema.Row; import com.github.javaparser.ast.comments.Comment;
import com.kim.apidoc.core.common.helper.StringHelper; import com.github.javaparser.javadoc.Javadoc;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.kim.apidoc.core.common.helper.CommentHelper;
import com.kim.apidoc.core.common.helper.StringHelper;
import com.kim.apidoc.core.schema.Row;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
...@@ -168,9 +171,9 @@ public abstract class TypeDescription { ...@@ -168,9 +171,9 @@ public abstract class TypeDescription {
* *
* @return the collection * @return the collection
*/ */
public Collection<Row> rows() { public Collection<Row> rows(String requestParameterType) {
String key = fullKey(); String fullKey = fullKey();
if (StringHelper.isBlank(key)) { if (StringHelper.isBlank(fullKey)) {
return Lists.newArrayList(); return Lists.newArrayList();
} }
String def; String def;
...@@ -186,7 +189,26 @@ public abstract class TypeDescription { ...@@ -186,7 +189,26 @@ public abstract class TypeDescription {
condition.append("required=").append(required); condition.append("required=").append(required);
} }
return Lists.newArrayList(new Row(key, type,false, condition.toString(), def, remark)); return Lists.newArrayList(new Row(key, type, false, condition.toString(), def, remark, requestParameterType));
}
/**
* Rows collection.
*
* @return the collection
*/
public Collection<Row> rows() {
return rows(null);
}
public void accept(Comment comment) {
if (!comment.isJavadocComment()) {
setRemark(comment.getContent());
return;
}
Javadoc javadoc = comment.asJavadocComment().parse();
setRemark(CommentHelper.getDescription(javadoc.getDescription()));
} }
} }
...@@ -13,26 +13,26 @@ public class StringHelper { ...@@ -13,26 +13,26 @@ public class StringHelper {
} }
public static boolean isBlank(Object text) { public static boolean isBlank(Object text) {
if(text instanceof String){ if (text instanceof String) {
return isBlank(((String) text)); return isBlank(((String) text));
} }
return isBlank(String.valueOf(text)); return isBlank(String.valueOf(text));
} }
public static boolean nonBlank(Object text) { public static boolean nonBlank(Object text) {
if(text instanceof String){ if (text instanceof String) {
return nonBlank(((String) text)); return nonBlank(((String) text));
} }
return nonBlank(String.valueOf(text)); return nonBlank(String.valueOf(text));
} }
public static String join(String delimiter, String ... values){ public static String join(String delimiter, String... values) {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
for (String value : values) { for (String value : values) {
if(isBlank(value)){ if (isBlank(value)) {
continue; continue;
} }
if(builder.length()>0){ if (builder.length() > 0) {
builder.append(delimiter); builder.append(delimiter);
} }
builder.append(value); builder.append(value);
......
...@@ -3,6 +3,7 @@ package com.kim.apidoc.core.resolver; ...@@ -3,6 +3,7 @@ package com.kim.apidoc.core.resolver;
import com.github.javaparser.resolution.declarations.ResolvedFieldDeclaration; import com.github.javaparser.resolution.declarations.ResolvedFieldDeclaration;
import com.github.javaparser.resolution.types.ResolvedReferenceType; import com.github.javaparser.resolution.types.ResolvedReferenceType;
import com.github.javaparser.resolution.types.ResolvedType; import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserClassDeclaration;
import com.kim.apidoc.core.ApiDoc; import com.kim.apidoc.core.ApiDoc;
import com.kim.apidoc.core.common.description.ObjectTypeDescription; import com.kim.apidoc.core.common.description.ObjectTypeDescription;
import com.kim.apidoc.core.common.description.TypeDescription; import com.kim.apidoc.core.common.description.TypeDescription;
...@@ -27,15 +28,16 @@ public class ObjectTypeResolver implements TypeResolver { ...@@ -27,15 +28,16 @@ public class ObjectTypeResolver implements TypeResolver {
ObjectTypeDescription typeDescription = new ObjectTypeDescription(); ObjectTypeDescription typeDescription = new ObjectTypeDescription();
typeDescription.setType(referenceType.getTypeDeclaration().getName()); typeDescription.setType(referenceType.getTypeDeclaration().getName());
((JavaParserClassDeclaration) referenceType.getTypeDeclaration()).getWrappedNode().getComment().ifPresent(typeDescription::accept);
//类型解析缓冲池,防止循环引用 //类型解析缓冲池,防止循环引用
if(!ReferenceContext.getInstance().push(referenceType.describe())){ if (!ReferenceContext.getInstance().push(referenceType.describe())) {
return typeDescription; return typeDescription;
} }
//解析父类属性,并合并至当前 //解析父类属性,并合并至当前
for (ResolvedReferenceType directAncestor : referenceType.getDirectAncestors()) { for (ResolvedReferenceType directAncestor : referenceType.getDirectAncestors()) {
TypeDescription ancestorDescription = ApiDoc.getInstance().getTypeResolvers().resolve(directAncestor); TypeDescription ancestorDescription = ApiDoc.getInstance().getTypeResolvers().resolve(directAncestor);
if(ancestorDescription.isAvailable() && ancestorDescription.isObject()){ if (ancestorDescription.isAvailable() && ancestorDescription.isObject()) {
typeDescription.merge(ancestorDescription.asObject()); typeDescription.merge(ancestorDescription.asObject());
} }
} }
...@@ -43,19 +45,19 @@ public class ObjectTypeResolver implements TypeResolver { ...@@ -43,19 +45,19 @@ public class ObjectTypeResolver implements TypeResolver {
//TODO fix use access method //TODO fix use access method
for (ResolvedFieldDeclaration declaredField : referenceType.getTypeDeclaration().getDeclaredFields()) { for (ResolvedFieldDeclaration declaredField : referenceType.getTypeDeclaration().getDeclaredFields()) {
if(declaredField.isStatic()){ if (declaredField.isStatic()) {
continue; continue;
} }
ResolvedType fieldType = declaredField.getType(); ResolvedType fieldType = declaredField.getType();
if(fieldType.isReferenceType()){ if (fieldType.isReferenceType()) {
//将父类的T,传递给 属性的T //将父类的T,传递给 属性的T
fieldType = TypeParameterHelper.useClassTypeParameter(referenceType,fieldType.asReferenceType()); fieldType = TypeParameterHelper.useClassTypeParameter(referenceType, fieldType.asReferenceType());
} }
if(declaredField.getType().isTypeVariable()){ if (declaredField.getType().isTypeVariable()) {
//类型为T,这种泛型 //类型为T,这种泛型
Optional<ResolvedType> optional = TypeParameterHelper.getTypeParameter(referenceType, declaredField.getType().asTypeParameter().getName()); Optional<ResolvedType> optional = TypeParameterHelper.getTypeParameter(referenceType, declaredField.getType().asTypeParameter().getName());
if(optional.isPresent()){ if (optional.isPresent()) {
fieldType = optional.get(); fieldType = optional.get();
} }
} }
...@@ -71,7 +73,7 @@ public class ObjectTypeResolver implements TypeResolver { ...@@ -71,7 +73,7 @@ public class ObjectTypeResolver implements TypeResolver {
fieldDescription.getCondition().append(validation).append(" "); fieldDescription.getCondition().append(validation).append(" ");
} }
//查找字段初始化值 //查找字段初始化值
FieldHelper.getInitializer(declaredField).ifPresent(expr-> fieldDescription.setDefaultValue(ExpressionHelper.getValue(expr))); FieldHelper.getInitializer(declaredField).ifPresent(expr -> fieldDescription.setDefaultValue(ExpressionHelper.getValue(expr)));
typeDescription.add(fieldDescription); typeDescription.add(fieldDescription);
} }
......
...@@ -6,22 +6,8 @@ import com.github.javaparser.resolution.types.ResolvedType; ...@@ -6,22 +6,8 @@ import com.github.javaparser.resolution.types.ResolvedType;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
public class PrimitiveTypeResolver implements TypeResolver { public class PrimitiveTypeResolver implements TypeResolver {
@Override private static boolean isBoxing(ResolvedType type) {
public boolean accept(ResolvedType type) { if (!type.isReferenceType()) {
return type.isPrimitive() || isBoxing(type);
}
@Override
public TypeDescription resolve(ResolvedType type) {
if(type.isPrimitive()){
return new PrimitiveTypeDescription(type.asPrimitive());
}else{
return new PrimitiveTypeDescription(type.asReferenceType());
}
}
private static boolean isBoxing(ResolvedType type){
if(!type.isReferenceType()){
return false; return false;
} }
String id = type.asReferenceType().getId(); String id = type.asReferenceType().getId();
...@@ -37,4 +23,18 @@ public class PrimitiveTypeResolver implements TypeResolver { ...@@ -37,4 +23,18 @@ public class PrimitiveTypeResolver implements TypeResolver {
).contains(id); ).contains(id);
} }
@Override
public boolean accept(ResolvedType type) {
return type.isPrimitive() || isBoxing(type);
}
@Override
public TypeDescription resolve(ResolvedType type) {
if (type.isPrimitive()) {
return new PrimitiveTypeDescription(type.asPrimitive());
} else {
return new PrimitiveTypeDescription(type.asReferenceType());
}
}
} }
...@@ -34,55 +34,57 @@ public class TypeResolvers { ...@@ -34,55 +34,57 @@ public class TypeResolvers {
/** /**
* 获取类型信息 * 获取类型信息
*
* @param type * @param type
* @return * @return
*/ */
public TypeDescription resolve(Type type){ public TypeDescription resolve(Type type) {
try{ try {
ResolvedType resolvedType = type.resolve(); ResolvedType resolvedType = type.resolve();
return resolve(resolvedType); return resolve(resolvedType);
} catch (UnsolvedSymbolException e){ } catch (UnsolvedSymbolException e) {
//解析失败时,尝试降级,使用名称解析 //解析失败时,尝试降级,使用名称解析
return nameResolve(type); return nameResolve(type);
} catch (Exception e){ } catch (Exception e) {
log.error(e.getMessage(),e); log.error(e.getMessage(), e);
} }
return new UnAvailableTypeDescription(); return new UnAvailableTypeDescription();
} }
/** /**
* 解析类型信息 * 解析类型信息
*
* @param type * @param type
* @return * @return
*/ */
public TypeDescription resolve(ResolvedType type){ public TypeDescription resolve(ResolvedType type) {
for (TypeResolver typeResolver : resolvers) { for (TypeResolver typeResolver : resolvers) {
if(typeResolver.accept(type)){ if (typeResolver.accept(type)) {
return typeResolver.resolve(type); return typeResolver.resolve(type);
} }
} }
if(objectTypeResolver.accept(type)){ if (objectTypeResolver.accept(type)) {
return objectTypeResolver.resolve(type); return objectTypeResolver.resolve(type);
} }
return new UnAvailableTypeDescription(); return new UnAvailableTypeDescription();
} }
public TypeDescription nameResolve(Type type){ public TypeDescription nameResolve(Type type) {
String id = TypeNameHelper.getName(type); String id = TypeNameHelper.getName(type);
for (TypeNameResolver nameResolver : nameResolvers) { for (TypeNameResolver nameResolver : nameResolvers) {
if (nameResolver.accept(id)) { if (nameResolver.accept(id)) {
return nameResolver.resolve(type); return nameResolver.resolve(type);
} }
} }
log.warn("type({}) resolve failed",id); log.warn("type({}) resolve failed", id);
return new UnAvailableTypeDescription(); return new UnAvailableTypeDescription();
} }
public void addResolver(TypeResolver typeResolver){ public void addResolver(TypeResolver typeResolver) {
resolvers.add(typeResolver); resolvers.add(typeResolver);
} }
public void addNameResolver(TypeNameResolver nameResolver){ public void addNameResolver(TypeNameResolver nameResolver) {
nameResolvers.add(nameResolver); nameResolvers.add(nameResolver);
} }
......
package com.kim.apidoc.core.schema;
/**
* 请求参数枚举类型
*
* @author duanledexianxianxian
* @date 2020 /3/29 0:37
* @since 1.0.0
*/
public enum RequestParameterType {
/**
* 查询参数.
*/
QUERY,
/**
* 路径参数.
*/
PATH,
/**
* 请求参数体.
*/
BODY
}
...@@ -40,6 +40,12 @@ public class Row { ...@@ -40,6 +40,12 @@ public class Row {
*/ */
String remark; String remark;
/**
* 请求参数类型
*/
String requestParameterType;
/** /**
* Instantiates a new Row. * Instantiates a new Row.
* *
......
...@@ -44,6 +44,7 @@ public class Section extends Node { ...@@ -44,6 +44,7 @@ public class Section extends Node {
* The Query parameter. * The Query parameter.
*/ */
boolean queryParameter = true; boolean queryParameter = true;
/** /**
* The Request rows. * The Request rows.
*/ */
......
...@@ -56,11 +56,11 @@ ${section.getParameterString()} ...@@ -56,11 +56,11 @@ ${section.getParameterString()}
<#-- 请求参数table列表--> <#-- 请求参数table列表-->
<#if section.requestRows?? && (section.requestRows?size>0)> <#if section.requestRows?? && (section.requestRows?size>0)>
**Query** **Request**
| Field | Type | Required | Condition | Default | Description | | Field | Type | Request Type | Required | Condition | Default | Description |
| :------- | :----- | :-------- |:-------- | :------ | :---------- | | :------- | :----- | :----- |:-------- |:-------- | :------ | :---------- |
<#list section.requestRows as rowKey,rowValue> <#list section.requestRows as rowKey,rowValue>
| ${rowValue.key!''} | ${rowValue.type!''} | ${rowValue.required?string('true','false')} | ${rowValue.condition!''} | ${rowValue.def!''} | ${rowValue.remark!''} | | ${rowValue.key!''} | ${rowValue.type!''} | ${rowValue.requestParameterType!''} |${rowValue.required?string('true','false')} | ${rowValue.condition!''} | ${rowValue.def!''} | ${rowValue.remark!''} |
</#list> </#list>
</#if> </#if>
<#-- 响应--> <#-- 响应-->
......
...@@ -22,6 +22,8 @@ import com.github.javaparser.ast.expr.Expression; ...@@ -22,6 +22,8 @@ import com.github.javaparser.ast.expr.Expression;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import static com.kim.apidoc.core.schema.RequestParameterType.*;
/** /**
* spring 解析 * spring 解析
...@@ -43,7 +45,6 @@ public class SpringParser implements ParserStrategy { ...@@ -43,7 +45,6 @@ public class SpringParser implements ParserStrategy {
public static final String EXT_URI = "uri"; public static final String EXT_URI = "uri";
@Override @Override
public String name() { public String name() {
return FRAMEWORK; return FRAMEWORK;
...@@ -157,6 +158,7 @@ public class SpringParser implements ParserStrategy { ...@@ -157,6 +158,7 @@ public class SpringParser implements ParserStrategy {
if (ParameterHelper.isPathVariable(parameter)) { if (ParameterHelper.isPathVariable(parameter)) {
section.getPathVariable().put(parameter.getNameAsString(), ""); section.getPathVariable().put(parameter.getNameAsString(), "");
Row row = new Row(); Row row = new Row();
row.setRequestParameterType(PATH.name());
row.setKey(parameter.getNameAsString()); row.setKey(parameter.getNameAsString());
row.setType(parameter.getType().toString()); row.setType(parameter.getType().toString());
// 路径参数必填 // 路径参数必填
...@@ -235,7 +237,7 @@ public class SpringParser implements ParserStrategy { ...@@ -235,7 +237,7 @@ public class SpringParser implements ParserStrategy {
} else if (description.isObject()) { } else if (description.isObject()) {
section.setParameter(description.asObject().getValue()); section.setParameter(description.asObject().getValue());
} }
section.addRequestRows(description.rows()); section.addRequestRows(description.rows(BODY.name()));
} }
break; break;
} }
...@@ -294,7 +296,7 @@ public class SpringParser implements ParserStrategy { ...@@ -294,7 +296,7 @@ public class SpringParser implements ParserStrategy {
} }
} }
section.setParameter(objectTypeDescription.getValue()); section.setParameter(objectTypeDescription.getValue());
section.addRequestRows(objectTypeDescription.rows()); section.addRequestRows(objectTypeDescription.rows(QUERY.name()));
} }
/** /**
......
...@@ -8,10 +8,12 @@ import javax.validation.constraints.Min; ...@@ -8,10 +8,12 @@ import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import java.util.Date; import java.util.Date;
import java.util.List;
/** /**
* 用户对象 * 用户对象
*
* @author fengyuchenglun
* @version 1.0.0
*/ */
@Setter @Setter
@Getter @Getter
......
...@@ -3,8 +3,8 @@ package com.kim.apidoc.example.spring.advanced; ...@@ -3,8 +3,8 @@ package com.kim.apidoc.example.spring.advanced;
import com.kim.apidoc.example.annotation.KimController; import com.kim.apidoc.example.annotation.KimController;
import com.kim.apidoc.example.common.ResultData; import com.kim.apidoc.example.common.ResultData;
import com.kim.apidoc.example.common.User; import com.kim.apidoc.example.common.User;
import com.kim.apidoc.example.common.UserQuery;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
...@@ -25,11 +25,11 @@ public class KimUserController { ...@@ -25,11 +25,11 @@ public class KimUserController {
* 用户详情信息 * 用户详情信息
* 主动根据id获取用户的信息 * 主动根据id获取用户的信息
* *
* @param id 用户编号 * @param userQuery the user query
* @return 用户数据 * @return result data
*/ */
@GetMapping(value = "/{id}") @GetMapping(value = "/{id}")
public ResultData<User> detail(@PathVariable String id) { public ResultData<User> detail(UserQuery userQuery) {
User user = new User(); User user = new User();
return ResultData.ok(user); return ResultData.ok(user);
} }
......
...@@ -17,7 +17,7 @@ public class SpringTest { ...@@ -17,7 +17,7 @@ public class SpringTest {
Context context = new Context(); Context context = new Context();
context.setId("test"); context.setId("test");
context.setName("测试项目"); context.setName("测试项目");
context.addSource(Paths.get("F:\\@project@\\@dianli@\\tool\\apidoc\\apidoc-springmvc\\src\\test\\java")); context.addSource(Paths.get("K:\\@project-dianli@\\tool\\apidoc\\apidoc-springmvc\\src\\test\\java"));
// context.setCss("https://darshandsoni.com/asciidoctor-skins/css/monospace.css"); // context.setCss("https://darshandsoni.com/asciidoctor-skins/css/monospace.css");
ApiDoc apigcc = new ApiDoc(context); ApiDoc apigcc = new ApiDoc(context);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment