From eaca75905513d67400e073c0009ad36d7e368623 Mon Sep 17 00:00:00 2001 From: oppofind <836575280@qq.com> Date: Thu, 28 Nov 2019 22:47:47 +0800 Subject: [PATCH] fix #5 bug on github --- RELEASE.md | 2 + .../com/power/doc/builder/SourceBuilder.java | 50 ++++++++++++++----- .../com/power/doc/utils/DocClassUtil.java | 13 +++-- 3 files changed, 48 insertions(+), 17 deletions(-) diff --git a/RELEASE.md b/RELEASE.md index c8f51d6..b94ffe6 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -134,5 +134,7 @@ 2. 修改使用mybatis-plus实体继承Model对象时将log字段输出到文档的问题。 3. 添加对transient修饰字段文档输出开关,默认不输出。 4. html文档添加项目名称显示 + 5. 修改github #4 泛型中Void类型解析死循环 + 6. 修改github #5 简单枚举参数解析空指针异常 \ No newline at end of file diff --git a/src/main/java/com/power/doc/builder/SourceBuilder.java b/src/main/java/com/power/doc/builder/SourceBuilder.java index afe6f4e..7a07d26 100644 --- a/src/main/java/com/power/doc/builder/SourceBuilder.java +++ b/src/main/java/com/power/doc/builder/SourceBuilder.java @@ -572,7 +572,7 @@ public class SourceBuilder { if (!isResp && javaClass.isEnum()) { List methods = javaClass.getMethods(); int index = 0; - String reTypeName = null; + String reTypeName = "string"; enumOut: for (JavaMethod method : methods) { JavaType type = method.getReturnType(); @@ -958,15 +958,7 @@ public class SourceBuilder { } else { JavaClass javaClass = builder.getClassByName(subTypeName); if (!isResp && javaClass.isEnum()) { - List javaFields = javaClass.getEnumConstants(); - Object value = null; - int index = 0; - for (JavaField javaField : javaFields) { - String simpleName = javaField.getType().getSimpleName(); - if (!DocClassUtil.isPrimitive(simpleName) && index < 1) { - value = javaField.getEnumConstantArguments().get(0); - } - } + Object value = this.handleEnumValue(javaClass,Boolean.FALSE); data0.append(value).append(","); } else { data0.append(buildJson(subTypeName, fieldGicName, responseFieldMap, isResp, counter + 1, registryClasses)).append(","); @@ -994,6 +986,7 @@ public class SourceBuilder { String simpleTypeName = javaType.getValue(); String gicTypeName = javaType.getGenericCanonicalName(); String typeName = javaType.getFullyQualifiedName(); + JavaClass javaClass = builder.getClassByName(typeName); String paraName = parameter.getName(); if (!DocClassUtil.isMvcIgnoreParams(typeName)) { //file upload @@ -1058,10 +1051,12 @@ public class SourceBuilder { return builder.toString(); } if (requestBodyCounter < 1 && paraName != null) { - if (annotations.size() < 1 && !DocClassUtil.isPrimitive(typeName)) { + if (javaClass.isEnum()) { + Object value = this.handleEnumValue(javaClass,Boolean.TRUE); + paramsMap.put(paraName, StringUtil.removeQuotes(String.valueOf(value))); + } else if (annotations.size() < 1 && !DocClassUtil.isPrimitive(typeName)) { return "Smart-doc can't support create form-data example,It is recommended to use @RequestBody to receive parameters."; - } - if (StringUtil.isEmpty(defaultVal)) { + } else if (StringUtil.isEmpty(defaultVal) && DocClassUtil.isPrimitive(typeName)) { paramsMap.put(paraName, DocUtil.getValByTypeAndFieldName(simpleTypeName, paraName, true)); } else { @@ -1104,6 +1099,7 @@ public class SourceBuilder { String typeName = parameter.getType().getGenericCanonicalName(); String simpleName = parameter.getType().getValue().toLowerCase(); String fullTypeName = parameter.getType().getFullyQualifiedName(); + JavaClass javaClass = builder.getClassByName(fullTypeName); if (!DocClassUtil.isMvcIgnoreParams(typeName)) { if (!paramTagMap.containsKey(paramName) && DocClassUtil.isPrimitive(fullTypeName) && isStrict) { throw new RuntimeException("ERROR: Unable to find javadoc @param for actual param \"" @@ -1146,6 +1142,10 @@ public class SourceBuilder { ApiParam param = ApiParam.of().setField(paramName) .setType("map").setDesc(comment).setRequired(true).setVersion(DocGlobalConstants.DEFAULT_VERSION); paramList.add(param); + } else if (javaClass.isEnum()) { + ApiParam param = ApiParam.of().setField(paramName) + .setType("string").setDesc(comment).setRequired(true).setVersion(DocGlobalConstants.DEFAULT_VERSION); + paramList.add(param); } else { paramList.addAll(buildParams(fullTypeName, "", 0, "true", responseFieldMap, false, new HashMap<>())); } @@ -1337,4 +1337,28 @@ public class SourceBuilder { apiDoc.setList(apiMethodDocs); apiDocList.add(apiDoc); } + + private Object handleEnumValue(JavaClass javaClass,boolean returnEnum) { + List javaFields = javaClass.getEnumConstants(); + Object value = null; + int index = 0; + for (JavaField javaField : javaFields) { + String simpleName = javaField.getType().getSimpleName(); + StringBuilder valueBuilder = new StringBuilder(); + valueBuilder.append("\"").append(javaField.getName()).append("\"").toString(); + if(returnEnum){ + value = valueBuilder.toString(); + return value; + } + if (!DocClassUtil.isPrimitive(simpleName) && index < 1) { + if (null != javaField.getEnumConstantArguments()) { + value = javaField.getEnumConstantArguments().get(0); + } else { + value = valueBuilder.toString(); + } + } + index++; + } + return value; + } } diff --git a/src/main/java/com/power/doc/utils/DocClassUtil.java b/src/main/java/com/power/doc/utils/DocClassUtil.java index 30a8e35..7ed0acf 100644 --- a/src/main/java/com/power/doc/utils/DocClassUtil.java +++ b/src/main/java/com/power/doc/utils/DocClassUtil.java @@ -1,5 +1,6 @@ package com.power.doc.utils; +import com.power.common.util.StringUtil; import com.power.doc.constants.DocGlobalConstants; import com.power.doc.model.ApiReturn; @@ -25,7 +26,7 @@ public class DocClassUtil { type = type.toLowerCase(); switch (type) { case "integer": - case "void": + case "void": case "int": case "long": case "double": @@ -168,6 +169,9 @@ public class DocClassUtil { * @return String */ public static String processTypeNameForParams(String javaTypeName) { + if (StringUtil.isEmpty(javaTypeName)) { + return "object"; + } if (javaTypeName.length() == 1) { return "object"; } @@ -388,11 +392,12 @@ public class DocClassUtil { } /** - * ignore field type name + * ignore field type name + * * @param typeName field type name * @return String */ - public static boolean isIgnoreFieldTypes(String typeName){ + public static boolean isIgnoreFieldTypes(String typeName) { switch (typeName) { case "org.slf4j.Logger": return true; @@ -424,7 +429,7 @@ public class DocClassUtil { fullyName.startsWith("java.util.concurrent.CompletableFuture") || fullyName.startsWith("org.springframework.web.context.request.async.DeferredResult") || fullyName.startsWith("org.springframework.web.context.request.async.WebAsyncTask") || - fullyName.startsWith("reactor.core.publisher.Mono")|| + fullyName.startsWith("reactor.core.publisher.Mono") || fullyName.startsWith("org.springframework.http.ResponseEntity")) { if (fullyName.contains("<")) { String[] strings = getSimpleGicName(fullyName);