From 27ab485bc14d7f895fa95796343a94ebb477d809 Mon Sep 17 00:00:00 2001 From: duanledexianxianxian Date: Sun, 29 Mar 2020 02:16:27 +0800 Subject: [PATCH] :recycle: Refactoring code. --- .../java/com/kim/apidoc/core/Context.java | 6 ++-- .../common/description/TypeDescription.java | 34 +++++++++++++++---- .../core/common/helper/StringHelper.java | 10 +++--- .../core/resolver/ObjectTypeResolver.java | 18 +++++----- .../core/resolver/PrimitiveTypeResolver.java | 32 ++++++++--------- .../apidoc/core/resolver/TypeResolvers.java | 26 +++++++------- .../core/schema/RequestParameterType.java | 23 +++++++++++++ .../java/com/kim/apidoc/core/schema/Row.java | 6 ++++ .../com/kim/apidoc/core/schema/Section.java | 1 + .../src/main/resources/templates/markdown.ftl | 8 ++--- .../kim/apidoc/springmvc/SpringParser.java | 8 +++-- .../com/kim/apidoc/example/common/User.java | 4 ++- .../spring/advanced/KimUserController.java | 8 ++--- .../com/kim/apidoc/springmvc/SpringTest.java | 2 +- 14 files changed, 122 insertions(+), 64 deletions(-) create mode 100644 apidoc-core/src/main/java/com/kim/apidoc/core/schema/RequestParameterType.java diff --git a/apidoc-core/src/main/java/com/kim/apidoc/core/Context.java b/apidoc-core/src/main/java/com/kim/apidoc/core/Context.java index 4b2dc19..bb58954 100644 --- a/apidoc-core/src/main/java/com/kim/apidoc/core/Context.java +++ b/apidoc-core/src/main/java/com/kim/apidoc/core/Context.java @@ -3,9 +3,7 @@ package com.kim.apidoc.core; import com.google.common.collect.Lists; import com.google.common.collect.Maps; 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.PostmanRender; import com.kim.apidoc.core.render.ProjectRender; import lombok.Getter; import lombok.Setter; @@ -54,8 +52,8 @@ public class Context { */ @Setter private List renders = Lists.newArrayList( - new AsciiDocRender(), - new PostmanRender(), + //new AsciiDocRender(), + //new PostmanRender(), new MarkdownRender()); /** diff --git a/apidoc-core/src/main/java/com/kim/apidoc/core/common/description/TypeDescription.java b/apidoc-core/src/main/java/com/kim/apidoc/core/common/description/TypeDescription.java index 88debb1..9aecef0 100644 --- a/apidoc-core/src/main/java/com/kim/apidoc/core/common/description/TypeDescription.java +++ b/apidoc-core/src/main/java/com/kim/apidoc/core/common/description/TypeDescription.java @@ -1,8 +1,11 @@ package com.kim.apidoc.core.common.description; -import com.kim.apidoc.core.schema.Row; -import com.kim.apidoc.core.common.helper.StringHelper; +import com.github.javaparser.ast.comments.Comment; +import com.github.javaparser.javadoc.Javadoc; 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.Setter; @@ -168,9 +171,9 @@ public abstract class TypeDescription { * * @return the collection */ - public Collection rows() { - String key = fullKey(); - if (StringHelper.isBlank(key)) { + public Collection rows(String requestParameterType) { + String fullKey = fullKey(); + if (StringHelper.isBlank(fullKey)) { return Lists.newArrayList(); } String def; @@ -186,7 +189,26 @@ public abstract class TypeDescription { 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 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())); } } diff --git a/apidoc-core/src/main/java/com/kim/apidoc/core/common/helper/StringHelper.java b/apidoc-core/src/main/java/com/kim/apidoc/core/common/helper/StringHelper.java index b6c3093..14f9f4e 100644 --- a/apidoc-core/src/main/java/com/kim/apidoc/core/common/helper/StringHelper.java +++ b/apidoc-core/src/main/java/com/kim/apidoc/core/common/helper/StringHelper.java @@ -13,26 +13,26 @@ public class StringHelper { } public static boolean isBlank(Object text) { - if(text instanceof String){ + if (text instanceof String) { return isBlank(((String) text)); } return isBlank(String.valueOf(text)); } public static boolean nonBlank(Object text) { - if(text instanceof String){ + if (text instanceof String) { return nonBlank(((String) 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(); for (String value : values) { - if(isBlank(value)){ + if (isBlank(value)) { continue; } - if(builder.length()>0){ + if (builder.length() > 0) { builder.append(delimiter); } builder.append(value); diff --git a/apidoc-core/src/main/java/com/kim/apidoc/core/resolver/ObjectTypeResolver.java b/apidoc-core/src/main/java/com/kim/apidoc/core/resolver/ObjectTypeResolver.java index cee038c..8a9bf37 100644 --- a/apidoc-core/src/main/java/com/kim/apidoc/core/resolver/ObjectTypeResolver.java +++ b/apidoc-core/src/main/java/com/kim/apidoc/core/resolver/ObjectTypeResolver.java @@ -3,6 +3,7 @@ package com.kim.apidoc.core.resolver; import com.github.javaparser.resolution.declarations.ResolvedFieldDeclaration; import com.github.javaparser.resolution.types.ResolvedReferenceType; 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.common.description.ObjectTypeDescription; import com.kim.apidoc.core.common.description.TypeDescription; @@ -27,15 +28,16 @@ public class ObjectTypeResolver implements TypeResolver { ObjectTypeDescription typeDescription = new ObjectTypeDescription(); 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; } //解析父类属性,并合并至当前 for (ResolvedReferenceType directAncestor : referenceType.getDirectAncestors()) { TypeDescription ancestorDescription = ApiDoc.getInstance().getTypeResolvers().resolve(directAncestor); - if(ancestorDescription.isAvailable() && ancestorDescription.isObject()){ + if (ancestorDescription.isAvailable() && ancestorDescription.isObject()) { typeDescription.merge(ancestorDescription.asObject()); } } @@ -43,19 +45,19 @@ public class ObjectTypeResolver implements TypeResolver { //TODO fix use access method for (ResolvedFieldDeclaration declaredField : referenceType.getTypeDeclaration().getDeclaredFields()) { - if(declaredField.isStatic()){ + if (declaredField.isStatic()) { continue; } ResolvedType fieldType = declaredField.getType(); - if(fieldType.isReferenceType()){ + if (fieldType.isReferenceType()) { //将父类的T,传递给 属性的T - fieldType = TypeParameterHelper.useClassTypeParameter(referenceType,fieldType.asReferenceType()); + fieldType = TypeParameterHelper.useClassTypeParameter(referenceType, fieldType.asReferenceType()); } - if(declaredField.getType().isTypeVariable()){ + if (declaredField.getType().isTypeVariable()) { //类型为T,这种泛型 Optional optional = TypeParameterHelper.getTypeParameter(referenceType, declaredField.getType().asTypeParameter().getName()); - if(optional.isPresent()){ + if (optional.isPresent()) { fieldType = optional.get(); } } @@ -71,7 +73,7 @@ public class ObjectTypeResolver implements TypeResolver { 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); } diff --git a/apidoc-core/src/main/java/com/kim/apidoc/core/resolver/PrimitiveTypeResolver.java b/apidoc-core/src/main/java/com/kim/apidoc/core/resolver/PrimitiveTypeResolver.java index 36e4123..fde1fe2 100644 --- a/apidoc-core/src/main/java/com/kim/apidoc/core/resolver/PrimitiveTypeResolver.java +++ b/apidoc-core/src/main/java/com/kim/apidoc/core/resolver/PrimitiveTypeResolver.java @@ -6,22 +6,8 @@ import com.github.javaparser.resolution.types.ResolvedType; import com.google.common.collect.ImmutableList; public class PrimitiveTypeResolver implements TypeResolver { - @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()); - } - } - - private static boolean isBoxing(ResolvedType type){ - if(!type.isReferenceType()){ + private static boolean isBoxing(ResolvedType type) { + if (!type.isReferenceType()) { return false; } String id = type.asReferenceType().getId(); @@ -37,4 +23,18 @@ public class PrimitiveTypeResolver implements TypeResolver { ).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()); + } + } + } diff --git a/apidoc-core/src/main/java/com/kim/apidoc/core/resolver/TypeResolvers.java b/apidoc-core/src/main/java/com/kim/apidoc/core/resolver/TypeResolvers.java index bc220d9..4858478 100644 --- a/apidoc-core/src/main/java/com/kim/apidoc/core/resolver/TypeResolvers.java +++ b/apidoc-core/src/main/java/com/kim/apidoc/core/resolver/TypeResolvers.java @@ -34,55 +34,57 @@ public class TypeResolvers { /** * 获取类型信息 + * * @param type * @return */ - public TypeDescription resolve(Type type){ - try{ + public TypeDescription resolve(Type type) { + try { ResolvedType resolvedType = type.resolve(); return resolve(resolvedType); - } catch (UnsolvedSymbolException e){ + } catch (UnsolvedSymbolException e) { //解析失败时,尝试降级,使用名称解析 return nameResolve(type); - } catch (Exception e){ - log.error(e.getMessage(),e); + } catch (Exception e) { + log.error(e.getMessage(), e); } return new UnAvailableTypeDescription(); } /** * 解析类型信息 + * * @param type * @return */ - public TypeDescription resolve(ResolvedType type){ + public TypeDescription resolve(ResolvedType type) { for (TypeResolver typeResolver : resolvers) { - if(typeResolver.accept(type)){ + if (typeResolver.accept(type)) { return typeResolver.resolve(type); } } - if(objectTypeResolver.accept(type)){ + if (objectTypeResolver.accept(type)) { return objectTypeResolver.resolve(type); } return new UnAvailableTypeDescription(); } - public TypeDescription nameResolve(Type type){ + public TypeDescription nameResolve(Type type) { String id = TypeNameHelper.getName(type); for (TypeNameResolver nameResolver : nameResolvers) { if (nameResolver.accept(id)) { return nameResolver.resolve(type); } } - log.warn("type({}) resolve failed",id); + log.warn("type({}) resolve failed", id); return new UnAvailableTypeDescription(); } - public void addResolver(TypeResolver typeResolver){ + public void addResolver(TypeResolver typeResolver) { resolvers.add(typeResolver); } - public void addNameResolver(TypeNameResolver nameResolver){ + public void addNameResolver(TypeNameResolver nameResolver) { nameResolvers.add(nameResolver); } diff --git a/apidoc-core/src/main/java/com/kim/apidoc/core/schema/RequestParameterType.java b/apidoc-core/src/main/java/com/kim/apidoc/core/schema/RequestParameterType.java new file mode 100644 index 0000000..dc17e8a --- /dev/null +++ b/apidoc-core/src/main/java/com/kim/apidoc/core/schema/RequestParameterType.java @@ -0,0 +1,23 @@ +package com.kim.apidoc.core.schema; + +/** + * 请求参数枚举类型 + * + * @author duanledexianxianxian + * @date 2020 /3/29 0:37 + * @since 1.0.0 + */ +public enum RequestParameterType { + /** + * 查询参数. + */ + QUERY, + /** + * 路径参数. + */ + PATH, + /** + * 请求参数体. + */ + BODY +} diff --git a/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Row.java b/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Row.java index 22bdbd6..9d3a51b 100644 --- a/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Row.java +++ b/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Row.java @@ -40,6 +40,12 @@ public class Row { */ String remark; + /** + * 请求参数类型 + */ + String requestParameterType; + + /** * Instantiates a new Row. * diff --git a/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Section.java b/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Section.java index 4f1c719..cd03939 100644 --- a/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Section.java +++ b/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Section.java @@ -44,6 +44,7 @@ public class Section extends Node { * The Query parameter. */ boolean queryParameter = true; + /** * The Request rows. */ diff --git a/apidoc-core/src/main/resources/templates/markdown.ftl b/apidoc-core/src/main/resources/templates/markdown.ftl index f1571b4..1d66903 100644 --- a/apidoc-core/src/main/resources/templates/markdown.ftl +++ b/apidoc-core/src/main/resources/templates/markdown.ftl @@ -56,11 +56,11 @@ ${section.getParameterString()} <#-- 请求参数table列表--> <#if section.requestRows?? && (section.requestRows?size>0)> -**Query** -| Field | Type | Required | Condition | Default | Description | -| :------- | :----- | :-------- |:-------- | :------ | :---------- | +**Request** +| Field | Type | Request Type | Required | Condition | Default | Description | +| :------- | :----- | :----- |:-------- |:-------- | :------ | :---------- | <#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!''} | <#-- 响应--> diff --git a/apidoc-springmvc/src/main/java/com/kim/apidoc/springmvc/SpringParser.java b/apidoc-springmvc/src/main/java/com/kim/apidoc/springmvc/SpringParser.java index 0f4fc8e..d73f364 100644 --- a/apidoc-springmvc/src/main/java/com/kim/apidoc/springmvc/SpringParser.java +++ b/apidoc-springmvc/src/main/java/com/kim/apidoc/springmvc/SpringParser.java @@ -22,6 +22,8 @@ import com.github.javaparser.ast.expr.Expression; import java.util.List; import java.util.Optional; +import static com.kim.apidoc.core.schema.RequestParameterType.*; + /** * spring 解析 @@ -43,7 +45,6 @@ public class SpringParser implements ParserStrategy { public static final String EXT_URI = "uri"; - @Override public String name() { return FRAMEWORK; @@ -157,6 +158,7 @@ public class SpringParser implements ParserStrategy { if (ParameterHelper.isPathVariable(parameter)) { section.getPathVariable().put(parameter.getNameAsString(), ""); Row row = new Row(); + row.setRequestParameterType(PATH.name()); row.setKey(parameter.getNameAsString()); row.setType(parameter.getType().toString()); // 路径参数必填 @@ -235,7 +237,7 @@ public class SpringParser implements ParserStrategy { } else if (description.isObject()) { section.setParameter(description.asObject().getValue()); } - section.addRequestRows(description.rows()); + section.addRequestRows(description.rows(BODY.name())); } break; } @@ -294,7 +296,7 @@ public class SpringParser implements ParserStrategy { } } section.setParameter(objectTypeDescription.getValue()); - section.addRequestRows(objectTypeDescription.rows()); + section.addRequestRows(objectTypeDescription.rows(QUERY.name())); } /** diff --git a/apidoc-springmvc/src/test/java/com/kim/apidoc/example/common/User.java b/apidoc-springmvc/src/test/java/com/kim/apidoc/example/common/User.java index 317cda9..9724a8f 100644 --- a/apidoc-springmvc/src/test/java/com/kim/apidoc/example/common/User.java +++ b/apidoc-springmvc/src/test/java/com/kim/apidoc/example/common/User.java @@ -8,10 +8,12 @@ import javax.validation.constraints.Min; import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; import java.util.Date; -import java.util.List; /** * 用户对象 + * + * @author fengyuchenglun + * @version 1.0.0 */ @Setter @Getter diff --git a/apidoc-springmvc/src/test/java/com/kim/apidoc/example/spring/advanced/KimUserController.java b/apidoc-springmvc/src/test/java/com/kim/apidoc/example/spring/advanced/KimUserController.java index 5f202ad..5a01cb0 100644 --- a/apidoc-springmvc/src/test/java/com/kim/apidoc/example/spring/advanced/KimUserController.java +++ b/apidoc-springmvc/src/test/java/com/kim/apidoc/example/spring/advanced/KimUserController.java @@ -3,8 +3,8 @@ package com.kim.apidoc.example.spring.advanced; import com.kim.apidoc.example.annotation.KimController; import com.kim.apidoc.example.common.ResultData; 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.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -25,11 +25,11 @@ public class KimUserController { * 用户详情信息 * 主动根据id获取用户的信息 * - * @param id 用户编号 - * @return 用户数据 + * @param userQuery the user query + * @return result data */ @GetMapping(value = "/{id}") - public ResultData detail(@PathVariable String id) { + public ResultData detail(UserQuery userQuery) { User user = new User(); return ResultData.ok(user); } diff --git a/apidoc-springmvc/src/test/java/com/kim/apidoc/springmvc/SpringTest.java b/apidoc-springmvc/src/test/java/com/kim/apidoc/springmvc/SpringTest.java index 824168d..23b6e0d 100644 --- a/apidoc-springmvc/src/test/java/com/kim/apidoc/springmvc/SpringTest.java +++ b/apidoc-springmvc/src/test/java/com/kim/apidoc/springmvc/SpringTest.java @@ -17,7 +17,7 @@ public class SpringTest { Context context = new Context(); context.setId("test"); 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"); ApiDoc apigcc = new ApiDoc(context); -- GitLab