修改多泛型example显示问题

This commit is contained in:
xingzi 2020-07-02 20:07:54 +08:00
parent 63bf1320ba
commit 6081d9c2e4
2 changed files with 27 additions and 19 deletions

View File

@ -33,10 +33,7 @@ import com.power.doc.model.ApiReturn;
import com.power.doc.model.CustomRespField;
import com.power.doc.model.DocJavaField;
import com.power.doc.utils.*;
import com.thoughtworks.qdox.model.JavaAnnotation;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaField;
import com.thoughtworks.qdox.model.JavaMethod;
import com.thoughtworks.qdox.model.*;
import java.util.HashMap;
import java.util.HashSet;
@ -76,6 +73,12 @@ public class JsonBuildHelper {
*/
public static String buildJson(String typeName, String genericCanonicalName,
boolean isResp, int counter, Map<String, String> registryClasses, ProjectDocConfigBuilder builder) {
//存储泛型所对应的实体类
Map<String,String> genericMap = new HashMap<>(10);
JavaClass javaClass = builder.getJavaProjectBuilder().getClassByName(typeName);
ApiConfig apiConfig = builder.getApiConfig();
if (counter > apiConfig.getRecursionLimit()) {
@ -102,8 +105,18 @@ public class JsonBuildHelper {
boolean skipTransientField = apiConfig.isSkipTransientField();
StringBuilder data0 = new StringBuilder();
JavaClass cls = builder.getClassByName(typeName);
data0.append("{");
String[] globGicName = DocClassUtil.getSimpleGicName(genericCanonicalName);
//添加泛型对应关系
if(cls!=null && null != cls.getTypeParameters()){
List<JavaTypeVariable<JavaGenericDeclaration>> variables = cls.getTypeParameters();
for(int i=0;i<cls.getTypeParameters().size();i++){
genericMap.put(variables.get(i).getName(),globGicName[i]);
}
}
StringBuilder data = new StringBuilder();
if (JavaClassValidateUtil.isCollection(typeName) || JavaClassValidateUtil.isArray(typeName)) {
data.append("[");
@ -163,7 +176,6 @@ public class JsonBuildHelper {
boolean responseFieldToUnderline = builder.getApiConfig().isResponseFieldToUnderline();
List<DocJavaField> fields = JavaClassUtil.getFields(cls, 0, new HashSet<>());
boolean isGenerics = JavaFieldUtil.checkGenerics(fields);
int i = 0;
out:
for (DocJavaField docField : fields) {
JavaField field = docField.getJavaField();
@ -255,7 +267,7 @@ public class JsonBuildHelper {
data0.append("{\"object\":\"any object\"},");
continue out;
}
String gicName1 = (i < globGicName.length) ? globGicName[i] : globGicName[globGicName.length - 1];
String gicName1 = genericMap.get(gicName)==null ? globGicName[0] : genericMap.get(gicName);
if (DocGlobalConstants.JAVA_STRING_FULLY.equals(gicName1)) {
data0.append("[").append("\"").append(buildJson(gicName1, gicName1, isResp, nextLevel, registryClasses, builder)).append("\"]").append(",");
} else {
@ -283,7 +295,7 @@ public class JsonBuildHelper {
}
String gicName = fieldGicName.substring(fieldGicName.indexOf(",") + 1, fieldGicName.indexOf(">"));
if (gicName.length() == 1) {
String gicName1 = (i < globGicName.length) ? globGicName[i] : globGicName[globGicName.length - 1];
String gicName1 = genericMap.get(gicName)==null ? globGicName[0] : genericMap.get(gicName);
if (DocGlobalConstants.JAVA_STRING_FULLY.equals(gicName1)) {
data0.append("{").append("\"mapKey\":\"").append(buildJson(gicName1, gicName1, isResp, nextLevel, registryClasses, builder)).append("\"},");
} else {
@ -298,7 +310,7 @@ public class JsonBuildHelper {
}
} else if (subTypeName.length() == 1) {
if (!typeName.equals(genericCanonicalName)) {
String gicName = globGicName[i];
String gicName = genericMap.get(subTypeName)==null ? globGicName[0] : genericMap.get(subTypeName);
if (JavaClassValidateUtil.isPrimitive(gicName)) {
data0.append(DocUtil.jsonValueByType(gicName)).append(",");
} else {
@ -308,12 +320,11 @@ public class JsonBuildHelper {
} else {
data0.append("{\"waring\":\"You may have used non-display generics.\"},");
}
i++;
} else if (DocGlobalConstants.JAVA_OBJECT_FULLY.equals(subTypeName)) {
if (isGenerics) {
data0.append("{\"object\":\"any object\"},");
} else if (i < globGicName.length) {
String gicName = globGicName[i];
} else if ( globGicName.length > 0) {
String gicName = genericMap.get(subTypeName)==null ? globGicName[0] : genericMap.get(subTypeName);
if (!typeName.equals(genericCanonicalName)) {
if (JavaClassValidateUtil.isPrimitive(gicName)) {
data0.append("\"").append(buildJson(gicName, genericCanonicalName, isResp, nextLevel, registryClasses, builder)).append("\",");
@ -327,9 +338,6 @@ public class JsonBuildHelper {
} else {
data0.append("{\"waring\":\"You may have used non-display generics.\"},");
}
if (!isGenerics) {
i++;
}
} else if (typeName.equals(subTypeName)) {
data0.append("{\"$ref\":\"...\"}").append(",");
} else {

View File

@ -1,6 +1,7 @@
package com.power.doc;
import com.power.common.util.DateTimeUtil;
import com.power.doc.builder.HtmlApiDocBuilder;
import com.power.doc.builder.PostmanJsonBuilder;
import com.power.doc.enums.OrderEnum;
import com.power.doc.model.*;
@ -30,11 +31,12 @@ public class ApiDocTest {
//config.setStrict(true);
config.setAllInOne(true);
config.setOutPath("d:\\md2");
config.setOutPath("d:\\md3");
config.setMd5EncryptedHtmlName(true);
//不指定SourcePaths默认加载代码为项目src/main/java下的
config.setSourceCodePaths(
SourceCodePath.path().setDesc("本项目代码").setPath("C:\\Users\\xingzi\\Desktop\\smart\\api-doc-test\\src\\main\\java")
SourceCodePath.path().setDesc("本项目代码").setPath("C:\\Users\\xingzi\\Desktop\\smart\\api-doc-test\\src\\main\\java\\com\\power\\doc")
//SourcePath.path().setPath("F:\\Personal\\project\\smart\\src\\main\\java")
//SourcePath.path().setDesc("加载项目外代码").setPath("E:\\ApplicationPower\\ApplicationPower\\Common-util\\src\\main\\java")
);
@ -63,9 +65,7 @@ public class ApiDocTest {
long start = System.currentTimeMillis();
// ApiDocBuilder.builderControllersApi(config);
//HtmlApiDocBuilder.builderControllersApi(config);
PostmanJsonBuilder.buildPostmanCollection(config);
HtmlApiDocBuilder.buildApiDoc(config);
long end = System.currentTimeMillis();
DateTimeUtil.printRunTime(end, start);
}