diff --git a/apidoc-core/build.gradle b/apidoc-core/build.gradle index dffa1eef891299413ad279489879625eece4cf6f..f517e6a2090ae6243f70be3e54e4980c99517401 100644 --- a/apidoc-core/build.gradle +++ b/apidoc-core/build.gradle @@ -3,4 +3,5 @@ dependencies { compile 'com.fasterxml.jackson.core:jackson-databind:2.5.2' compile 'org.asciidoctor:asciidoctorj:2.1.0' compile group: 'org.freemarker', name: 'freemarker', version: '2.3.30' + compile group: 'com.google.guava', name: 'guava', version: '23.0' } diff --git a/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/Context.java b/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/Context.java index 44d11ada2ac54d75462ecf0ad697d6d90baea3b0..793509cef0166a50dda61937b87a475dfc7e1b48 100644 --- a/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/Context.java +++ b/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/Context.java @@ -1,6 +1,8 @@ package com.github.fengyuchenglun.apidoc.core; import com.github.fengyuchenglun.apidoc.core.common.Constants; +import com.github.fengyuchenglun.apidoc.core.render.AsciiDocRender; +import com.github.fengyuchenglun.apidoc.core.render.PostmanRender; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.github.fengyuchenglun.apidoc.core.common.helper.FileHelper; @@ -51,8 +53,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/github/fengyuchenglun/apidoc/core/common/QueryStringBuilder.java b/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/common/QueryStringBuilder.java index 9210614469f26ddac2b015a6c5b8e915a806046d..78a9e4115a5837b3ec83d21df5075c8236fc69e6 100644 --- a/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/common/QueryStringBuilder.java +++ b/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/common/QueryStringBuilder.java @@ -2,6 +2,8 @@ package com.github.fengyuchenglun.apidoc.core.common; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ObjectNode; +import org.apache.commons.lang3.ClassUtils; +import org.apache.commons.lang3.ObjectUtils; import java.util.Iterator; @@ -15,7 +17,7 @@ public class QueryStringBuilder { } builder.append(key); builder.append("="); - builder.append(value); + builder.append(ObjectUtils.isEmpty(value) ? "xxx" : value); return this; } diff --git a/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/common/description/ArrayTypeDescription.java b/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/common/description/ArrayTypeDescription.java index b6b014b40b2d3d4e9d73bbbeaa12e6c3939c23d8..a7ff30ad72cf5345da28346db0290ad4ada2f370 100644 --- a/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/common/description/ArrayTypeDescription.java +++ b/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/common/description/ArrayTypeDescription.java @@ -4,6 +4,7 @@ import com.github.fengyuchenglun.apidoc.core.common.ObjectMappers; import com.github.fengyuchenglun.apidoc.core.schema.Row; import com.fasterxml.jackson.databind.node.ArrayNode; import lombok.Data; +import lombok.EqualsAndHashCode; import java.util.ArrayList; import java.util.Collection; @@ -11,6 +12,7 @@ import java.util.Collection; /** * 数组类型描述 */ +@EqualsAndHashCode(callSuper = true) @Data public class ArrayTypeDescription extends TypeDescription { @@ -81,6 +83,7 @@ public class ArrayTypeDescription extends TypeDescription { } } + @Override public ArrayNode getValue() { return value; } diff --git a/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/common/description/ObjectTypeDescription.java b/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/common/description/ObjectTypeDescription.java index c138f6232c8704c26e68c530749114081cb0ba5b..6c70f1124f7a6bbd3483026cc57f46f5555e4039 100644 --- a/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/common/description/ObjectTypeDescription.java +++ b/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/common/description/ObjectTypeDescription.java @@ -5,6 +5,7 @@ import com.github.fengyuchenglun.apidoc.core.schema.Row; import com.fasterxml.jackson.databind.node.ObjectNode; import com.google.common.collect.Lists; import lombok.Data; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; @@ -17,6 +18,7 @@ import java.util.List; * @author fengyuchenglun * @version 1.0.0 */ +@EqualsAndHashCode(callSuper = true) @Data public class ObjectTypeDescription extends TypeDescription { @@ -136,11 +138,11 @@ public class ObjectTypeDescription extends TypeDescription { } @Override - public Collection rows(String requestParameterType) { + public Collection rows(String parameterType) { Collection rows = super.rows(null); for (TypeDescription member : members) { if (member.isAvailable()) { - rows.addAll(member.rows(requestParameterType)); + rows.addAll(member.rows(parameterType)); } } return rows; diff --git a/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/common/description/PrimitiveTypeDescription.java b/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/common/description/PrimitiveTypeDescription.java index 5b641749a19ed67257caba987e268bd3e5a0a9c7..caa727f0fa23648f6202f313c3fbacfabeed97ff 100644 --- a/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/common/description/PrimitiveTypeDescription.java +++ b/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/common/description/PrimitiveTypeDescription.java @@ -3,10 +3,12 @@ package com.github.fengyuchenglun.apidoc.core.common.description; import com.github.javaparser.resolution.types.ResolvedPrimitiveType; import com.github.javaparser.resolution.types.ResolvedReferenceType; import lombok.Data; +import lombok.EqualsAndHashCode; /** * 原始类型描述 */ +@EqualsAndHashCode(callSuper = true) @Data public class PrimitiveTypeDescription extends TypeDescription { diff --git a/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/common/description/StringTypeDescription.java b/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/common/description/StringTypeDescription.java index d866428d2642187e393119f76ac9f997a7eb7bc7..cda95636022de507880a7520967c78e2842dec22 100644 --- a/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/common/description/StringTypeDescription.java +++ b/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/common/description/StringTypeDescription.java @@ -1,10 +1,12 @@ package com.github.fengyuchenglun.apidoc.core.common.description; import lombok.Data; +import lombok.EqualsAndHashCode; /** * 字符串类型 */ +@EqualsAndHashCode(callSuper = true) @Data public class StringTypeDescription extends TypeDescription { diff --git a/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/common/description/TypeDescription.java b/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/common/description/TypeDescription.java index ff53410b5dc48b79016aa552b57b9ae18a6dd566..655ef561f070b92eae49c4fccf722421d5be367e 100644 --- a/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/common/description/TypeDescription.java +++ b/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/common/description/TypeDescription.java @@ -9,11 +9,13 @@ import com.google.common.collect.Lists; import lombok.Data; import lombok.Getter; import lombok.Setter; +import org.apache.commons.lang3.ObjectUtils; import java.util.Collection; /** * The type Type description. + * * @author duanledexianxianxian */ @Data @@ -51,7 +53,7 @@ public abstract class TypeDescription { /** * 是否必填. */ - protected Boolean required=false; + protected Boolean required = false; /** * Is available boolean. @@ -173,7 +175,7 @@ public abstract class TypeDescription { * * @return the collection */ - public Collection rows(String requestParameterType) { + public Collection rows(String parameterType) { String fullKey = fullKey(); if (StringHelper.isBlank(fullKey)) { return Lists.newArrayList(); @@ -191,7 +193,7 @@ public abstract class TypeDescription { // condition.append("required=").append(required); // } - return Lists.newArrayList(new Row(key, type, required, condition.toString(), def, remark, requestParameterType)); + return Lists.newArrayList(new Row(fullKey, type, required, condition.toString(), def, remark, parameterType)); } diff --git a/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/common/description/UnAvailableTypeDescription.java b/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/common/description/UnAvailableTypeDescription.java index 28567997dc98e4cf29eb0446ee327a52b17a7745..8a3860d1366c16a634c3ebfa9c10fcecd9e58276 100644 --- a/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/common/description/UnAvailableTypeDescription.java +++ b/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/common/description/UnAvailableTypeDescription.java @@ -2,6 +2,7 @@ package com.github.fengyuchenglun.apidoc.core.common.description; import com.github.fengyuchenglun.apidoc.core.schema.Row; import lombok.Data; +import lombok.EqualsAndHashCode; import java.util.Collection; @@ -9,6 +10,7 @@ import java.util.Collection; * 未知类型,应该忽略 * @author duanledexianxianxian */ +@EqualsAndHashCode(callSuper = true) @Data public class UnAvailableTypeDescription extends TypeDescription { diff --git a/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/parser/VisitorParser.java b/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/parser/VisitorParser.java index 7472041481fd30e5a4c8e74c556e91d7117e2823..7aa6080d7c3473b62bf918ce60386680ff4e3730 100644 --- a/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/parser/VisitorParser.java +++ b/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/parser/VisitorParser.java @@ -33,16 +33,22 @@ public class VisitorParser extends VoidVisitorAdapter { public void visit(final EnumDeclaration enumDeclaration, final Node arg) { // 访问枚举 if (arg instanceof Project) { - Project project = (Project) arg; - // 章节 - Chapter chapter = new Chapter(); - enumDeclaration.getFullyQualifiedName().ifPresent(chapter::setId); - chapter.setName(enumDeclaration.getNameAsString()); - enumDeclaration.getComment().ifPresent(chapter::accept); - OptionalHelper.any(chapter.getTag("book"), chapter.getTag("group")) - .ifPresent(tag -> chapter.setBookName(tag.getContent())); - project.addChapter(chapter); - super.visit(enumDeclaration, chapter); +// Project project = (Project) arg; +// // 章节 +// Chapter chapter = new Chapter(); +// enumDeclaration.getFullyQualifiedName().ifPresent(chapter::setId); +// chapter.setName(enumDeclaration.getNameAsString()); +// enumDeclaration.getComment().ifPresent(chapter::accept); +// OptionalHelper.any(chapter.getTag("book"), chapter.getTag("group")) +// .ifPresent(tag -> chapter.setBookName(tag.getContent())); +// project.addChapter(chapter); + //放入附录 + if (enumDeclaration.getJavadocComment().isPresent()) { + Appendix appendix = Appendix.parse(enumDeclaration.getJavadocComment().get()); + ApiDoc.getInstance().getProject().getAppendices().add(appendix); + } + +// super.visit(enumDeclaration, chapter); } } @@ -87,9 +93,7 @@ public class VisitorParser extends VoidVisitorAdapter { if (commentedNode instanceof ClassOrInterfaceDeclaration || commentedNode instanceof EnumDeclaration) { Appendix appendix = Appendix.parse(javadocComment); - if (appendix != null) { - ApiDoc.getInstance().getProject().getAppendices().add(appendix); - } + ApiDoc.getInstance().getProject().getAppendices().add(appendix); } } }); diff --git a/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/render/AsciiDocRender.java b/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/render/AsciiDocRender.java index 07f25e9365812b0f050050e37585bf7a4b97d308..e360b7c298ab142e8ae4c7671c53ea15f1bcbc37 100644 --- a/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/render/AsciiDocRender.java +++ b/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/render/AsciiDocRender.java @@ -74,7 +74,7 @@ public class AsciiDocRender implements ProjectRender { section.getInHeaders().values().forEach(header -> builder.textLine(header.toString())); if (section.hasRequestBody()) { b.br(); - b.text(section.getParameterString()); + b.text(section.getRequestBodyParameterString()); } }, "source,HTTP"); diff --git a/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/render/MarkdownRender.java b/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/render/MarkdownRender.java index a587fdfcf7695d72f943840197e259cb7496b5c5..6e95f2a931dfecba635397e3144e9472af652d18 100644 --- a/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/render/MarkdownRender.java +++ b/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/render/MarkdownRender.java @@ -43,7 +43,6 @@ public class MarkdownRender implements ProjectRender { } private void build(Project project) throws Exception { - System.out.println(project); String templatePath = ApiDoc.getInstance().getContext().getMarkdownTemplate(); String id = ApiDoc.getInstance().getContext().getId(); Path buildPath = ApiDoc.getInstance().getContext().getBuildPath(); diff --git a/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/render/PostmanRender.java b/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/render/PostmanRender.java index 538ec81aca5a2ebb3656daf6f0297d951c5cae74..991a4c302047a7e037f5f9ea0db30ce6b73fd3a1 100644 --- a/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/render/PostmanRender.java +++ b/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/render/PostmanRender.java @@ -74,18 +74,18 @@ public class PostmanRender implements ProjectRender { request.setMethod(section.getMethod()); request.getHeaders().addAll(section.getInHeaders().values()); + ObjectNode objectNode = (ObjectNode) section.getQueryParameters(); + for (String key : section.getRequestRows().keySet()) { + if (objectNode.has(key)) { + Row row = section.getRequestRows().get(key); + request.getUrl().getQuery().add(Parameter.of(row)); + } + } if (section.isQueryParameter()) { - if (Method.GET.equals(request.getMethod())) { - ObjectNode objectNode = (ObjectNode) section.getParameter(); - for (String key : section.getRequestRows().keySet()) { - if (objectNode.has(key)) { - Row row = section.getRequestRows().get(key); - request.getUrl().getQuery().add(Parameter.of(row)); - } - } - } else { + // get请求 + if (!Method.GET.equals(request.getMethod())) { request.getBody().setMode(BodyMode.urlencoded); - ObjectNode objectNode = (ObjectNode) section.getParameter(); + objectNode = (ObjectNode) section.getRequestBodyParameters(); for (String key : section.getRequestRows().keySet()) { if (objectNode.has(key)) { Row row = section.getRequestRows().get(key); @@ -95,7 +95,7 @@ public class PostmanRender implements ProjectRender { } } else { request.getBody().setMode(BodyMode.raw); - request.getBody().setRaw(section.getParameterString()); + request.getBody().setRaw(section.getRequestBodyParameterString()); } item.setRequest(request); diff --git a/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/schema/Appendix.java b/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/schema/Appendix.java index 4a8d39691c16dcda3981fa069300a22ed020dd3d..84ceedda3f47763eaaf83b387330f40d3bde9e4d 100644 --- a/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/schema/Appendix.java +++ b/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/schema/Appendix.java @@ -1,6 +1,7 @@ package com.github.fengyuchenglun.apidoc.core.schema; import com.github.javaparser.ast.body.BodyDeclaration; +import com.github.javaparser.ast.body.EnumDeclaration; import com.github.javaparser.ast.comments.JavadocComment; import com.github.javaparser.ast.nodeTypes.NodeWithSimpleName; import com.github.fengyuchenglun.apidoc.core.common.helper.EnumHelper; @@ -35,6 +36,8 @@ public class Appendix extends Node { return cells.isEmpty(); } + + /** * Parse appendix. * diff --git a/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/schema/RequestParameterType.java b/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/schema/ParameterType.java similarity index 90% rename from apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/schema/RequestParameterType.java rename to apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/schema/ParameterType.java index 98c3315bb009150db6a7e947529cbf5cf12739ca..df66496d613bde8d56ce0c18275185a6ea0cce0e 100644 --- a/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/schema/RequestParameterType.java +++ b/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/schema/ParameterType.java @@ -7,7 +7,7 @@ package com.github.fengyuchenglun.apidoc.core.schema; * @date 2020 /3/29 0:37 * @since 1.0.0 */ -public enum RequestParameterType { +public enum ParameterType { /** * 查询参数. */ @@ -29,7 +29,7 @@ public enum RequestParameterType { */ private String msg; - RequestParameterType(String code, String msg) { + ParameterType(String code, String msg) { this.code = code; this.msg = msg; } diff --git a/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/schema/Row.java b/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/schema/Row.java index 22956b919fcb757ad66e1df5b47f8a6d9fae6017..e623055113e1abfd55a858ae2b089cce0c736377 100644 --- a/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/schema/Row.java +++ b/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/schema/Row.java @@ -42,7 +42,7 @@ public class Row { /** * 请求参数类型 */ - String requestParameterType; + String parameterType; /** diff --git a/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/schema/Section.java b/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/schema/Section.java index 1d83c6551e79db9cd1f0557c1a59648c520ec59b..459e5707e0dd4587617e35423e3019a44f10a590 100644 --- a/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/schema/Section.java +++ b/apidoc-core/src/main/java/com/github/fengyuchenglun/apidoc/core/schema/Section.java @@ -20,7 +20,7 @@ import java.util.*; * @version 1.0.0 */ @Data -@EqualsAndHashCode(callSuper=true) +@EqualsAndHashCode(callSuper = true) public class Section extends Node { /** @@ -39,14 +39,24 @@ public class Section extends Node { * 路径变量. */ ObjectNode pathVariable = ObjectMappers.instance.createObjectNode(); - /** - * The Parameter. - */ - JsonNode parameter; /** * The Query parameter. */ boolean queryParameter = true; + + /** + * 查询参数+请求体参数 + */ + JsonNode parameters = ObjectMappers.instance.createObjectNode(); + /** + * 查询参数 + */ + JsonNode queryParameters = ObjectMappers.instance.createObjectNode(); + /** + * 请求体参数 + */ + JsonNode requestBodyParameters = ObjectMappers.instance.createObjectNode(); + /** * The Request rows. */ @@ -68,6 +78,38 @@ public class Section extends Node { */ Object rawResponse; + /** + * Sets parameter. + * + * @param parameter the parameter + */ + public void setParameters(JsonNode parameter) { + ((ObjectNode) this.parameters).setAll((ObjectNode) parameter); + } + + /** + * Sets query parameters. + * + * @param parameter the parameter + */ + public void setQueryParameters(JsonNode parameter) { + ((ObjectNode) this.queryParameters).setAll((ObjectNode) parameter); + } + + /** + * Sets request body parameters. + * + * @param parameter the parameter + */ + public void setRequestBodyParameters(JsonNode parameter) { + ((ObjectNode) this.requestBodyParameters).setAll((ObjectNode) parameter); + } + + /** + * Sets uri. + * + * @param uri the uri + */ public void setUri(String uri) { String value = ""; if (StringUtils.isNotBlank(uri)) { @@ -106,8 +148,8 @@ public class Section extends Node { public String getRequestLine() { StringBuilder builder = new StringBuilder(this.method.toString()); builder.append(" ").append(this.uri); - if (this.queryParameter && Objects.equals("GET", this.method)) { - String parameterString = getParameterString(); + if (Objects.equals("GET", this.method)) { + String parameterString = getQueryParameterString(); if (StringHelper.nonBlank(parameterString)) { builder.append("?").append(parameterString); } @@ -116,16 +158,39 @@ public class Section extends Node { return builder.toString(); } + /** - * Get parameter string string. + * Gets query parameter string. * - * @return the string + * @return the query parameter string */ public String getParameterString() { - if (queryParameter && parameter instanceof ObjectNode) { - return new QueryStringBuilder().append((ObjectNode) parameter).toString(); + if (queryParameter) { + // 查询参数 + new QueryStringBuilder().append((ObjectNode) queryParameters).toString(); } - return ObjectMappers.pretty(parameter); + // 请求体参数 + return ObjectMappers.pretty(parameters); + } + + + /** + * Gets query parameter string. + * + * @return the query parameter string + */ + public String getQueryParameterString() { + return new QueryStringBuilder().append((ObjectNode) queryParameters).toString(); + } + + /** + * Gets request body parameter string. + * + * @return the request body parameter string + */ + public String getRequestBodyParameterString() { + // 请求体参数 + return ObjectMappers.pretty(requestBodyParameters); } /** @@ -137,7 +202,7 @@ public class Section extends Node { if (Objects.equals("GET", this.method)) { return false; } - return parameter != null && parameter.size() > 0; + return requestBodyParameters != null && requestBodyParameters.size() > 0; } /** diff --git a/apidoc-core/src/main/resources/templates/markdown.ftl b/apidoc-core/src/main/resources/templates/markdown.ftl index bfe474cc4fd8b46dc772b42b02bf005302d0fdb1..24df5739d866922762d6b1886455daf301e4de07 100644 --- a/apidoc-core/src/main/resources/templates/markdown.ftl +++ b/apidoc-core/src/main/resources/templates/markdown.ftl @@ -46,9 +46,13 @@ ${section.method} ${section.uri!''} HTTP/1.1 ${inHeaderValue!''} - <#if section.hasRequestBody()> +<#if (section.getQueryParameterString()?length gt 1)> -${section.getParameterString()} +${section.getQueryParameterString()} + + <#if section.hasRequestBody()> + +${section.getRequestBodyParameterString()} ``` <#-- 请求参数table列表--> @@ -57,7 +61,7 @@ ${section.getParameterString()} | 字段 | 类型 | 参数类型 | 是否必填 | 验证 | 默认值 | 描述 | | :------- | :----- | :----- |:-------- |:-------- | :------ | :---------- | <#list section.requestRows as rowKey,rowValue> -| ${rowValue.key!''} | ${rowValue.type!''} | **${rowValue.requestParameterType!''}** |${rowValue.required?string('true','false')} | ${rowValue.condition!''} | ${rowValue.def!''} | ${rowValue.remark!''} | +| ${rowValue.key!''} | ${rowValue.type!''} | **${rowValue.parameterType!''}** |${rowValue.required?string('true','false')} | ${rowValue.condition!''} | ${rowValue.def!''} | ${rowValue.remark!''} | <#-- 响应--> @@ -85,3 +89,17 @@ ${section.getResponseString()} <#------------ END 循环遍历book ----------> + +# 附录 +<#if appendices??> + <#list appendices as appendfix> +## ${appendfix.name} + <#if appendfix.cells?? && (appendfix.cells?size>0)> +| 编码 | 值 | 说明 | +| :------- | :----- |:----- + <#list appendfix.cells as cell> +| <#if (cell.values?size>0)>${cell.values[0]} | <#if (cell.values?size>1)>${cell.values[1]} | <#if (cell.values?size>2)>${cell.values[2]} | + + + + diff --git a/apidoc-springmvc/src/main/java/com/github/fengyuchenglun/apidoc/springmvc/SpringParser.java b/apidoc-springmvc/src/main/java/com/github/fengyuchenglun/apidoc/springmvc/SpringParser.java index 9f4d0668f587e4ee92e331f61c21cbc30a897a05..f56d30cc50f6b8a76c7b55b12bf2b6c10982d106 100644 --- a/apidoc-springmvc/src/main/java/com/github/fengyuchenglun/apidoc/springmvc/SpringParser.java +++ b/apidoc-springmvc/src/main/java/com/github/fengyuchenglun/apidoc/springmvc/SpringParser.java @@ -24,7 +24,7 @@ import com.github.fengyuchenglun.apidoc.springmvc.resovler.SpringComponentTypeRe import java.util.List; import java.util.Optional; -import static com.github.fengyuchenglun.apidoc.core.schema.RequestParameterType.*; +import static com.github.fengyuchenglun.apidoc.core.schema.ParameterType.*; /** @@ -141,10 +141,12 @@ public class SpringParser implements ParserStrategy { * @param section the section */ private void visitParameters(MethodDeclaration n, Chapter chapter, Section section) { - if (ParameterHelper.hasRequestBody(n.getParameters())) { - visitRequestBody(n, chapter, section); - } else { - visitParameter(n, chapter, section); + for (Parameter parameter : n.getParameters()) { + if (ParameterHelper.isRequestBody(parameter)) { + visitRequestBody(n, chapter, section); + } else { + visitParameter(n, chapter, section); + } } } @@ -160,7 +162,7 @@ public class SpringParser implements ParserStrategy { if (ParameterHelper.isPathVariable(parameter)) { section.getPathVariable().put(parameter.getNameAsString(), ""); Row row = new Row(); - row.setRequestParameterType(PATH.getMsg()); + row.setParameterType(PATH.getMsg()); row.setKey(parameter.getNameAsString()); row.setType(parameter.getType().toString()); // 路径参数必填 @@ -235,9 +237,9 @@ public class SpringParser implements ParserStrategy { TypeDescription description = ApiDoc.getInstance().getTypeResolvers().resolve(parameter.getType()); if (description.isAvailable()) { if (description.isArray()) { - section.setParameter(description.asArray().getValue()); + section.setRequestBodyParameters(description.asArray().getValue()); } else if (description.isObject()) { - section.setParameter(description.asObject().getValue()); + section.setRequestBodyParameters(description.asObject().getValue()); } section.addRequestRows(description.rows(BODY.getMsg())); } @@ -299,7 +301,7 @@ public class SpringParser implements ParserStrategy { } } } - section.setParameter(objectTypeDescription.getValue()); + section.setQueryParameters(objectTypeDescription.getValue()); section.addRequestRows(objectTypeDescription.rows(QUERY.getMsg())); } diff --git a/apidoc-springmvc/src/test/java/com/github/fengyuchenglun/example/common/Query.java b/apidoc-springmvc/src/test/java/com/github/fengyuchenglun/example/common/Query.java index c71b8055b9629ac4a3d86a007be9c0e6cb0425e4..2d79eef8e489cbb807a585b43b08ab969e125ba9 100644 --- a/apidoc-springmvc/src/test/java/com/github/fengyuchenglun/example/common/Query.java +++ b/apidoc-springmvc/src/test/java/com/github/fengyuchenglun/example/common/Query.java @@ -3,6 +3,8 @@ package com.github.fengyuchenglun.example.common; import lombok.Getter; import lombok.Setter; +import java.time.LocalDateTime; + @Setter @Getter public class Query { @@ -13,8 +15,24 @@ public class Query { public static final String CONSTANS = ""; /** - * 查询关键字 + * 用户名称 + */ + private String userName; + /** + * 用户手机号码 + */ + private String userPhoneNum; + /** + * 用户邮箱 + */ + private String userEmail; + /** + * 开始时间 + */ + private LocalDateTime startTime; + /** + * 结束时间 */ - String q = "123"; + private LocalDateTime endTime; } diff --git a/apidoc-springmvc/src/test/java/com/github/fengyuchenglun/example/common/ResultData.java b/apidoc-springmvc/src/test/java/com/github/fengyuchenglun/example/common/ResultData.java index ff9d0bcd2b507c0e0abc35f8076dd5cf38af25ff..6a38052e5573c608f2085c3a0c2ce5bb3c7bd0f7 100644 --- a/apidoc-springmvc/src/test/java/com/github/fengyuchenglun/example/common/ResultData.java +++ b/apidoc-springmvc/src/test/java/com/github/fengyuchenglun/example/common/ResultData.java @@ -4,7 +4,7 @@ import lombok.Getter; import lombok.Setter; /** - * The type Result data. + * 统一返回结果对象. * * @param the type parameter * @author fengyuchenglun @@ -25,7 +25,7 @@ public class ResultData { //返回信息 String msg; /** - * The Data. + * 数据对象. */ T data; @@ -35,7 +35,7 @@ public class ResultData { * @param the type parameter * @return the result data */ - public static ResultData ok(){ + public static ResultData ok() { return ok(null); } @@ -46,7 +46,7 @@ public class ResultData { * @param data the data * @return the result data */ - public static ResultData ok(T data){ + public static ResultData ok(T data) { ResultData resultData = new ResultData<>(); resultData.code = 0; resultData.msg = "ok"; diff --git a/apidoc-springmvc/src/test/java/com/github/fengyuchenglun/example/common/User.java b/apidoc-springmvc/src/test/java/com/github/fengyuchenglun/example/common/User.java index 7565d08be99e7293509cad09dbc2a001c87177b3..fe798e915ec2b3819b90a1974cff589a98c51703 100644 --- a/apidoc-springmvc/src/test/java/com/github/fengyuchenglun/example/common/User.java +++ b/apidoc-springmvc/src/test/java/com/github/fengyuchenglun/example/common/User.java @@ -8,6 +8,7 @@ import javax.validation.constraints.Min; import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; import java.util.Date; +import java.util.List; /** * 用户对象 @@ -19,30 +20,40 @@ import java.util.Date; @Getter public class User { +// /** +// * 用户编号. +// */ +// int id; +// /** +// * 用户名称. +// */ +// @NotBlank +// String name; +// /** +// * 用户年龄. +// */ +// @Min(1) +// @NotNull +// Integer age; +// /** +// * 创建时间. +// */ +// Date createAt; +// /** +// * 性别. +// */ +// @NotBlank +// @JsonProperty("Sex") +// String sex; + /** - * 用户编号. - */ - int id; - /** - * 用户名称. - */ - @NotBlank - String name; - /** - * 用户年龄. - */ - @Min(1) - @NotNull - Integer age; - /** - * 创建时间. + * 用户. */ - Date createAt; + private UserQuery query; + /** - * 性别. + * 用户. */ - @NotBlank - @JsonProperty("Sex") - String sex; + private List menus; } diff --git a/apidoc-springmvc/src/test/java/com/github/fengyuchenglun/example/spring/advanced/UserController.java b/apidoc-springmvc/src/test/java/com/github/fengyuchenglun/example/spring/advanced/UserController.java index 046aba5a991f4f2998578fb6cd6714cbc8f5c9cd..8a8214c7d6e5fef186caebf5bce9cd4b217ea062 100644 --- a/apidoc-springmvc/src/test/java/com/github/fengyuchenglun/example/spring/advanced/UserController.java +++ b/apidoc-springmvc/src/test/java/com/github/fengyuchenglun/example/spring/advanced/UserController.java @@ -1,6 +1,5 @@ package com.github.fengyuchenglun.example.spring.advanced; -import com.github.fengyuchenglun.example.annotation.KimController; import com.github.fengyuchenglun.example.common.Query; import com.github.fengyuchenglun.example.common.User; import com.github.fengyuchenglun.example.common.UserQuery; @@ -8,6 +7,7 @@ import lombok.Data; import org.springframework.web.bind.annotation.*; import java.io.Serializable; +import java.util.List; /** * 用户接口. @@ -18,138 +18,141 @@ import java.io.Serializable; * @index 20 * @since 1.0.0 */ -@KimController +@RestController @RequestMapping("/api/v1/users") public class UserController { - - /** - * 查看用户详情 - * - * @param userId 用户编号 - * @param query 过滤条件 - * @return 用户对象 user - */ - @GetMapping(value = "/{userId}") - public User detail(@PathVariable String userId, Query query) { - User user = new User(); - return user; - } - - /** - * 查看用户详情 - * - * @param query 过滤条件 - * @return 用户对象 user - */ - @GetMapping(value = "/detail1") - public User detail1(Query query) { - User user = new User(); - return user; - } - - /** - * 查看用户详情 - * - * @param query 过滤条件 - * @return 用户对象 user - */ - @GetMapping(value = "/detail3") - public User detail3(@RequestParam Query query) { - User user = new User(); - return user; - } - - /** - * 查看用户详情 - * - * @param query 过滤条件 - * @return 用户对象 user - */ - @GetMapping(value = "/detail4") - public User detail4(@RequestParam(required = false) Query query) { - User user = new User(); - return user; - } - - /** - * 查看用户详情 - * - * @return 用户对象 user - */ - @GetMapping(value = "/detail5") - public User detail5() { - User user = new User(); - return user; - } - - /** - * 查看用户详情 - * - * @param userName the user name - * @param age the age - * @param query the query - * @return 用户对象 user - */ - @GetMapping(value = "/detail6") - public User detail6(String userName, Integer age, UserQuery query) { - User user = new User(); - return user; - } - - /** - * 查看用户详情 - * - * @return 用户对象 user - */ - @GetMapping(value = "/detail7") - public void detail7() { - } - - - /** - * 删除用户 - * - * @param userId the user id - * @return boolean 是否成功 - */ - @DeleteMapping(value = "/{userId}") - public Boolean deleteUser(@PathVariable("userId") Long userId) { - return true; - } - - - /** - * 用户表单对象. - * - * @author duanledexianxianxian - */ - @Data - public static class UserForm implements Serializable { - - private static final long serialVersionUID = 5681371348688016281L; - /** - * 用户名 - */ - private String userName; - /** - * 地址 - */ - private String address; - /** - * 年龄 - */ - private Integer age; - } - - /** - * 添加用户 - * - * @param form 用户表单对象 - * @return integer 返回记录 - */ - @PostMapping - public Integer add(@RequestBody UserForm form) { - return null; - } +// +// /** +// * 查看用户详情 +// * +// * @param userId 用户编号 +// * @param age 年龄 +// * @param query 过滤条件 +// * @return 用户对象 user +// */ +// @PostMapping(value = "/{userId}") +// public User detail(@PathVariable String userId, String age, @RequestBody Query query) { +// User user = new User(); +// return user; +// } +// +// /** +// * 查看用户详情 +// * 测试get的query对象 +// * +// * @param query 过滤条件 +// * @return 用户对象 user +// */ +// @GetMapping(value = "/detail1") +// public User detail1(Query query) { +// User user = new User(); +// return user; +// } +// +// +// /** +// * 测试get的query对象,带RequestParam注解 +// * +// * @param query 过滤条件 +// * @return 用户对象 user +// */ +// @GetMapping(value = "/detail3") +// public User detail3(@RequestParam String query) { +// User user = new User(); +// return user; +// } +// +// /** +// * 测试get的query对象,带RequestParam注解,required=false +// * +// * @param query 过滤条件 +// * @return 用户对象 user +// */ +// @GetMapping(value = "/detail4") +// public User detail4(@RequestParam(required = false) String query) { +// User user = new User(); +// return user; +// } +// +// /** +// * get-无查询参数 +// * +// * @return 用户对象 user +// */ +// @GetMapping(value = "/detail5") +// public List detail5() { +// return null; +// } +// +// /** +// * get-原始对象+查询对象 +// * +// * @param userName the user name +// * @param age the age +// * @param query the query +// * @return 用户对象 user +// */ +// @GetMapping(value = "/detail6") +// public User detail6(String userName, Integer age, UserQuery query) { +// User user = new User(); +// return user; +// } +// +// /** +// * post- +// * +// * @return 用户对象 user +// */ +// @GetMapping(value = "/post1") +// public UserQuery post1() { +// return null; +// } +// +// +// /** +// * 删除用户 +// * +// * @param userId the user id +// * @return boolean 是否成功 +// */ +// @DeleteMapping(value = "/{userId}") +// public Boolean deleteUser(@PathVariable("userId") Long userId) { +// return true; +// } +// +// +// /** +// * 用户表单对象. +// * +// * @author duanledexianxianxian +// */ +// @Data +// public static class UserForm implements Serializable { +// +// private static final long serialVersionUID = 5681371348688016281L; +// /** +// * 用户名 +// */ +// private String userName; +// /** +// * 地址 +// */ +// private String address; +// /** +// * 年龄 +// */ +// private Integer age; +// } +// +// /** +// * 添加用户 +// * +// * @param form 用户表单对象 +// * @return integer 返回记录 +// */ +// @PostMapping +// public Integer add(@RequestBody UserForm form) { +// return null; +// } }