Skip to content

Commit

Permalink
Merge pull request #755 from shalousun/master
Browse files Browse the repository at this point in the history
feat: Optimizing the extraction of JSR validation information
  • Loading branch information
shalousun authored Mar 6, 2024
2 parents a76145c + e4c90b2 commit 9bcb98a
Showing 7 changed files with 69 additions and 26 deletions.
20 changes: 19 additions & 1 deletion src/main/java/com/ly/doc/constants/ValidatorAnnotations.java
Original file line number Diff line number Diff line change
@@ -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";
}
2 changes: 1 addition & 1 deletion src/main/java/com/ly/doc/helper/ParamsBuildHelper.java
Original file line number Diff line number Diff line change
@@ -247,7 +247,7 @@ public static List<ApiParam> 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);

14 changes: 14 additions & 0 deletions src/main/java/com/ly/doc/model/ApiConfig.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
9 changes: 5 additions & 4 deletions src/main/java/com/ly/doc/template/IRestDocTemplate.java
Original file line number Diff line number Diff line change
@@ -502,10 +502,10 @@ default List<ApiMethodDoc> buildEntryPointMethod(

//handle extension
Map<String, String> 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<ApiReqParam> 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<String, String> 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);
}
3 changes: 2 additions & 1 deletion src/main/java/com/ly/doc/template/JaxrsDocBuildTemplate.java
Original file line number Diff line number Diff line change
@@ -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;
}
3 changes: 2 additions & 1 deletion src/main/java/com/ly/doc/template/RpcDocBuildTemplate.java
Original file line number Diff line number Diff line change
@@ -169,6 +169,7 @@ private List<ApiParam> requestParams(final JavaMethod javaMethod,
Map<String, JavaType> actualTypesMap) {
boolean isStrict = builder.getApiConfig().isStrict();
boolean isShowJavaType = builder.getApiConfig().getShowJavaType();
boolean isShowValidation = builder.getApiConfig().isShowValidation();
String className = javaMethod.getDeclaringClass().getCanonicalName();
Map<String, String> paramTagMap = DocUtil.getCommentsByTag(javaMethod, DocTags.PARAM, className);
List<JavaParameter> parameterList = javaMethod.getParameters();
@@ -197,7 +198,7 @@ private List<ApiParam> requestParams(final JavaMethod javaMethod,
required = true;
}
}
comment.append(JavaFieldUtil.getJsrComment(classLoader, annotations));
comment.append(JavaFieldUtil.getJsrComment(isShowValidation, classLoader, annotations));
Set<String> groupClasses = JavaClassUtil.getParamGroupJavaClass(annotations, builder.getJavaProjectBuilder());
if (JavaClassValidateUtil.isCollection(fullTypeName) || JavaClassValidateUtil.isArray(fullTypeName)) {
if (JavaClassValidateUtil.isCollection(typeName)) {
44 changes: 26 additions & 18 deletions src/main/java/com/ly/doc/utils/JavaFieldUtil.java
Original file line number Diff line number Diff line change
@@ -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<JavaAnnotation> 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<JavaAnnotation> annotations) {
public static String getJsrComment(boolean showValidation, ClassLoader classLoader, List<JavaAnnotation> annotations) {
if (!showValidation) {
return DocGlobalConstants.EMPTY;
}
StringBuilder sb = new StringBuilder();
for (JavaAnnotation annotation : annotations) {
Map<String, AnnotationValue> 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<String, AnnotationValue> 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 + "]";
}


0 comments on commit 9bcb98a

Please sign in to comment.