fix ParamBuilder null point

This commit is contained in:
oppofind 2019-12-24 00:12:18 +08:00
parent ce00413141
commit d046deda9e
3 changed files with 85 additions and 22 deletions

View File

@ -149,7 +149,7 @@ public class ParamsBuildHelper {
} }
} else { } else {
ApiParam param = ApiParam.of().setField(pre + fieldName); ApiParam param = ApiParam.of().setField(pre + fieldName);
JavaClass javaClass = projectBuilder.getClassByName(subTypeName); JavaClass javaClass = projectBuilder.getJavaProjectBuilder().getClassByName(subTypeName);
String enumComments = javaClass.getComment(); String enumComments = javaClass.getComment();
if (StringUtil.isNotEmpty(enumComments) && javaClass.isEnum()) { if (StringUtil.isNotEmpty(enumComments) && javaClass.isEnum()) {
enumComments = DocUtil.replaceNewLineToHtmlBr(enumComments); enumComments = DocUtil.replaceNewLineToHtmlBr(enumComments);

View File

@ -133,8 +133,7 @@ public class DocUtil {
value = new StringBuilder(entry.getValue()); value = new StringBuilder(entry.getValue());
if (!isArray) { if (!isArray) {
break; break;
} } else {
else {
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
value.append(",").append(entry.getValue()); value.append(",").append(entry.getValue());
} }
@ -327,6 +326,7 @@ public class DocUtil {
/** /**
* Get field tags * Get field tags
*
* @param field JavaField * @param field JavaField
* @return map * @return map
*/ */
@ -390,6 +390,15 @@ public class DocUtil {
} }
public static Map<String, String> formDataToMap(List<FormData> formDataList) { public static Map<String, String> formDataToMap(List<FormData> formDataList) {
return formDataList.stream().collect(Collectors.toMap(FormData::getKey,FormData::getValue)); Map<String, String> map = formDataList.stream()
.collect(Collectors.toMap(
FormData::getKey,
FormData::getValue,
(u, v) -> {
throw new IllegalStateException(String.format("Duplicate key %s", u));
},
LinkedHashMap::new
));
return map;
} }
} }

View File

@ -1,11 +1,11 @@
package com.power.doc.utils; package com.power.doc.utils;
import com.power.common.util.StringUtil; import com.power.common.util.RandomUtil;
import com.power.doc.constants.DocTags; import com.power.doc.builder.ProjectDocConfigBuilder;
import com.power.doc.model.postman.request.body.FormData; import com.power.doc.model.postman.request.body.FormData;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaField; import com.thoughtworks.qdox.model.JavaField;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -15,7 +15,6 @@ import java.util.List;
public class JavaFieldUtil { public class JavaFieldUtil {
/** /**
*
* @param fields * @param fields
* @return * @return
*/ */
@ -29,26 +28,81 @@ public class JavaFieldUtil {
return false; return false;
} }
public static List<FormData> getFormData(List<JavaField> fields){ public static List<FormData> getFormData(String className, ProjectDocConfigBuilder builder, String pre) {
String simpleName = DocClassUtil.getSimpleName(className);
String[] globGicName = DocClassUtil.getSimpleGicName(className);
JavaClass cls = builder.getJavaProjectBuilder().getClassByName(simpleName);
List<JavaField> fields = JavaClassUtil.getFields(cls, 0);
List<FormData> formDataList = new ArrayList<>(); List<FormData> formDataList = new ArrayList<>();
if(DocClassUtil.isPrimitive(simpleName)){
FormData formData = new FormData();
formData.setKey(pre);
formData.setType("text");
formData.setValue(RandomUtil.randomValueByType(className));
formDataList.add(formData);
return formDataList;
}
if (DocClassUtil.isCollection(simpleName) || DocClassUtil.isArray(simpleName)) {
String gicName = globGicName[0];
if (DocClassUtil.isArray(gicName)) {
gicName = gicName.substring(0, gicName.indexOf("["));
}
formDataList.addAll(getFormData(gicName, builder, pre+"[]"));
}
int n = 0;
out:
for (JavaField field : fields) { for (JavaField field : fields) {
String fieldName = field.getName(); String fieldName = field.getName();
String subTypeName = field.getType().getFullyQualifiedName(); String subTypeName = field.getType().getFullyQualifiedName();
String fieldGicName = field.getType().getGenericCanonicalName();
JavaClass javaClass = builder.getJavaProjectBuilder().getClassByName(subTypeName);
if ("this$0".equals(fieldName) || if ("this$0".equals(fieldName) ||
"serialVersionUID".equals(fieldName) || "serialVersionUID".equals(fieldName) ||
DocClassUtil.isIgnoreFieldTypes(subTypeName)) { DocClassUtil.isIgnoreFieldTypes(subTypeName)) {
continue; continue;
} }
String typeSimpleName = field.getType().getSimpleName(); String typeSimpleName = field.getType().getSimpleName();
if(DocClassUtil.isMap(subTypeName)){
continue;
}
if (DocClassUtil.isPrimitive(subTypeName)) { if (DocClassUtil.isPrimitive(subTypeName)) {
String fieldValue = DocUtil.getValByTypeAndFieldName(typeSimpleName, field.getName()); String fieldValue = DocUtil.getValByTypeAndFieldName(typeSimpleName, field.getName());
FormData formData = new FormData(); FormData formData = new FormData();
formData.setKey(fieldName); formData.setKey(pre+fieldName);
formData.setType("text"); formData.setType("text");
formData.setValue(fieldValue); formData.setValue(fieldValue);
formDataList.add(formData); formDataList.add(formData);
} else if (javaClass.isEnum()) {
Object value = JavaClassUtil.getEnumValue(javaClass, Boolean.FALSE);
FormData formData = new FormData();
formData.setKey(pre+fieldName);
formData.setType("text");
formData.setValue(String.valueOf(value));
formDataList.add(formData);
} else if (DocClassUtil.isCollection(subTypeName)) {
String gNameTemp = field.getType().getGenericCanonicalName();
String[] gNameArr = DocClassUtil.getSimpleGicName(gNameTemp);
if (gNameArr.length == 0) {
continue out;
}
String gName = DocClassUtil.getSimpleGicName(gNameTemp)[0];
if (!DocClassUtil.isPrimitive(gName)) {
if (!simpleName.equals(gName) && !gName.equals(simpleName)) {
if (gName.length() == 1) {
int len = globGicName.length;
if (len > 0) {
String gicName = (n < len) ? globGicName[n] : globGicName[len - 1];
if (!DocClassUtil.isPrimitive(gicName) && !simpleName.equals(gicName)) {
formDataList.addAll(getFormData(gicName, builder,pre+fieldName+"[0]."));
}
}
} else { } else {
continue; formDataList.addAll(getFormData(gName,builder, pre+fieldName+"[0]."));
}
}
}
} else {
formDataList.addAll(getFormData(fieldGicName,builder, pre+fieldName+"."));
} }
} }
return formDataList; return formDataList;