support enum

This commit is contained in:
oppofind 2019-11-10 16:12:26 +08:00
parent d751824437
commit 620b68e966
2 changed files with 68 additions and 8 deletions

View File

@ -209,15 +209,15 @@ public class DocBuilderTemplate {
} else { } else {
dataDict.setType("int32"); dataDict.setType("int32");
} }
dataDict.setDesc(desc.toString()); dataDict.setDesc(String.valueOf(desc));
dataDict.setValue(val.toString()); dataDict.setValue(String.valueOf(val));
dataDictList.add(dataDict); dataDictList.add(dataDict);
} }
apiDocDict.setDataDictList(dataDictList); apiDocDict.setDataDictList(dataDictList);
apiDocDictList.add(apiDocDict); apiDocDictList.add(apiDocDict);
} }
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | ClassNotFoundException e) { } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | ClassNotFoundException e) {
e.fillInStackTrace(); e.printStackTrace();
} }
return apiDocDictList; return apiDocDictList;
} }

View File

@ -446,7 +446,6 @@ public class SourceBuilder {
param.setDesc(DocGlobalConstants.ANY_OBJECT_MSG).setVersion(DocGlobalConstants.DEFAULT_VERSION); param.setDesc(DocGlobalConstants.ANY_OBJECT_MSG).setVersion(DocGlobalConstants.DEFAULT_VERSION);
} else { } else {
param.setDesc(DocGlobalConstants.ANY_OBJECT_MSG).setRequired(false).setVersion(DocGlobalConstants.DEFAULT_VERSION); param.setDesc(DocGlobalConstants.ANY_OBJECT_MSG).setRequired(false).setVersion(DocGlobalConstants.DEFAULT_VERSION);
} }
paramList.add(param); paramList.add(param);
} else { } else {
@ -523,8 +522,8 @@ public class SourceBuilder {
comment = field.getComment(); comment = field.getComment();
} }
if (StringUtil.isNotEmpty(comment)) { if (StringUtil.isNotEmpty(comment)) {
comment = comment.replace("\r\n", "<br>"); comment = comment.replaceAll("\r\n", "<br>");
comment = comment.replace("\n", "<br>"); comment = comment.replaceAll("\n", "<br>");
} }
if (DocClassUtil.isPrimitive(subTypeName)) { if (DocClassUtil.isPrimitive(subTypeName)) {
ApiParam param = ApiParam.of().setField(pre + fieldName); ApiParam param = ApiParam.of().setField(pre + fieldName);
@ -537,8 +536,36 @@ public class SourceBuilder {
} }
} else { } else {
ApiParam param = ApiParam.of().setField(pre + fieldName); ApiParam param = ApiParam.of().setField(pre + fieldName);
JavaClass javaClass = builder.getClassByName(subTypeName);
String enumComments = javaClass.getComment();
if(StringUtil.isNotEmpty(enumComments)){
enumComments = enumComments.replaceAll("\r\n", "<br>");
enumComments = enumComments.replaceAll("\r\n", "<br>");
comment = comment +"(See: "+ enumComments+")";
}
String processedType = DocClassUtil.processTypeNameForParams(typeSimpleName.toLowerCase()); String processedType = DocClassUtil.processTypeNameForParams(typeSimpleName.toLowerCase());
param.setType(processedType); param.setType(processedType);
if (!isResp && javaClass.isEnum()) {
List<JavaMethod> methods = javaClass.getMethods();
int index = 0;
String reTypeName = "object";
enumOut:for (JavaMethod method : methods) {
JavaType type = method.getReturnType();
reTypeName = type.getCanonicalName();
List<JavaAnnotation> javaAnnotationList = method.getAnnotations();
for (JavaAnnotation annotation : javaAnnotationList) {
if (annotation.getType().getSimpleName().contains("JsonValue")) {
break enumOut;
}
}
if (CollectionUtil.isEmpty(javaAnnotations) && index < 1) {
break enumOut;
}
index++;
}
param.setType(DocClassUtil.processTypeNameForParams(reTypeName));
}
if (StringUtil.isNotEmpty(comment)) { if (StringUtil.isNotEmpty(comment)) {
commonHandleParam(paramList, param, isRequired, comment, since, strRequired); commonHandleParam(paramList, param, isRequired, comment, since, strRequired);
} else { } else {
@ -629,7 +656,9 @@ public class SourceBuilder {
} else if (simpleName.equals(subTypeName)) { } else if (simpleName.equals(subTypeName)) {
//do nothing //do nothing
} else { } else {
paramList.addAll(buildParams(fieldGicName, preBuilder.toString(), i + 1, isRequired, responseFieldMap, isResp, registryClasses)); if (isResp && !javaClass.isEnum()) {
paramList.addAll(buildParams(fieldGicName, preBuilder.toString(), i + 1, isRequired, responseFieldMap, isResp, registryClasses));
}
} }
} }
} }
@ -899,7 +928,38 @@ public class SourceBuilder {
} else if (typeName.equals(subTypeName)) { } else if (typeName.equals(subTypeName)) {
data0.append("{\"$ref\":\"...\"}").append(","); data0.append("{\"$ref\":\"...\"}").append(",");
} else { } else {
data0.append(buildJson(subTypeName, fieldGicName, responseFieldMap, isResp, counter + 1, registryClasses)).append(","); JavaClass javaClass = builder.getClassByName(subTypeName);
if (!isResp && javaClass.isEnum()) {
List<JavaMethod> methods = javaClass.getMethods();
int index = 0;
enumOut:
for (JavaMethod method : methods) {
JavaType type = method.getReturnType();
List<JavaAnnotation> javaAnnotations = method.getAnnotations();
for (JavaAnnotation annotation : javaAnnotations) {
if (annotation.getType().getSimpleName().contains("JsonValue")) {
break enumOut;
}
}
if (CollectionUtil.isEmpty(javaAnnotations) && index < 1) {
break enumOut;
}
index++;
}
List<JavaField> javaFields = javaClass.getEnumConstants();
Object value = null;
index = 0;
for (JavaField javaField : javaFields) {
String simpleName = javaField.getType().getSimpleName();
if (!DocClassUtil.isPrimitive(simpleName) && index < 1) {
value = javaField.getEnumConstantArguments().get(0);
}
}
data0.append(value).append(",");
} else {
data0.append(buildJson(subTypeName, fieldGicName, responseFieldMap, isResp, counter + 1, registryClasses)).append(",");
}
} }
} }
} }