diff --git a/src/main/java/com/ly/doc/constants/ValidatorAnnotations.java b/src/main/java/com/ly/doc/constants/ValidatorAnnotations.java index 084c63c7..defdeacc 100644 --- a/src/main/java/com/ly/doc/constants/ValidatorAnnotations.java +++ b/src/main/java/com/ly/doc/constants/ValidatorAnnotations.java @@ -27,7 +27,25 @@ public interface ValidatorAnnotations { String VALID = "Valid"; - String NULL = "Null"; + /** + * Spring validator annotations `@NotEmpty` + */ + String NOT_EMPTY = "NotEmpty"; + + /** + * Spring validator annotations `@NotBlank` + */ + String NOT_BLANK = "NotBlank"; + + /** + * Spring validator annotations `@NotNull` + */ + String NOT_NULL = "NotNull"; + + /** + * Spring validator annotations `@Null` + */ + String NULL= "Null"; String VALIDATED = "Validated"; } diff --git a/src/main/java/com/ly/doc/helper/ParamsBuildHelper.java b/src/main/java/com/ly/doc/helper/ParamsBuildHelper.java index d1be7db0..5c06953d 100644 --- a/src/main/java/com/ly/doc/helper/ParamsBuildHelper.java +++ b/src/main/java/com/ly/doc/helper/ParamsBuildHelper.java @@ -247,7 +247,7 @@ public static List buildParams(String className, String pre, int level } } } - comment.append(JavaFieldUtil.getJsrComment(classLoader, javaAnnotations)); + comment.append(JavaFieldUtil.getJsrComment(apiConfig.isShowValidation(), classLoader, javaAnnotations)); // fixme post form curl example error String fieldValue = getFieldValueFromMock(tagsMap); diff --git a/src/main/java/com/ly/doc/model/ApiConfig.java b/src/main/java/com/ly/doc/model/ApiConfig.java index 6e707f7e..4b76b5be 100644 --- a/src/main/java/com/ly/doc/model/ApiConfig.java +++ b/src/main/java/com/ly/doc/model/ApiConfig.java @@ -391,6 +391,12 @@ public class ApiConfig { */ private Integer apiUploadNums; + /** + * Show JSR validation information + * @since 3.0.3 + */ + private boolean showValidation = Boolean.TRUE; + public static ApiConfig getInstance() { return instance; } @@ -1040,4 +1046,12 @@ public Integer getApiUploadNums() { public void setApiUploadNums(Integer apiUploadNums) { this.apiUploadNums = apiUploadNums; } + + public boolean isShowValidation() { + return showValidation; + } + + public void setShowValidation(boolean showValidation) { + this.showValidation = showValidation; + } } diff --git a/src/main/java/com/ly/doc/template/IRestDocTemplate.java b/src/main/java/com/ly/doc/template/IRestDocTemplate.java index ecde5812..44bb0c71 100644 --- a/src/main/java/com/ly/doc/template/IRestDocTemplate.java +++ b/src/main/java/com/ly/doc/template/IRestDocTemplate.java @@ -502,10 +502,10 @@ default List buildEntryPointMethod( //handle extension Map extensions = DocUtil.getCommentsByTag(method, DocTags.EXTENSION, null); - if (extensions != null){ + if (extensions != null) { Map extensionParams = apiMethodDoc.getExtensions() != null ? apiMethodDoc.getExtensions() : new HashMap(); - extensions.entrySet().stream().forEach( e -> extensionParams.put(e.getKey(), DocUtil.detectTagValue(e.getValue()))); - apiMethodDoc.setExtensions( extensionParams ); + extensions.entrySet().stream().forEach(e -> extensionParams.put(e.getKey(), DocUtil.detectTagValue(e.getValue()))); + apiMethodDoc.setExtensions(extensionParams); } TornaUtil.setTornaArrayTags(docJavaMethod.getJavaMethod(), apiMethodDoc, apiConfig); @@ -519,6 +519,7 @@ default ApiMethodReqParam requestParams(final DocJavaMethod docJavaMethod, Proje List configApiReqParams, FrameworkAnnotations frameworkAnnotations) { JavaMethod javaMethod = docJavaMethod.getJavaMethod(); boolean isStrict = builder.getApiConfig().isStrict(); + boolean isShowValidation = builder.getApiConfig().isShowValidation(); ClassLoader classLoader = builder.getApiConfig().getClassLoader(); String className = javaMethod.getDeclaringClass().getCanonicalName(); Map paramTagMap = docJavaMethod.getParamTagMap(); @@ -638,7 +639,7 @@ default ApiMethodReqParam requestParams(final DocJavaMethod docJavaMethod, Proje } required = Boolean.parseBoolean(strRequired); } - comment.append(JavaFieldUtil.getJsrComment(classLoader, annotations)); + comment.append(JavaFieldUtil.getJsrComment(isShowValidation, classLoader, annotations)); if (requestFieldToUnderline && !isPathVariable) { paramName = StringUtil.camelToUnderline(paramName); } diff --git a/src/main/java/com/ly/doc/template/JaxrsDocBuildTemplate.java b/src/main/java/com/ly/doc/template/JaxrsDocBuildTemplate.java index 9f76a405..d3d6c903 100644 --- a/src/main/java/com/ly/doc/template/JaxrsDocBuildTemplate.java +++ b/src/main/java/com/ly/doc/template/JaxrsDocBuildTemplate.java @@ -339,6 +339,7 @@ private ApiMethodReqParam requestParams(final DocJavaMethod docJavaMethod, Proje .setRequestParams(new ArrayList<>(0)); } boolean isStrict = builder.getApiConfig().isStrict(); + boolean isShowValidation = builder.getApiConfig().isShowValidation(); ClassLoader classLoader = builder.getApiConfig().getClassLoader(); JavaMethod javaMethod = docJavaMethod.getJavaMethod(); String className = javaMethod.getDeclaringClass().getCanonicalName(); @@ -401,7 +402,7 @@ private ApiMethodReqParam requestParams(final DocJavaMethod docJavaMethod, Proje strRequired = "true"; } } - comment.append(JavaFieldUtil.getJsrComment(classLoader, annotations)); + comment.append(JavaFieldUtil.getJsrComment(isShowValidation, classLoader, annotations)); } else { isRequestBody = true; } diff --git a/src/main/java/com/ly/doc/template/RpcDocBuildTemplate.java b/src/main/java/com/ly/doc/template/RpcDocBuildTemplate.java index adf4a8de..24864dc8 100644 --- a/src/main/java/com/ly/doc/template/RpcDocBuildTemplate.java +++ b/src/main/java/com/ly/doc/template/RpcDocBuildTemplate.java @@ -169,6 +169,7 @@ private List requestParams(final JavaMethod javaMethod, Map actualTypesMap) { boolean isStrict = builder.getApiConfig().isStrict(); boolean isShowJavaType = builder.getApiConfig().getShowJavaType(); + boolean isShowValidation = builder.getApiConfig().isShowValidation(); String className = javaMethod.getDeclaringClass().getCanonicalName(); Map paramTagMap = DocUtil.getCommentsByTag(javaMethod, DocTags.PARAM, className); List parameterList = javaMethod.getParameters(); @@ -197,7 +198,7 @@ private List requestParams(final JavaMethod javaMethod, required = true; } } - comment.append(JavaFieldUtil.getJsrComment(classLoader, annotations)); + comment.append(JavaFieldUtil.getJsrComment(isShowValidation, classLoader, annotations)); Set groupClasses = JavaClassUtil.getParamGroupJavaClass(annotations, builder.getJavaProjectBuilder()); if (JavaClassValidateUtil.isCollection(fullTypeName) || JavaClassValidateUtil.isArray(fullTypeName)) { if (JavaClassValidateUtil.isCollection(typeName)) { diff --git a/src/main/java/com/ly/doc/utils/JavaFieldUtil.java b/src/main/java/com/ly/doc/utils/JavaFieldUtil.java index bce83d3e..521b9328 100644 --- a/src/main/java/com/ly/doc/utils/JavaFieldUtil.java +++ b/src/main/java/com/ly/doc/utils/JavaFieldUtil.java @@ -25,6 +25,7 @@ import com.ly.doc.constants.DocAnnotationConstants; import com.ly.doc.constants.DocGlobalConstants; import com.ly.doc.constants.DocValidatorAnnotationEnum; +import com.ly.doc.constants.ValidatorAnnotations; import com.ly.doc.model.CustomField; import com.ly.doc.model.DocJavaField; import com.power.common.util.StringUtil; @@ -128,40 +129,47 @@ public static String getParamMaxLength(List annotations) { /** * getJsr303Comment * - * @param annotations annotations + * @param showValidation Show JSR validation information + * @param classLoader ClassLoader + * @param annotations annotations * @return Jsr comments */ - public static String getJsrComment(ClassLoader classLoader, List annotations) { + public static String getJsrComment(boolean showValidation, ClassLoader classLoader, List annotations) { + if (!showValidation) { + return DocGlobalConstants.EMPTY; + } StringBuilder sb = new StringBuilder(); for (JavaAnnotation annotation : annotations) { Map values = annotation.getPropertyMap(); String name = annotation.getType().getValue(); + if (ValidatorAnnotations.NOT_BLANK.equals(name) || + ValidatorAnnotations.NOT_EMPTY.equals(name) || + ValidatorAnnotations.NOT_NULL.equals(name) || + ValidatorAnnotations.NULL.equals(name) || + ValidatorAnnotations.VALIDATED.equals(name)) { + continue; + } if (DocValidatorAnnotationEnum.listValidatorAnnotations().contains(name)) { + sb.append(name).append("("); + int j = 0; for (Map.Entry m : values.entrySet()) { + j++; String value = DocUtil.resolveAnnotationValue(classLoader, m.getValue()); - if (DocAnnotationConstants.REGEXP.equals(m.getKey())) { - sb.append(m.getKey()).append(": ").append(StringUtil.removeDoubleQuotes(value)) - .append("; "); - } - if (DocAnnotationConstants.MAX.equals(m.getKey())) { - sb.append(m.getKey()).append(": ").append(StringUtil.removeDoubleQuotes(value)) - .append("; "); - } - if (DocAnnotationConstants.LENGTH.equals(m.getKey())) { - sb.append(m.getKey()).append(": ").append(StringUtil.removeDoubleQuotes(value)) - .append("; "); - } - if (DocAnnotationConstants.SIZE.equals(m.getKey())) { - sb.append(m.getKey()).append(": ").append(StringUtil.removeDoubleQuotes(value)) - .append("; "); + sb.append(m.getKey()).append("=").append(StringUtil.removeDoubleQuotes(value)); + if (j < values.size()) { + sb.append(", "); } } + sb.append("); "); } } if (sb.length() < 1) { return DocGlobalConstants.EMPTY; } - return "\nValidate[" + sb + "]"; + if (sb.toString().contains(";")) { + sb.deleteCharAt(sb.lastIndexOf(";")); + } + return "\nValidation[" + sb + "]"; }