Support use FastJson JSONType annotation and Jackson JsonIgnoreProperties annotation to ignore fields. #https://gitee.com/smart-doc-team/smart-doc/issues/I401KH .

This commit is contained in:
shalousun 2021-07-11 18:34:45 +08:00
parent 79e1b1d176
commit 4cc4c8e00a
7 changed files with 94 additions and 9 deletions

View File

@ -125,7 +125,7 @@ smart-doc官方目前已经开发完成[Maven插件](https://gitee.com/smart-doc
"outPath": "D://md2", //指定文档的输出路径
"coverOld": true, //是否覆盖旧的文件主要用于mardown文件覆盖
"createDebugPage": true,//@since 2.0.0 smart-doc支持创建可以测试的html页面仅在AllInOne模式中起作用。
"packageFilters": "",//controller包过滤多个包用英文逗号隔开需要采用正则com.test.controller.*
"packageFilters": "",//controller包过滤多个包用英文逗号隔开2.2.2开始需要采用正则com.test.controller.*
"md5EncryptedHtmlName": false,//只有每个controller生成一个html文件是才使用
"style":"xt256", //基于highlight.js的代码高设置,可选值很多可查看码云wiki喜欢配色统一简洁的同学可以不设置
"projectName": "smart-doc",//配置自己的项目名称

View File

@ -39,6 +39,11 @@ public interface DocAnnotationConstants {
String SHORT_JSON_IGNORE = "JsonIgnore";
/**
* jackson JsonIgnoreProperties annotation
*/
String SHORT_JSON_IGNORE_PROPERTIES = "JsonIgnoreProperties";
String SHORT_JSON_PROPERTY = "JsonProperty";
String SHORT_JSON_FIELD = "JSONField";
@ -68,4 +73,14 @@ public interface DocAnnotationConstants {
String MAX = "max";
String JSON_PROPERTY = "JsonProperty";
/**
* Fastjson JSONType annotation
*/
String SHORT_JSON_TYPE = "JSONType";
/**
* Fastjson JSONType annotation ignores prop
*/
String IGNORE_PROP = "ignores";
}

View File

@ -141,6 +141,8 @@ public interface DocGlobalConstants {
String JAVA_OBJECT_FULLY = "java.lang.Object";
String JAVA_BOOLEAN = "java.lang.Boolean";
String JAVA_STRING_FULLY = "java.lang.String";
String JAVA_MAP_FULLY = "java.util.Map";

View File

@ -211,14 +211,18 @@ public class JsonBuildHelper {
boolean requestFieldToUnderline = builder.getApiConfig().isRequestFieldToUnderline();
boolean responseFieldToUnderline = builder.getApiConfig().isResponseFieldToUnderline();
List<DocJavaField> fields = JavaClassUtil.getFields(cls, 0, new LinkedHashMap<>());
Map<String, String> ignoreFields = JavaClassUtil.getClassJsonIgnoreFields(cls);
out:
for (DocJavaField docField : fields) {
JavaField field = docField.getJavaField();
if (field.isTransient() && skipTransientField) {
continue;
}
String fieldName = docField.getFieldName();
if (ignoreFields.containsKey(fieldName)) {
continue;
}
String subTypeName = docField.getFullyQualifiedName();
String fieldName = field.getName();
if ((responseFieldToUnderline && isResp) || (requestFieldToUnderline && !isResp)) {
fieldName = StringUtil.camelToUnderline(fieldName);
}
@ -260,10 +264,10 @@ public class JsonBuildHelper {
}
String typeSimpleName = field.getType().getSimpleName();
String fieldGicName = docField.getGenericCanonicalName();
CustomField customResponseField = builder.getCustomRespFieldMap().get(typeName +"."+fieldName);
CustomField customRequestField = builder.getCustomReqFieldMap().get(typeName +"."+fieldName);
CustomField customResponseField = builder.getCustomRespFieldMap().get(typeName + "." + fieldName);
CustomField customRequestField = builder.getCustomReqFieldMap().get(typeName + "." + fieldName);
if (customRequestField != null && JavaClassUtil.isTargetChildClass(typeName, customRequestField.getOwnerClassName()) && (customRequestField.isIgnore()) && !isResp) {
if (customRequestField != null && JavaClassUtil.isTargetChildClass(typeName, customRequestField.getOwnerClassName()) && (customRequestField.isIgnore()) && !isResp) {
continue;
}
if (customResponseField != null && JavaClassUtil.isTargetChildClass(typeName, customResponseField.getOwnerClassName()) && (customResponseField.isIgnore()) && isResp) {

View File

@ -115,6 +115,7 @@ public class ParamsBuildHelper {
paramList.addAll(buildParams(globGicName[0], pre, nextLevel, isRequired, isResp,
registryClasses, projectBuilder, groupClasses, pid, jsonRequest));
} else {
Map<String, String> ignoreFields = JavaClassUtil.getClassJsonIgnoreFields(cls);
out:
for (DocJavaField docField : fields) {
String maxLength = null;
@ -122,7 +123,11 @@ public class ParamsBuildHelper {
if (field.isTransient() && skipTransientField) {
continue;
}
String fieldName = field.getName();
String fieldName = docField.getFieldName();
if (ignoreFields.containsKey(fieldName)) {
continue;
}
String subTypeName = docField.getFullyQualifiedName();
if ((responseFieldToUnderline && isResp) || (requestFieldToUnderline && !isResp)) {
fieldName = StringUtil.camelToUnderline(fieldName);
@ -147,12 +152,12 @@ public class ParamsBuildHelper {
}
boolean strRequired = false;
int annotationCounter = 0;
CustomField customResponseField = responseFieldMap.get(simpleName + "."+ fieldName);
CustomField customResponseField = responseFieldMap.get(simpleName + "." + fieldName);
if (customResponseField != null && JavaClassUtil.isTargetChildClass(simpleName, customResponseField.getOwnerClassName())
&& (customResponseField.isIgnore()) && isResp) {
continue;
}
CustomField customRequestField = projectBuilder.getCustomReqFieldMap().get(simpleName +"."+fieldName);
CustomField customRequestField = projectBuilder.getCustomReqFieldMap().get(simpleName + "." + fieldName);
if (customRequestField != null && JavaClassUtil.isTargetChildClass(simpleName, customRequestField.getOwnerClassName())
&& (customRequestField.isIgnore()) && !isResp) {
continue;

View File

@ -69,6 +69,11 @@ public class DocJavaField {
*/
private String actualJavaType;
/**
* field name
*/
private String fieldName;
private boolean array;
private boolean primitive;
@ -199,4 +204,13 @@ public class DocJavaField {
public void setEnum(boolean anEnum) {
isEnum = anEnum;
}
public String getFieldName() {
return fieldName;
}
public DocJavaField setFieldName(String fieldName) {
this.fieldName = fieldName;
return this;
}
}

View File

@ -25,6 +25,7 @@ package com.power.doc.utils;
import com.power.common.util.CollectionUtil;
import com.power.common.util.StringUtil;
import com.power.doc.constants.DocAnnotationConstants;
import com.power.doc.constants.DocGlobalConstants;
import com.power.doc.constants.DocValidatorAnnotationEnum;
import com.power.doc.constants.ValidatorAnnotations;
import com.power.doc.model.DocJavaField;
@ -89,6 +90,7 @@ public class JavaClassUtil {
String comment = javaMethod.getComment();
JavaField javaField = new DefaultJavaField(javaMethod.getReturns(), methodName);
DocJavaField docJavaField = DocJavaField.builder()
.setFieldName(methodName)
.setJavaField(javaField)
.setComment(comment)
.setDocletTags(javaMethod.getTags())
@ -129,16 +131,23 @@ public class JavaClassUtil {
DocJavaField docJavaField = addedFields.get(methodName);
docJavaField.setAnnotations(method.getAnnotations());
docJavaField.setComment(comment);
docJavaField.setFieldName(methodName);
addedFields.put(methodName, docJavaField);
}
}
for (JavaField javaField : cls1.getFields()) {
String fieldName = javaField.getName();
String subTypeName = javaField.getType().getFullyQualifiedName();
if (javaField.isStatic() || "this$0".equals(fieldName) ||
JavaClassValidateUtil.isIgnoreFieldTypes(subTypeName)) {
continue;
}
if (fieldName.startsWith("is") && ("boolean".equals(subTypeName)
|| DocGlobalConstants.JAVA_BOOLEAN.equals(subTypeName))) {
fieldName = StringUtil.firstToLowerCase(fieldName.substring(2));
}
DocJavaField docJavaField = DocJavaField.builder();
boolean typeChecked = false;
String gicName = javaField.getType().getGenericCanonicalName();
@ -174,7 +183,8 @@ public class JavaClassUtil {
docJavaField.setComment(javaField.getComment())
.setJavaField(javaField).setFullyQualifiedName(subTypeName)
.setGenericCanonicalName(gicName).setActualJavaType(actualType)
.setAnnotations(javaField.getAnnotations());
.setAnnotations(javaField.getAnnotations())
.setFieldName(fieldName);
if (addedFields.containsKey(fieldName)) {
addedFields.put(fieldName, docJavaField);
continue;
@ -518,4 +528,39 @@ public class JavaClassUtil {
}
return false;
}
public static Map<String, String> getClassJsonIgnoreFields(JavaClass cls) {
List<JavaAnnotation> classAnnotation = cls.getAnnotations();
Map<String, String> ignoreFields = new HashMap<>();
for (JavaAnnotation annotation : classAnnotation) {
String simpleAnnotationName = annotation.getType().getValue();
if (DocAnnotationConstants.SHORT_JSON_IGNORE_PROPERTIES.equalsIgnoreCase(simpleAnnotationName)) {
return JavaClassUtil.getJsonIgnoresProp(annotation, DocAnnotationConstants.VALUE_PROP);
}
if (DocAnnotationConstants.SHORT_JSON_TYPE.equals(simpleAnnotationName)) {
return JavaClassUtil.getJsonIgnoresProp(annotation, DocAnnotationConstants.IGNORE_PROP);
}
}
return ignoreFields;
}
public static Map<String, String> getJsonIgnoresProp(JavaAnnotation annotation, String propName) {
Map<String, String> ignoreFields = new HashMap<>();
Object ignoresObject = annotation.getNamedParameter(propName);
if (Objects.isNull(ignoresObject)) {
return ignoreFields;
}
if (ignoresObject instanceof String) {
String prop = StringUtil.removeQuotes(ignoresObject.toString());
ignoreFields.put(prop, null);
return ignoreFields;
}
List<String> ignorePropList = (LinkedList) ignoresObject;
for (String str : ignorePropList) {
String prop = StringUtil.removeQuotes(str);
ignoreFields.put(prop, null);
}
return ignoreFields;
}
}