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 {
ApiParam param = ApiParam.of().setField(pre + fieldName);
JavaClass javaClass = projectBuilder.getClassByName(subTypeName);
JavaClass javaClass = projectBuilder.getJavaProjectBuilder().getClassByName(subTypeName);
String enumComments = javaClass.getComment();
if (StringUtil.isNotEmpty(enumComments) && javaClass.isEnum()) {
enumComments = DocUtil.replaceNewLineToHtmlBr(enumComments);

View File

@ -125,17 +125,16 @@ public class DocUtil {
String type = typeName.contains("java.lang") ? typeName.substring(typeName.lastIndexOf(".") + 1, typeName.length()) : typeName;
String key = filedName.toLowerCase() + "-" + type.toLowerCase();
StringBuilder value = null;
if(! type.contains("[")){
if (!type.contains("[")) {
isArray = false;
}
for (Map.Entry<String, String> entry : fieldValue.entrySet()) {
if (key.contains(entry.getKey())) {
value = new StringBuilder(entry.getValue());
if(! isArray){
if (!isArray) {
break;
}
else {
for(int i=0;i<2;i++){
} else {
for (int i = 0; i < 2; i++) {
value.append(",").append(entry.getValue());
}
break;
@ -292,7 +291,7 @@ public class DocUtil {
Map<String, String> paramTagMap = new HashMap<>();
for (DocletTag docletTag : paramTags) {
String value = docletTag.getValue();
if (StringUtil.isEmpty(value)&&StringUtil.isNotEmpty(className)) {
if (StringUtil.isEmpty(value) && StringUtil.isNotEmpty(className)) {
throw new RuntimeException("ERROR: #" + javaMethod.getName()
+ "() - bad @" + tagName + " javadoc from " + className + ", must be add comment if you use it.");
}
@ -327,6 +326,7 @@ public class DocUtil {
/**
* Get field tags
*
* @param field JavaField
* @return map
*/
@ -383,13 +383,22 @@ public class DocUtil {
return null;
}
public static String handleJsonStr(String content){
public static String handleJsonStr(String content) {
StringBuilder builder = new StringBuilder();
builder.append("\"").append(content).append("\"");
return builder.toString();
}
public static Map<String,String> formDataToMap(List<FormData> formDataList){
return formDataList.stream().collect(Collectors.toMap(FormData::getKey,FormData::getValue));
public static Map<String, String> formDataToMap(List<FormData> formDataList) {
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;
import com.power.common.util.StringUtil;
import com.power.doc.constants.DocTags;
import com.power.common.util.RandomUtil;
import com.power.doc.builder.ProjectDocConfigBuilder;
import com.power.doc.model.postman.request.body.FormData;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaField;
import java.util.ArrayList;
import java.util.List;
@ -15,7 +15,6 @@ import java.util.List;
public class JavaFieldUtil {
/**
*
* @param fields
* @return
*/
@ -29,26 +28,81 @@ public class JavaFieldUtil {
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<>();
for(JavaField field:fields){
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) {
String fieldName = field.getName();
String subTypeName = field.getType().getFullyQualifiedName();
String fieldGicName = field.getType().getGenericCanonicalName();
JavaClass javaClass = builder.getJavaProjectBuilder().getClassByName(subTypeName);
if ("this$0".equals(fieldName) ||
"serialVersionUID".equals(fieldName) ||
DocClassUtil.isIgnoreFieldTypes(subTypeName)) {
continue;
}
String typeSimpleName = field.getType().getSimpleName();
if (DocClassUtil.isPrimitive(subTypeName)){
if(DocClassUtil.isMap(subTypeName)){
continue;
}
if (DocClassUtil.isPrimitive(subTypeName)) {
String fieldValue = DocUtil.getValByTypeAndFieldName(typeSimpleName, field.getName());
FormData formData = new FormData();
formData.setKey(fieldName);
formData.setKey(pre+fieldName);
formData.setType("text");
formData.setValue(fieldValue);
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 {
continue;
formDataList.addAll(getFormData(gName,builder, pre+fieldName+"[0]."));
}
}
}
} else {
formDataList.addAll(getFormData(fieldGicName,builder, pre+fieldName+"."));
}
}
return formDataList;