Add recursion limit config

This commit is contained in:
oppofind 2020-06-16 23:23:23 +08:00
parent a8e092e26a
commit f058583d71
5 changed files with 49 additions and 11 deletions

View File

@ -26,6 +26,7 @@ import com.power.common.util.RandomUtil;
import com.power.common.util.StringUtil;
import com.power.doc.builder.ProjectDocConfigBuilder;
import com.power.doc.constants.DocGlobalConstants;
import com.power.doc.model.ApiConfig;
import com.power.doc.model.DocJavaField;
import com.power.doc.model.FormData;
import com.power.doc.utils.DocClassUtil;
@ -59,18 +60,23 @@ public class FormDataBuildHelper {
if (StringUtil.isEmpty(className)) {
throw new RuntimeException("Class name can't be null or empty.");
}
// Check circular reference
ApiConfig apiConfig = builder.getApiConfig();
List<FormData> formDataList = new ArrayList<>();
if (counter > apiConfig.getRecursionLimit()) {
return formDataList;
}
// Check circular reference
if (registryClasses.containsKey(className) && counter > registryClasses.size()) {
return formDataList;
}
// Registry class
registryClasses.put(className, className);
counter++;
boolean skipTransientField = apiConfig.isSkipTransientField();
String simpleName = DocClassUtil.getSimpleName(className);
String[] globGicName = DocClassUtil.getSimpleGicName(className);
JavaClass cls = builder.getJavaProjectBuilder().getClassByName(simpleName);
List<DocJavaField> fields = JavaClassUtil.getFields(cls, 0,new HashSet<>());
List<DocJavaField> fields = JavaClassUtil.getFields(cls, 0, new HashSet<>());
if (JavaClassValidateUtil.isPrimitive(simpleName)) {
FormData formData = new FormData();
@ -99,6 +105,9 @@ public class FormDataBuildHelper {
JavaClassValidateUtil.isIgnoreFieldTypes(subTypeName)) {
continue;
}
if (field.isTransient() && skipTransientField) {
continue;
}
String typeSimpleName = field.getType().getSimpleName();
if (JavaClassValidateUtil.isMap(subTypeName)) {
continue;

View File

@ -28,6 +28,7 @@ import com.power.doc.builder.ProjectDocConfigBuilder;
import com.power.doc.constants.DocAnnotationConstants;
import com.power.doc.constants.DocGlobalConstants;
import com.power.doc.constants.DocTags;
import com.power.doc.model.ApiConfig;
import com.power.doc.model.ApiReturn;
import com.power.doc.model.CustomRespField;
import com.power.doc.model.DocJavaField;
@ -76,6 +77,10 @@ public class JsonBuildHelper {
public static String buildJson(String typeName, String genericCanonicalName,
boolean isResp, int counter, Map<String, String> registryClasses, ProjectDocConfigBuilder builder) {
JavaClass javaClass = builder.getJavaProjectBuilder().getClassByName(typeName);
ApiConfig apiConfig = builder.getApiConfig();
if (counter > apiConfig.getRecursionLimit()) {
return "{\"$ref\":\"...\"}";
}
if (registryClasses.containsKey(typeName) && counter > registryClasses.size()) {
return "{\"$ref\":\"...\"}";
}
@ -94,6 +99,7 @@ public class JsonBuildHelper {
if (javaClass.isEnum()) {
return String.valueOf(JavaClassUtil.getEnumValue(javaClass, Boolean.FALSE));
}
boolean skipTransientField = apiConfig.isSkipTransientField();
StringBuilder data0 = new StringBuilder();
JavaClass cls = builder.getClassByName(typeName);
data0.append("{");
@ -155,7 +161,7 @@ public class JsonBuildHelper {
} else {
boolean requestFieldToUnderline = builder.getApiConfig().isRequestFieldToUnderline();
boolean responseFieldToUnderline = builder.getApiConfig().isResponseFieldToUnderline();
List<DocJavaField> fields = JavaClassUtil.getFields(cls, 0,new HashSet<>());
List<DocJavaField> fields = JavaClassUtil.getFields(cls, 0, new HashSet<>());
boolean isGenerics = JavaFieldUtil.checkGenerics(fields);
int i = 0;
out:
@ -167,6 +173,9 @@ public class JsonBuildHelper {
JavaClassValidateUtil.isIgnoreFieldTypes(subTypeName)) {
continue;
}
if (field.isTransient() && skipTransientField) {
continue;
}
if ((responseFieldToUnderline && isResp) || (requestFieldToUnderline && !isResp)) {
fieldName = StringUtil.camelToUnderline(fieldName);
}

View File

@ -30,10 +30,7 @@ import com.power.doc.constants.DocAnnotationConstants;
import com.power.doc.constants.DocGlobalConstants;
import com.power.doc.constants.DocTags;
import com.power.doc.constants.ValidatorAnnotations;
import com.power.doc.model.ApiDataDictionary;
import com.power.doc.model.ApiParam;
import com.power.doc.model.CustomRespField;
import com.power.doc.model.DocJavaField;
import com.power.doc.model.*;
import com.power.doc.utils.*;
import com.thoughtworks.qdox.model.*;
@ -57,12 +54,17 @@ public class ParamsBuildHelper {
if (StringUtil.isEmpty(className)) {
throw new RuntimeException("Class name can't be null or empty.");
}
ApiConfig apiConfig = projectBuilder.getApiConfig();
int nextLevel = level + 1;
// Check circular reference
List<ApiParam> paramList = new ArrayList<>();
if (level > apiConfig.getRecursionLimit()) {
return paramList;
}
if (registryClasses.containsKey(className) && level > registryClasses.size()) {
return paramList;
}
boolean skipTransientField = apiConfig.isSkipTransientField();
boolean isShowJavaType = projectBuilder.getApiConfig().getShowJavaType();
boolean requestFieldToUnderline = projectBuilder.getApiConfig().isRequestFieldToUnderline();
boolean responseFieldToUnderline = projectBuilder.getApiConfig().isResponseFieldToUnderline();
@ -71,7 +73,7 @@ public class ParamsBuildHelper {
String simpleName = DocClassUtil.getSimpleName(className);
String[] globGicName = DocClassUtil.getSimpleGicName(className);
JavaClass cls = projectBuilder.getClassByName(simpleName);
List<DocJavaField> fields = JavaClassUtil.getFields(cls, 0,new HashSet<>());
List<DocJavaField> fields = JavaClassUtil.getFields(cls, 0, new HashSet<>());
int n = 0;
if (JavaClassValidateUtil.isPrimitive(simpleName)) {
String processedType = isShowJavaType ? simpleName : DocClassUtil.processTypeNameForParams(simpleName.toLowerCase());
@ -107,6 +109,9 @@ public class ParamsBuildHelper {
JavaClassValidateUtil.isIgnoreFieldTypes(subTypeName)) {
continue;
}
if (field.isTransient() && skipTransientField) {
continue;
}
if ((responseFieldToUnderline && isResp) || (requestFieldToUnderline && !isResp)) {
fieldName = StringUtil.camelToUnderline(fieldName);
}
@ -348,7 +353,7 @@ public class ParamsBuildHelper {
public static String dictionaryListComment(ApiDataDictionary dictionary) {
List<EnumDictionary> enumDataDict = dictionary.getEnumDataDict();
return enumDataDict.stream().map(apiDataDictionary ->
apiDataDictionary.getValue() + ":" + apiDataDictionary.getDesc()
apiDataDictionary.getValue() + ":" + apiDataDictionary.getDesc()
).collect(Collectors.joining(","));
}

View File

@ -193,6 +193,12 @@ public class ApiConfig {
*/
private String rpcConsumerConfig;
/**
* recursion limit
* @since 1.8.8
*/
private int recursionLimit = 7;
public String getServerUrl() {
return serverUrl;
@ -419,4 +425,12 @@ public class ApiConfig {
public void setInlineEnum(Boolean inlineEnum) {
this.inlineEnum = inlineEnum;
}
public int getRecursionLimit() {
return recursionLimit;
}
public void setRecursionLimit(int recursionLimit) {
this.recursionLimit = recursionLimit;
}
}

View File

@ -71,11 +71,12 @@ public class JavaClassUtil {
List<JavaMethod> methods = cls1.getMethods();
for (JavaMethod javaMethod : methods) {
String methodName = javaMethod.getName();
int paramSize = javaMethod.getParameters().size();
boolean enable = false;
if (methodName.startsWith("get") && !"get".equals(methodName)) {
if (methodName.startsWith("get") && !"get".equals(methodName) && paramSize == 0) {
methodName = StringUtil.firstToLowerCase(methodName.substring(3, methodName.length()));
enable = true;
} else if (methodName.startsWith("is") && !"is".equals(methodName)) {
} else if (methodName.startsWith("is") && !"is".equals(methodName) && paramSize == 0) {
methodName = StringUtil.firstToLowerCase(methodName.substring(2, methodName.length()));
enable = true;
}