support validate group
This commit is contained in:
parent
6eaf6bf09c
commit
e7c83e9c1f
|
@ -33,7 +33,6 @@ import java.util.List;
|
|||
*/
|
||||
public enum DocValidatorAnnotationEnum {
|
||||
|
||||
|
||||
NOT_EMPTY("NotEmpty"),
|
||||
|
||||
NOT_BLANK("NotBlank"),
|
||||
|
@ -68,7 +67,10 @@ public enum DocValidatorAnnotationEnum {
|
|||
|
||||
LENGTH("Length"),
|
||||
|
||||
RANGE("Range");
|
||||
RANGE("Range"),
|
||||
|
||||
VALIDATED("Validated")
|
||||
;
|
||||
|
||||
private String value;
|
||||
|
||||
|
|
|
@ -28,4 +28,6 @@ package com.power.doc.constants;
|
|||
public interface ValidatorAnnotations {
|
||||
|
||||
String VALID = "Valid";
|
||||
|
||||
String NULL = "Null";
|
||||
}
|
||||
|
|
|
@ -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.constants.ValidatorAnnotations;
|
||||
import com.power.doc.model.ApiParam;
|
||||
import com.power.doc.model.CustomRespField;
|
||||
import com.power.doc.utils.*;
|
||||
|
@ -46,7 +47,8 @@ public class ParamsBuildHelper {
|
|||
|
||||
public static List<ApiParam> buildParams(String className, String pre, int i, String isRequired,
|
||||
Map<String, CustomRespField> responseFieldMap, boolean isResp,
|
||||
Map<String, String> registryClasses, ProjectDocConfigBuilder projectBuilder) {
|
||||
Map<String, String> registryClasses, ProjectDocConfigBuilder projectBuilder,
|
||||
List<String> groupClasses) {
|
||||
if (StringUtil.isEmpty(className)) {
|
||||
throw new RuntimeException("Class name can't be null or empty.");
|
||||
}
|
||||
|
@ -70,11 +72,11 @@ public class ParamsBuildHelper {
|
|||
if (JavaClassValidateUtil.isArray(gicName)) {
|
||||
gicName = gicName.substring(0, gicName.indexOf("["));
|
||||
}
|
||||
paramList.addAll(buildParams(gicName, pre, i + 1, isRequired, responseFieldMap, isResp, registryClasses, projectBuilder));
|
||||
paramList.addAll(buildParams(gicName, pre, i + 1, isRequired, responseFieldMap, isResp, registryClasses, projectBuilder, groupClasses));
|
||||
}
|
||||
} else if (JavaClassValidateUtil.isMap(simpleName)) {
|
||||
if (globGicName.length == 2) {
|
||||
paramList.addAll(buildParams(globGicName[1], pre, i + 1, isRequired, responseFieldMap, isResp, registryClasses, projectBuilder));
|
||||
paramList.addAll(buildParams(globGicName[1], pre, i + 1, isRequired, responseFieldMap, isResp, registryClasses, projectBuilder, groupClasses));
|
||||
}
|
||||
} else if (DocGlobalConstants.JAVA_OBJECT_FULLY.equals(className)) {
|
||||
ApiParam param = ApiParam.of().setField(pre + "any object").setType("object");
|
||||
|
@ -115,6 +117,7 @@ public class ParamsBuildHelper {
|
|||
|
||||
boolean strRequired = false;
|
||||
int annotationCounter = 0;
|
||||
|
||||
an:
|
||||
for (JavaAnnotation annotation : javaAnnotations) {
|
||||
String annotationName = annotation.getType().getSimpleName();
|
||||
|
@ -132,9 +135,27 @@ public class ParamsBuildHelper {
|
|||
if (null != annotation.getProperty(DocAnnotationConstants.VALUE_PROP)) {
|
||||
fieldName = StringUtil.removeQuotes(annotation.getProperty(DocAnnotationConstants.VALUE_PROP).toString());
|
||||
}
|
||||
} else if (ValidatorAnnotations.NULL.equals(annotationName)) {
|
||||
List<String> groupClassList = JavaClassUtil.getParamGroupJavaClass(annotation);
|
||||
for (String javaClass : groupClassList) {
|
||||
if (groupClasses.contains(javaClass)) {
|
||||
continue out;
|
||||
}
|
||||
}
|
||||
} else if (JavaClassValidateUtil.isJSR303Required(annotationName)) {
|
||||
strRequired = true;
|
||||
annotationCounter++;
|
||||
boolean hasGroup = false;
|
||||
List<String> groupClassList = JavaClassUtil.getParamGroupJavaClass(annotation);
|
||||
for (String javaClass : groupClassList) {
|
||||
if (groupClasses.contains(javaClass)) {
|
||||
hasGroup = true;
|
||||
}
|
||||
}
|
||||
if (hasGroup) {
|
||||
strRequired = true;
|
||||
} else if (CollectionUtil.isEmpty(groupClassList) && CollectionUtil.isEmpty(groupClasses)) {
|
||||
strRequired = true;
|
||||
}
|
||||
break an;
|
||||
}
|
||||
}
|
||||
|
@ -220,10 +241,10 @@ public class ParamsBuildHelper {
|
|||
if (valType.length() == 1) {
|
||||
String gicName = (n < globGicName.length) ? globGicName[n] : globGicName[globGicName.length - 1];
|
||||
if (!JavaClassValidateUtil.isPrimitive(gicName) && !simpleName.equals(gicName)) {
|
||||
paramList.addAll(buildParams(gicName, preBuilder.toString(), i + 1, isRequired, responseFieldMap, isResp, registryClasses, projectBuilder));
|
||||
paramList.addAll(buildParams(gicName, preBuilder.toString(), i + 1, isRequired, responseFieldMap, isResp, registryClasses, projectBuilder, groupClasses));
|
||||
}
|
||||
} else {
|
||||
paramList.addAll(buildParams(valType, preBuilder.toString(), i + 1, isRequired, responseFieldMap, isResp, registryClasses, projectBuilder));
|
||||
paramList.addAll(buildParams(valType, preBuilder.toString(), i + 1, isRequired, responseFieldMap, isResp, registryClasses, projectBuilder, groupClasses));
|
||||
}
|
||||
}
|
||||
} else if (JavaClassValidateUtil.isCollection(subTypeName)) {
|
||||
|
@ -243,11 +264,11 @@ public class ParamsBuildHelper {
|
|||
if (len > 0) {
|
||||
String gicName = (n < len) ? globGicName[n] : globGicName[len - 1];
|
||||
if (!JavaClassValidateUtil.isPrimitive(gicName) && !simpleName.equals(gicName)) {
|
||||
paramList.addAll(buildParams(gicName, preBuilder.toString(), i + 1, isRequired, responseFieldMap, isResp, registryClasses, projectBuilder));
|
||||
paramList.addAll(buildParams(gicName, preBuilder.toString(), i + 1, isRequired, responseFieldMap, isResp, registryClasses, projectBuilder, groupClasses));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
paramList.addAll(buildParams(gName, preBuilder.toString(), i + 1, isRequired, responseFieldMap, isResp, registryClasses, projectBuilder));
|
||||
paramList.addAll(buildParams(gName, preBuilder.toString(), i + 1, isRequired, responseFieldMap, isResp, registryClasses, projectBuilder, groupClasses));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -266,21 +287,21 @@ public class ParamsBuildHelper {
|
|||
if (JavaClassValidateUtil.isCollection(simple)) {
|
||||
String gName = DocClassUtil.getSimpleGicName(gicName)[0];
|
||||
if (!JavaClassValidateUtil.isPrimitive(gName)) {
|
||||
paramList.addAll(buildParams(gName, preBuilder.toString(), i + 1, isRequired, responseFieldMap, isResp, registryClasses, projectBuilder));
|
||||
paramList.addAll(buildParams(gName, preBuilder.toString(), i + 1, isRequired, responseFieldMap, isResp, registryClasses, projectBuilder, groupClasses));
|
||||
}
|
||||
} else if (JavaClassValidateUtil.isMap(simple)) {
|
||||
String valType = DocClassUtil.getMapKeyValueType(gicName)[1];
|
||||
if (!JavaClassValidateUtil.isPrimitive(valType)) {
|
||||
paramList.addAll(buildParams(valType, preBuilder.toString(), i + 1, isRequired, responseFieldMap, isResp, registryClasses, projectBuilder));
|
||||
paramList.addAll(buildParams(valType, preBuilder.toString(), i + 1, isRequired, responseFieldMap, isResp, registryClasses, projectBuilder, groupClasses));
|
||||
}
|
||||
} else {
|
||||
paramList.addAll(buildParams(gicName, preBuilder.toString(), i + 1, isRequired, responseFieldMap, isResp, registryClasses, projectBuilder));
|
||||
paramList.addAll(buildParams(gicName, preBuilder.toString(), i + 1, isRequired, responseFieldMap, isResp, registryClasses, projectBuilder, groupClasses));
|
||||
}
|
||||
} else {
|
||||
paramList.addAll(buildParams(gicName, preBuilder.toString(), i + 1, isRequired, responseFieldMap, isResp, registryClasses, projectBuilder));
|
||||
paramList.addAll(buildParams(gicName, preBuilder.toString(), i + 1, isRequired, responseFieldMap, isResp, registryClasses, projectBuilder, groupClasses));
|
||||
}
|
||||
} else {
|
||||
paramList.addAll(buildParams(subTypeName, preBuilder.toString(), i + 1, isRequired, responseFieldMap, isResp, registryClasses, projectBuilder));
|
||||
paramList.addAll(buildParams(subTypeName, preBuilder.toString(), i + 1, isRequired, responseFieldMap, isResp, registryClasses, projectBuilder, groupClasses));
|
||||
}
|
||||
n++;
|
||||
}
|
||||
|
@ -289,13 +310,13 @@ public class ParamsBuildHelper {
|
|||
if (className.equals(fieldGicName)) {
|
||||
//do nothing
|
||||
} else if (!JavaClassValidateUtil.isPrimitive(fieldGicName)) {
|
||||
paramList.addAll(buildParams(fieldGicName, preBuilder.toString(), i + 1, isRequired, responseFieldMap, isResp, registryClasses, projectBuilder));
|
||||
paramList.addAll(buildParams(fieldGicName, preBuilder.toString(), i + 1, isRequired, responseFieldMap, isResp, registryClasses, projectBuilder, groupClasses));
|
||||
}
|
||||
} else if (simpleName.equals(subTypeName)) {
|
||||
//do nothing
|
||||
} else {
|
||||
if (!javaClass.isEnum()) {
|
||||
paramList.addAll(buildParams(fieldGicName, preBuilder.toString(), i + 1, isRequired, responseFieldMap, isResp, registryClasses, projectBuilder));
|
||||
paramList.addAll(buildParams(fieldGicName, preBuilder.toString(), i + 1, isRequired, responseFieldMap, isResp, registryClasses, projectBuilder, groupClasses));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -109,7 +109,8 @@ public interface IDocBuildTemplate {
|
|||
if (JavaClassValidateUtil.isPrimitive(gicName)) {
|
||||
return ParamsBuildHelper.primitiveReturnRespComment("array of " + DocClassUtil.processTypeNameForParams(gicName));
|
||||
}
|
||||
return ParamsBuildHelper.buildParams(gicName, "", 0, null, projectBuilder.getCustomRespFieldMap(), Boolean.TRUE, new HashMap<>(), projectBuilder);
|
||||
return ParamsBuildHelper.buildParams(gicName, "", 0, null, projectBuilder.getCustomRespFieldMap(),
|
||||
Boolean.TRUE, new HashMap<>(), projectBuilder, null);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
@ -122,10 +123,12 @@ public interface IDocBuildTemplate {
|
|||
if (JavaClassValidateUtil.isPrimitive(keyValue[1])) {
|
||||
return ParamsBuildHelper.primitiveReturnRespComment("key value");
|
||||
}
|
||||
return ParamsBuildHelper.buildParams(keyValue[1], "", 0, null, projectBuilder.getCustomRespFieldMap(), Boolean.TRUE, new HashMap<>(), projectBuilder);
|
||||
return ParamsBuildHelper.buildParams(keyValue[1], "", 0, null, projectBuilder.getCustomRespFieldMap(),
|
||||
Boolean.TRUE, new HashMap<>(), projectBuilder, null);
|
||||
}
|
||||
if (StringUtil.isNotEmpty(returnType)) {
|
||||
return ParamsBuildHelper.buildParams(returnType, "", 0, null, projectBuilder.getCustomRespFieldMap(), Boolean.TRUE, new HashMap<>(), projectBuilder);
|
||||
return ParamsBuildHelper.buildParams(returnType, "", 0, null, projectBuilder.getCustomRespFieldMap(),
|
||||
Boolean.TRUE, new HashMap<>(), projectBuilder, null);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -396,6 +396,7 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate {
|
|||
}
|
||||
JavaClass javaClass = builder.getJavaProjectBuilder().getClassByName(fullTypeName);
|
||||
List<JavaAnnotation> annotations = parameter.getAnnotations();
|
||||
List<String> groupClasses = JavaClassUtil.getParamGroupJavaClass(annotations);
|
||||
String strRequired = "true";
|
||||
for (JavaAnnotation annotation : annotations) {
|
||||
String annotationName = JavaClassUtil.getAnnotationSimpleName(annotation.getType().getName());
|
||||
|
@ -439,7 +440,7 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate {
|
|||
} else {
|
||||
if (requestBodyCounter > 0) {
|
||||
//for json
|
||||
paramList.addAll(ParamsBuildHelper.buildParams(gicNameArr[0], DocGlobalConstants.ENMPTY, 0, "true", responseFieldMap, Boolean.FALSE, new HashMap<>(), builder));
|
||||
paramList.addAll(ParamsBuildHelper.buildParams(gicNameArr[0], DocGlobalConstants.ENMPTY, 0, "true", responseFieldMap, Boolean.FALSE, new HashMap<>(), builder,groupClasses));
|
||||
} else {
|
||||
throw new RuntimeException("Spring MVC can't support binding Collection on method "
|
||||
+ javaMethod.getName() + "Check it in " + javaMethod.getDeclaringClass().getCanonicalName());
|
||||
|
@ -458,13 +459,13 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate {
|
|||
continue out;
|
||||
}
|
||||
String[] gicNameArr = DocClassUtil.getSimpleGicName(typeName);
|
||||
paramList.addAll(ParamsBuildHelper.buildParams(gicNameArr[1], DocGlobalConstants.ENMPTY, 0, "true", responseFieldMap, Boolean.FALSE, new HashMap<>(), builder));
|
||||
paramList.addAll(ParamsBuildHelper.buildParams(gicNameArr[1], DocGlobalConstants.ENMPTY, 0, "true", responseFieldMap, Boolean.FALSE, new HashMap<>(), builder,groupClasses));
|
||||
} else if (javaClass.isEnum()) {
|
||||
ApiParam param = ApiParam.of().setField(paramName)
|
||||
.setType("string").setDesc(comment).setRequired(required).setVersion(DocGlobalConstants.DEFAULT_VERSION);
|
||||
paramList.add(param);
|
||||
} else {
|
||||
paramList.addAll(ParamsBuildHelper.buildParams(fullTypeName, DocGlobalConstants.ENMPTY, 0, "true", responseFieldMap, Boolean.FALSE, new HashMap<>(), builder));
|
||||
paramList.addAll(ParamsBuildHelper.buildParams(fullTypeName, DocGlobalConstants.ENMPTY, 0, "true", responseFieldMap, Boolean.FALSE, new HashMap<>(), builder,groupClasses));
|
||||
}
|
||||
}
|
||||
return paramList;
|
||||
|
|
|
@ -22,16 +22,19 @@
|
|||
*/
|
||||
package com.power.doc.utils;
|
||||
|
||||
import com.power.common.util.CollectionUtil;
|
||||
import com.power.common.util.StringUtil;
|
||||
import com.thoughtworks.qdox.model.JavaClass;
|
||||
import com.thoughtworks.qdox.model.JavaField;
|
||||
import com.thoughtworks.qdox.model.JavaMethod;
|
||||
import com.thoughtworks.qdox.model.JavaType;
|
||||
import com.power.doc.constants.DocValidatorAnnotationEnum;
|
||||
import com.thoughtworks.qdox.model.*;
|
||||
import com.thoughtworks.qdox.model.expression.AnnotationValue;
|
||||
import com.thoughtworks.qdox.model.expression.AnnotationValueList;
|
||||
import com.thoughtworks.qdox.model.expression.TypeRef;
|
||||
import com.thoughtworks.qdox.model.impl.DefaultJavaField;
|
||||
import com.thoughtworks.qdox.model.impl.DefaultJavaParameterizedType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Handle JavaClass
|
||||
|
@ -170,4 +173,66 @@ public class JavaClassUtil {
|
|||
});
|
||||
return javaClassList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtain Validate Group classes
|
||||
*
|
||||
* @param annotations the annotations of controller method param
|
||||
* @return the group annotation value
|
||||
*/
|
||||
public static List<String> getParamGroupJavaClass(List<JavaAnnotation> annotations) {
|
||||
if (CollectionUtil.isEmpty(annotations)) {
|
||||
return new ArrayList<>(0);
|
||||
}
|
||||
List<String> javaClassList = new ArrayList<>();
|
||||
List<String> validates = DocValidatorAnnotationEnum.listValidatorAnnotations();
|
||||
for (JavaAnnotation javaAnnotation : annotations) {
|
||||
List<AnnotationValue> annotationValueList = getAnnotationValues(validates,javaAnnotation);
|
||||
addGroupClass(annotationValueList, javaClassList);
|
||||
}
|
||||
return javaClassList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtain Validate Group classes
|
||||
* @param javaAnnotation the annotation of controller method param
|
||||
* @return the group annotation value
|
||||
*/
|
||||
public static List<String> getParamGroupJavaClass(JavaAnnotation javaAnnotation) {
|
||||
if (Objects.isNull(javaAnnotation)) {
|
||||
return new ArrayList<>(0);
|
||||
}
|
||||
List<String> javaClassList = new ArrayList<>();
|
||||
List<String> validates = DocValidatorAnnotationEnum.listValidatorAnnotations();
|
||||
List<AnnotationValue> annotationValueList = getAnnotationValues(validates,javaAnnotation);
|
||||
addGroupClass(annotationValueList, javaClassList);
|
||||
return javaClassList;
|
||||
}
|
||||
|
||||
|
||||
private static void addGroupClass(List<AnnotationValue> annotationValueList, List<String> javaClassList) {
|
||||
if (CollectionUtil.isEmpty(annotationValueList)) {
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < annotationValueList.size(); i++) {
|
||||
TypeRef annotationValue = (TypeRef) annotationValueList.get(i);
|
||||
DefaultJavaParameterizedType annotationValueType = (DefaultJavaParameterizedType) annotationValue.getType();
|
||||
javaClassList.add(annotationValueType.getGenericCanonicalName());
|
||||
}
|
||||
}
|
||||
|
||||
private static List<AnnotationValue> getAnnotationValues(List<String> validates,JavaAnnotation javaAnnotation){
|
||||
List<AnnotationValue> annotationValueList = null;
|
||||
String simpleName = javaAnnotation.getType().getSimpleName();
|
||||
if (simpleName.equalsIgnoreCase("Validated")) {
|
||||
if (Objects.nonNull(javaAnnotation.getProperty("value"))) {
|
||||
annotationValueList = ((AnnotationValueList) javaAnnotation.getProperty("value")).getValueList();
|
||||
}
|
||||
} else if (validates.contains(simpleName)) {
|
||||
if (Objects.nonNull(javaAnnotation.getProperty("groups"))) {
|
||||
annotationValueList = ((AnnotationValueList) javaAnnotation.getProperty("groups")).getValueList();
|
||||
}
|
||||
}
|
||||
return annotationValueList;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -175,11 +175,8 @@ public class JavaClassValidateUtil {
|
|||
public static boolean isJSR303Required(String annotationSimpleName) {
|
||||
switch (annotationSimpleName) {
|
||||
case "NotNull":
|
||||
return true;
|
||||
case "NotEmpty":
|
||||
return true;
|
||||
case "NotBlank":
|
||||
return true;
|
||||
case "Required":
|
||||
return true;
|
||||
default:
|
||||
|
@ -187,6 +184,7 @@ public class JavaClassValidateUtil {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* custom tag
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue