From 4b13cbe83b3e83cce15f3def65bfd3541a0170d2 Mon Sep 17 00:00:00 2001 From: oppofind <836575280@qq.com> Date: Sat, 24 Apr 2021 15:18:08 +0800 Subject: [PATCH 1/5] Fix #109. --- CHANGELOG.md | 8 ++++ .../template/SpringBootDocBuildTemplate.java | 45 +++++++++++++++++-- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 74232b3e..e0992d1e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ ## smart-doc版本 版本小于1.0都属于试用,正式1.0起始发布将会等到文中提到的问题解决后才发布。 +#### 版本号:2.1.4 +- 更新日期: 2020-04-24 +- 更新内容: + 1. 修复Controller继承时,父类的Mapping未继承的问题。 + 2. 修复配置responseBodyAdvice后,controller中void方法返回显示错误。 + 3. 修复往torna推送漏掉pathParams的问题。 + 4. 修复非json请求集合中绑定枚举强制检查错误的问题。 + 5. 新增requestAdvice支持,可以实现请求参数包装 #### 版本号:2.1.3 - 更新日期: 2020-04-11 - 更新内容: diff --git a/src/main/java/com/power/doc/template/SpringBootDocBuildTemplate.java b/src/main/java/com/power/doc/template/SpringBootDocBuildTemplate.java index 1c82ffd7..af112a31 100644 --- a/src/main/java/com/power/doc/template/SpringBootDocBuildTemplate.java +++ b/src/main/java/com/power/doc/template/SpringBootDocBuildTemplate.java @@ -120,7 +120,7 @@ private List<ApiMethodDoc> buildControllerMethod(final JavaClass cls, ApiConfig boolean paramsDataToTree = projectBuilder.getApiConfig().isParamsDataToTree(); String group = JavaClassUtil.getClassTagsValue(cls, DocTags.GROUP, Boolean.TRUE); String classAuthor = JavaClassUtil.getClassTagsValue(cls, DocTags.AUTHOR, Boolean.TRUE); - List<JavaAnnotation> classAnnotations = cls.getAnnotations(); + List<JavaAnnotation> classAnnotations = this.getAnnotations(cls); Map<String, String> constantsMap = projectBuilder.getConstantsMap(); String baseUrl = ""; for (JavaAnnotation annotation : classAnnotations) { @@ -411,7 +411,8 @@ private ApiRequestExample buildReqJson(DocJavaMethod javaMethod, ApiMethodDoc ap if (JavaClassValidateUtil.isArray(gicName)) { gicName = gicName.substring(0, gicName.indexOf("[")); } - if (!JavaClassValidateUtil.isPrimitive(gicName)) { + if (!JavaClassValidateUtil.isPrimitive(gicName) + &&!configBuilder.getJavaProjectBuilder().getClassByName(gicName).isEnum()) { throw new RuntimeException("Spring MVC can't support binding Collection on method " + method.getName() + "Check it in " + method.getDeclaringClass().getCanonicalName()); } @@ -667,14 +668,29 @@ private ApiMethodReqParam requestParams(final DocJavaMethod docJavaMethod, Proje if (JavaClassValidateUtil.isArray(gicName)) { gicName = gicName.substring(0, gicName.indexOf("[")); } - if (JavaClassValidateUtil.isPrimitive(gicName)) { + JavaClass gicJavaClass = builder.getJavaProjectBuilder().getClassByName(gicName); + if(gicJavaClass.isEnum()){ + Object value = JavaClassUtil.getEnumValue(gicJavaClass, Boolean.TRUE); + ApiParam param = ApiParam.of().setField(paramName).setDesc(comment + ",[array of enum]") + .setRequired(required) + .setPathParam(isPathVariable) + .setQueryParam(queryParam) + .setId(paramList.size() + 1) + .setType("array").setValue(String.valueOf(value)); + paramList.add(param); + if (requestBodyCounter > 0) { + Map<String, Object> map = OpenApiSchemaUtil.arrayTypeSchema(gicName); + docJavaMethod.setRequestSchema(map); + } + } else if (JavaClassValidateUtil.isPrimitive(gicName)) { String shortSimple = DocClassUtil.processTypeNameForParams(gicName); ApiParam param = ApiParam.of().setField(paramName).setDesc(comment + ",[array of " + shortSimple + "]") .setRequired(required) .setPathParam(isPathVariable) .setQueryParam(queryParam) .setId(paramList.size() + 1) - .setType("array"); + .setType("array") + .setValue(DocUtil.getValByTypeAndFieldName(gicName,paramName)); paramList.add(param); if (requestBodyCounter > 0) { Map<String, Object> map = OpenApiSchemaUtil.arrayTypeSchema(gicName); @@ -878,4 +894,25 @@ private void mappingParamToApiParam(String str, List<ApiParam> paramList, Map<St paramList.add(apiParam); mappingParams.put(paramName, null); } + + private List<JavaAnnotation> getAnnotations(JavaClass cls) { + List<JavaAnnotation> annotationsList = cls.getAnnotations(); + boolean flag = annotationsList.stream().anyMatch(item -> { + String annotationName = item.getType().getValue(); + if (DocAnnotationConstants.REQUEST_MAPPING.equals(annotationName) || + DocGlobalConstants.REQUEST_MAPPING_FULLY.equals(annotationName)) { + return true; + } + return false; + }); + // child override parent set + if (flag) { + return annotationsList; + } + JavaClass superJavaClass = cls.getSuperJavaClass(); + if (!"Object".equals(superJavaClass.getSimpleName())) { + annotationsList.addAll(getAnnotations(superJavaClass)); + } + return annotationsList; + } } From 92eb919aed400a55143e8df9ae42ee1a81eb70be Mon Sep 17 00:00:00 2001 From: oppofind <836575280@qq.com> Date: Sat, 24 Apr 2021 22:23:01 +0800 Subject: [PATCH 2/5] support override filename --- .../com/power/doc/builder/HtmlApiDocBuilder.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/power/doc/builder/HtmlApiDocBuilder.java b/src/main/java/com/power/doc/builder/HtmlApiDocBuilder.java index 1902e2fe..6603e7b5 100644 --- a/src/main/java/com/power/doc/builder/HtmlApiDocBuilder.java +++ b/src/main/java/com/power/doc/builder/HtmlApiDocBuilder.java @@ -77,14 +77,18 @@ public static void buildApiDoc(ApiConfig config, JavaProjectBuilder javaProjectB Template indexCssTemplate = BeetlTemplateUtil.getByName(ALL_IN_ONE_CSS); FileUtil.nioWriteFile(indexCssTemplate.render(), config.getOutPath() + FILE_SEPARATOR + ALL_IN_ONE_CSS); if (config.isAllInOne()) { - if (StringUtils.isNotEmpty(config.getAllInOneDocFileName())) { - INDEX_HTML = config.getAllInOneDocFileName(); - } if (config.isCreateDebugPage()) { - builderTemplate.buildAllInOne(apiDocList, config, javaProjectBuilder, DEBUG_PAGE_ALL_TPL, DEBUG_PAGE_ALL_TPL); + INDEX_HTML = DEBUG_PAGE_ALL_TPL; + if (StringUtils.isNotEmpty(config.getAllInOneDocFileName())) { + INDEX_HTML = config.getAllInOneDocFileName(); + } + builderTemplate.buildAllInOne(apiDocList, config, javaProjectBuilder, DEBUG_PAGE_ALL_TPL, INDEX_HTML); Template mockJs = BeetlTemplateUtil.getByName(DEBUG_JS_TPL); FileUtil.nioWriteFile(mockJs.render(), config.getOutPath() + FILE_SEPARATOR + DEBUG_JS_OUT); } else { + if (StringUtils.isNotEmpty(config.getAllInOneDocFileName())) { + INDEX_HTML = config.getAllInOneDocFileName(); + } builderTemplate.buildAllInOne(apiDocList, config, javaProjectBuilder, ALL_IN_ONE_HTML_TPL, INDEX_HTML); } builderTemplate.buildSearchJs(config, javaProjectBuilder, apiDocList, SEARCH_ALL_JS_TPL); From 417956cb9e5827e2a9332967e95bbdcbab701613 Mon Sep 17 00:00:00 2001 From: Forget <1771761586@qq.com> Date: Tue, 20 Apr 2021 12:31:48 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B3=9B=E5=9E=8B?= =?UTF-8?q?=E4=B8=BAlist=E6=97=B6=EF=BC=8C=E7=B1=BB=E5=9E=8B=E4=B8=BAobjec?= =?UTF-8?q?t=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/power/doc/helper/ParamsBuildHelper.java | 1 + src/main/java/com/power/doc/utils/JavaClassUtil.java | 3 +++ 2 files changed, 4 insertions(+) diff --git a/src/main/java/com/power/doc/helper/ParamsBuildHelper.java b/src/main/java/com/power/doc/helper/ParamsBuildHelper.java index c750174a..c02384aa 100644 --- a/src/main/java/com/power/doc/helper/ParamsBuildHelper.java +++ b/src/main/java/com/power/doc/helper/ParamsBuildHelper.java @@ -406,6 +406,7 @@ public static List<ApiParam> buildParams(String className, String pre, int level //do nothing } else if (gicName.contains("<")) { if (JavaClassValidateUtil.isCollection(simple)) { + param.setType(ARRAY); String gName = DocClassUtil.getSimpleGicName(gicName)[0]; if (!JavaClassValidateUtil.isPrimitive(gName)) { paramList.addAll(buildParams(gName, preBuilder.toString(), nextLevel, isRequired, diff --git a/src/main/java/com/power/doc/utils/JavaClassUtil.java b/src/main/java/com/power/doc/utils/JavaClassUtil.java index 448c6e25..b6a7704a 100644 --- a/src/main/java/com/power/doc/utils/JavaClassUtil.java +++ b/src/main/java/com/power/doc/utils/JavaClassUtil.java @@ -497,6 +497,9 @@ public static String javaTypeFormat(String returnType) { public static boolean isTargetChildClass(String sourceClass,String targetClass){ try { + if(sourceClass.equals(targetClass)){ + return true; + } Class c = Class.forName(sourceClass); while (c != null){ if(c.getName().equals(targetClass)){ From ecc4e0992155bc15a5d8cd61b7e4099c7570fde5 Mon Sep 17 00:00:00 2001 From: Forget <1771761586@qq.com> Date: Sun, 25 Apr 2021 14:42:20 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=BD=93=E8=AE=BE?= =?UTF-8?q?=E7=BD=AEresponseadvice=20=E5=87=BD=E6=95=B0=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E7=B1=BB=E5=9E=8Bvoid=E6=97=B6=E6=B2=A1=E6=9C=89=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/power/doc/template/IDocBuildTemplate.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/power/doc/template/IDocBuildTemplate.java b/src/main/java/com/power/doc/template/IDocBuildTemplate.java index fc7b1e41..c94e1684 100644 --- a/src/main/java/com/power/doc/template/IDocBuildTemplate.java +++ b/src/main/java/com/power/doc/template/IDocBuildTemplate.java @@ -96,7 +96,7 @@ default void handleApiDoc(JavaClass cls, List<ApiDoc> apiDocList, List<ApiMethod default List<ApiParam> buildReturnApiParams(DocJavaMethod docJavaMethod, ProjectDocConfigBuilder projectBuilder) { JavaMethod method = docJavaMethod.getJavaMethod(); - if (method.getReturns().isVoid()) { + if (method.getReturns().isVoid() && Objects.isNull(projectBuilder.getApiConfig().getResponseBodyAdvice())) { return new ArrayList<>(0); } String returnTypeGenericCanonicalName = method.getReturnType().getGenericCanonicalName(); From bea4c1a2ed05abd62c171d9e681a09d85e44b89b Mon Sep 17 00:00:00 2001 From: Forget <1771761586@qq.com> Date: Sun, 25 Apr 2021 16:06:29 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E6=96=B0=E5=A2=9ErequestBodyAdvice?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 4 +++- .../doc/builder/ProjectDocConfigBuilder.java | 15 +++++++-------- .../java/com/power/doc/constants/DocTags.java | 2 ++ src/main/java/com/power/doc/model/ApiConfig.java | 16 +++++++++++++--- .../{ResponseBodyAdvice.java => BodyAdvice.java} | 12 ++++++------ .../doc/template/SpringBootDocBuildTemplate.java | 15 ++++++++++++++- 6 files changed, 45 insertions(+), 19 deletions(-) rename src/main/java/com/power/doc/model/{ResponseBodyAdvice.java => BodyAdvice.java} (84%) diff --git a/CHANGELOG.md b/CHANGELOG.md index e0992d1e..d839a86b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,9 @@ 2. 修复配置responseBodyAdvice后,controller中void方法返回显示错误。 3. 修复往torna推送漏掉pathParams的问题。 4. 修复非json请求集合中绑定枚举强制检查错误的问题。 - 5. 新增requestAdvice支持,可以实现请求参数包装 + 5. 新增requestBodyAdvice支持,可以实现请求参数包装。 + 6. 修复泛型为List数据时,类型为object问题。 + 7. 修复customFiled为继承参数时配置失效问题。 #### 版本号:2.1.3 - 更新日期: 2020-04-11 - 更新内容: diff --git a/src/main/java/com/power/doc/builder/ProjectDocConfigBuilder.java b/src/main/java/com/power/doc/builder/ProjectDocConfigBuilder.java index 670668bd..845c1f56 100644 --- a/src/main/java/com/power/doc/builder/ProjectDocConfigBuilder.java +++ b/src/main/java/com/power/doc/builder/ProjectDocConfigBuilder.java @@ -90,7 +90,8 @@ public ProjectDocConfigBuilder(ApiConfig apiConfig, JavaProjectBuilder javaProje this.initCustomRequestFieldsMap(apiConfig); this.initReplaceClassMap(apiConfig); this.initConstants(apiConfig); - this.checkResponseBodyAdvice(apiConfig); + this.checkBodyAdvice(apiConfig.getRequestBodyAdvice()); + this.checkBodyAdvice(apiConfig.getResponseBodyAdvice()); } public JavaClass getClassByName(String simpleName) { @@ -179,20 +180,18 @@ private void initConstants(ApiConfig config) { } } - private void checkResponseBodyAdvice(ApiConfig config) { - ResponseBodyAdvice responseBodyAdvice = config.getResponseBodyAdvice(); - if (Objects.nonNull(responseBodyAdvice) && StringUtil.isNotEmpty(responseBodyAdvice.getClassName())) { - if (Objects.nonNull(responseBodyAdvice.getWrapperClass())) { + private void checkBodyAdvice(BodyAdvice bodyAdvice) { + if (Objects.nonNull(bodyAdvice) && StringUtil.isNotEmpty(bodyAdvice.getClassName())) { + if (Objects.nonNull(bodyAdvice.getWrapperClass())) { return; } try { - Class.forName(responseBodyAdvice.getClassName()); + Class.forName(bodyAdvice.getClassName()); } catch (ClassNotFoundException e) { - throw new RuntimeException("Can't find class " + responseBodyAdvice.getClassName() + " for ResponseBodyAdvice."); + throw new RuntimeException("Can't find class " + bodyAdvice.getClassName() + " for ResponseBodyAdvice."); } } } - /** * 设置高亮样式 */ diff --git a/src/main/java/com/power/doc/constants/DocTags.java b/src/main/java/com/power/doc/constants/DocTags.java index 79c9404a..18c2a8fe 100644 --- a/src/main/java/com/power/doc/constants/DocTags.java +++ b/src/main/java/com/power/doc/constants/DocTags.java @@ -112,4 +112,6 @@ public interface DocTags { * Ignore ResponseBodyAdvice */ String IGNORE_RESPONSE_BODY_ADVICE = "ignoreResponseBodyAdvice"; + + String IGNORE_REQUEST_BODY_ADVICE = "ignoreRequestBodyAdvice"; } diff --git a/src/main/java/com/power/doc/model/ApiConfig.java b/src/main/java/com/power/doc/model/ApiConfig.java index 73873d9b..517c6706 100644 --- a/src/main/java/com/power/doc/model/ApiConfig.java +++ b/src/main/java/com/power/doc/model/ApiConfig.java @@ -268,7 +268,9 @@ public class ApiConfig { * Support Spring MVC ResponseBodyAdvice * @since 1.9.8 */ - private ResponseBodyAdvice responseBodyAdvice; + private BodyAdvice responseBodyAdvice; + + private BodyAdvice requestBodyAdvice; private String style; @@ -718,14 +720,22 @@ public void setDisplayActualType(boolean displayActualType) { this.displayActualType = displayActualType; } - public ResponseBodyAdvice getResponseBodyAdvice() { + public BodyAdvice getResponseBodyAdvice() { return responseBodyAdvice; } - public void setResponseBodyAdvice(ResponseBodyAdvice responseBodyAdvice) { + public void setResponseBodyAdvice(BodyAdvice responseBodyAdvice) { this.responseBodyAdvice = responseBodyAdvice; } + public BodyAdvice getRequestBodyAdvice() { + return requestBodyAdvice; + } + + public void setRequestBodyAdvice(BodyAdvice requestBodyAdvice) { + this.requestBodyAdvice = requestBodyAdvice; + } + public String getStyle() { return style; } diff --git a/src/main/java/com/power/doc/model/ResponseBodyAdvice.java b/src/main/java/com/power/doc/model/BodyAdvice.java similarity index 84% rename from src/main/java/com/power/doc/model/ResponseBodyAdvice.java rename to src/main/java/com/power/doc/model/BodyAdvice.java index fedefdb6..1e2a726d 100644 --- a/src/main/java/com/power/doc/model/ResponseBodyAdvice.java +++ b/src/main/java/com/power/doc/model/BodyAdvice.java @@ -26,7 +26,7 @@ * @since 1.9.8 * @author yu 2020/11/5. */ -public class ResponseBodyAdvice { +public class BodyAdvice { private String className; @@ -34,15 +34,15 @@ public class ResponseBodyAdvice { private String dataField; - public static ResponseBodyAdvice builder(){ - return new ResponseBodyAdvice(); + public static BodyAdvice builder(){ + return new BodyAdvice(); } public String getClassName() { return className; } - public ResponseBodyAdvice setClassName(String className) { + public BodyAdvice setClassName(String className) { this.className = className; return this; } @@ -51,7 +51,7 @@ public String getDataField() { return dataField; } - public ResponseBodyAdvice setDataField(String dataField) { + public BodyAdvice setDataField(String dataField) { this.dataField = dataField; return this; } @@ -60,7 +60,7 @@ public Class getWrapperClass() { return wrapperClass; } - public ResponseBodyAdvice setWrapperClass(Class wrapperClass) { + public BodyAdvice setWrapperClass(Class wrapperClass) { this.wrapperClass = wrapperClass; return this; } diff --git a/src/main/java/com/power/doc/template/SpringBootDocBuildTemplate.java b/src/main/java/com/power/doc/template/SpringBootDocBuildTemplate.java index af112a31..3d92c3d6 100644 --- a/src/main/java/com/power/doc/template/SpringBootDocBuildTemplate.java +++ b/src/main/java/com/power/doc/template/SpringBootDocBuildTemplate.java @@ -46,7 +46,7 @@ import static com.power.doc.constants.DocGlobalConstants.FILE_CONTENT_TYPE; import static com.power.doc.constants.DocGlobalConstants.JSON_CONTENT_TYPE; -import static com.power.doc.constants.DocTags.IGNORE; +import static com.power.doc.constants.DocTags.*; /** * @author yu 2019/12/21. @@ -365,6 +365,19 @@ private ApiRequestExample buildReqJson(DocJavaMethod javaMethod, ApiMethodDoc ap } if (SpringMvcAnnotations.REQUEST_BODY.equals(annotationName) || DocGlobalConstants.REQUEST_BODY_FULLY.equals(annotationName)) { apiMethodDoc.setContentType(JSON_CONTENT_TYPE); + if (Objects.nonNull(configBuilder.getApiConfig().getRequestBodyAdvice()) + && Objects.isNull(method.getTagByName(IGNORE_REQUEST_BODY_ADVICE))) { + String requestBodyAdvice = configBuilder.getApiConfig().getRequestBodyAdvice().getClassName(); + gicTypeName = new StringBuffer() + .append(requestBodyAdvice) + .append("<") + .append(gicTypeName).append(">").toString(); + typeName = new StringBuffer() + .append(requestBodyAdvice) + .append("<") + .append(typeName).append(">").toString(); + } + if (JavaClassValidateUtil.isPrimitive(simpleTypeName)) { StringBuilder builder = new StringBuilder(); builder.append("{\"")