Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat ✨ Split transient field serialization into request and response flags #904

Merged
merged 1 commit into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/main/java/com/ly/doc/helper/FormDataBuildHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
*/
package com.ly.doc.helper;

import java.util.*;

import com.ly.doc.constants.*;
import com.ly.doc.builder.ProjectDocConfigBuilder;
import com.ly.doc.constants.DocTags;
import com.ly.doc.constants.JSRAnnotationConstants;
import com.ly.doc.constants.ParamTypeConstants;
import com.ly.doc.model.ApiConfig;
import com.ly.doc.model.CustomField;
import com.ly.doc.model.DocJavaField;
Expand All @@ -31,11 +32,12 @@
import com.power.common.util.CollectionUtil;
import com.power.common.util.RandomUtil;
import com.power.common.util.StringUtil;
import com.ly.doc.builder.ProjectDocConfigBuilder;
import com.thoughtworks.qdox.model.JavaAnnotation;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaField;

import java.util.*;

/**
* @author yu 2019/12/25.
*/
Expand Down Expand Up @@ -72,7 +74,6 @@ public static List<FormData> getFormData(String className, Map<String, String> r
// Registry class
registryClasses.put(className, className);
counter++;
boolean skipTransientField = apiConfig.isSkipTransientField();
boolean requestFieldToUnderline = apiConfig.isRequestFieldToUnderline();
boolean responseFieldToUnderline = apiConfig.isResponseFieldToUnderline();
String simpleName = DocClassUtil.getSimpleName(className);
Expand Down Expand Up @@ -109,9 +110,10 @@ public static List<FormData> getFormData(String className, Map<String, String> r
|| JavaClassValidateUtil.isIgnoreFieldTypes(subTypeName)) {
continue;
}
if (field.isTransient() && skipTransientField) {
if (field.isTransient() && apiConfig.isSerializeRequestTransients()) {
continue;
}

List<JavaAnnotation> javaAnnotations = docField.getAnnotations();
for (JavaAnnotation annotation : javaAnnotations) {
String simpleAnnotationName = annotation.getType().getValue();
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/com/ly/doc/helper/JsonBuildHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public static String buildJson(String typeName, String genericCanonicalName, boo
if (javaClass.isEnum()) {
return StringUtil.removeQuotes(String.valueOf(JavaClassUtil.getEnumValue(javaClass, Boolean.FALSE)));
}
boolean skipTransientField = apiConfig.isSkipTransientField();

StringBuilder data0 = new StringBuilder();
JavaClass cls = builder.getClassByName(typeName);

Expand Down Expand Up @@ -290,9 +290,14 @@ else if (JavaClassValidateUtil.isReactor(typeName)) {
// Process each field of the class
out: for (DocJavaField docField : fields) {
JavaField field = docField.getJavaField();
if (field.isTransient() && skipTransientField) {
continue;
if (field.isTransient()) {
boolean passBuild = (apiConfig.isSerializeRequestTransients() && !isResp)
|| (apiConfig.isSerializeResponseTransients() && isResp);
if (passBuild) {
continue;
}
}

String fieldName = docField.getFieldName();

// if ignore fields contains the field name, then skip this field
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/ly/doc/helper/ParamsBuildHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ public static List<ApiParam> buildParams(String className, String pre, int level
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();
Expand Down Expand Up @@ -176,9 +175,14 @@ else if (JavaClassValidateUtil.isReactor(simpleName)) {
String maxLength = JavaFieldUtil.getParamMaxLength(field.getAnnotations());
StringBuilder comment = new StringBuilder();
comment.append(docField.getComment());
if (field.isTransient() && skipTransientField) {
continue;
if (field.isTransient()) {
boolean passBuild = (apiConfig.isSerializeRequestTransients() && !isResp)
|| (apiConfig.isSerializeResponseTransients() && isResp);
if (passBuild) {
continue;
}
}

String fieldName = docField.getFieldName();
if (Objects.nonNull(fieldNameConvert)) {
fieldName = fieldNameConvert.translate(fieldName);
Expand Down
27 changes: 21 additions & 6 deletions src/main/java/com/ly/doc/model/ApiConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,14 @@ public class ApiConfig {
private String projectCName;

/**
* Skip Transient Field
* serialize request transients;default false
*/
private boolean skipTransientField = true;
private boolean serializeRequestTransients = false;

/**
* serialize response transients;default false
*/
private boolean serializeResponseTransients = false;

/**
* @since 1.7.10 default show author
Expand Down Expand Up @@ -800,12 +805,22 @@ public void setGroup(String group) {
this.group = group;
}

public boolean isSkipTransientField() {
return skipTransientField;
public boolean isSerializeRequestTransients() {
return serializeRequestTransients;
}

public void setSkipTransientField(boolean skipTransientField) {
this.skipTransientField = skipTransientField;
public ApiConfig setSerializeRequestTransients(boolean serializeRequestTransients) {
this.serializeRequestTransients = serializeRequestTransients;
return this;
}

public boolean isSerializeResponseTransients() {
return serializeResponseTransients;
}

public ApiConfig setSerializeResponseTransients(boolean serializeResponseTransients) {
this.serializeResponseTransients = serializeResponseTransients;
return this;
}

public boolean isShowAuthor() {
Expand Down