修复当@pathvariable为数组时空指针异常bug

This commit is contained in:
xingzi 2019-12-11 19:57:20 +08:00
parent 9072447454
commit 70f788f980
3 changed files with 54 additions and 6 deletions

View File

@ -1062,13 +1062,18 @@ public class SourceBuilder {
} else if (StringUtil.isEmpty(defaultVal) && DocClassUtil.isPrimitive(typeName)) {
paramsMap.put(paraName, DocUtil.getValByTypeAndFieldName(simpleTypeName, paraName,
true));
} else {
} else if((StringUtil.isEmpty(defaultVal) && DocClassUtil.isPrimitiveArray(typeName))){
paramsMap.put(paraName, DocUtil.getValByTypeAndFieldName(simpleTypeName, paraName,
true));
}
else {
paramsMap.put(paraName, defaultVal);
}
}
}
}
String url;
if (containsBrace) {
url = DocUtil.formatAndRemove(apiMethodDoc.getUrl(), paramsMap);
url = UrlUtil.urlJoin(url, paramsMap);

View File

@ -51,6 +51,35 @@ public class DocClassUtil {
}
}
/**
* Check if it is the basic data array type of json data
*
* @param type0 java class name
* @return boolean
*/
public static boolean isPrimitiveArray(String type0) {
String type = type0.contains("java.lang") ? type0.substring(type0.lastIndexOf(".") + 1, type0.length()) : type0;
type = type.toLowerCase();
switch (type) {
case "integer[]":
case "void":
case "int[]":
case "long[]":
case "double[]":
case "float[]":
case "short[]":
case "bigdecimal[]":
case "char[]":
case "string[]":
case "boolean[]":
case "byte[]":
return true;
default:
return false;
}
}
/**
* get class names by generic class name
*

View File

@ -23,7 +23,7 @@ import java.util.*;
public class DocUtil {
private static Faker faker = new Faker(new Locale(System.getProperty(DocGlobalConstants.DOC_LANGUAGE)));
//private static Faker faker = new Faker(new Locale("smart-doc_language"));
private static Faker enFaker = new Faker(new Locale("en-US"));
private static Map<String, String> fieldValue = new LinkedHashMap<>();
@ -117,13 +117,27 @@ public class DocUtil {
* @return random value
*/
public static String getValByTypeAndFieldName(String typeName, String filedName) {
boolean isArray = true;
String type = typeName.contains("java.lang") ? typeName.substring(typeName.lastIndexOf(".") + 1, typeName.length()) : typeName;
String key = filedName.toLowerCase() + "-" + type.toLowerCase();
String value = null;
StringBuilder value = null;
if(! type.contains("[")){
isArray = false;
}
for (Map.Entry<String, String> entry : fieldValue.entrySet()) {
if (key.contains(entry.getKey())) {
value = entry.getValue();
break;
value = new StringBuilder(entry.getValue());
if(! isArray){
break;
}
else {
for(int i=0;i<2;i++){
value.append(",").append(entry.getValue());
}
break;
}
}
}
if (null == value) {
@ -134,7 +148,7 @@ public class DocUtil {
builder.append("\"").append(value).append("\"");
return builder.toString();
} else {
return value;
return value.toString();
}
}
}