This commit is contained in:
xingzi 2019-11-29 13:47:18 +08:00
commit b4dfb88190
3 changed files with 48 additions and 17 deletions

View File

@ -134,5 +134,7 @@
2. 修改使用mybatis-plus实体继承Model对象时将log字段输出到文档的问题。
3. 添加对transient修饰字段文档输出开关默认不输出。
4. html文档添加项目名称显示
5. 修改github #4 泛型中Void类型解析死循环
6. 修改github #5 简单枚举参数解析空指针异常

View File

@ -572,7 +572,7 @@ public class SourceBuilder {
if (!isResp && javaClass.isEnum()) {
List<JavaMethod> 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<JavaField> 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<JavaField> 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;
}
}

View File

@ -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);