Skip to content

Commit

Permalink
refactor (Enum): Optimize the handling logic for enum parameter types
Browse files Browse the repository at this point in the history
- Add a `type` field in the `EnumInfoAndValues` class to store the parameter type.
- Modify the enum processing method in the `JavaClassUtil` class to include type information.
- Update the enum parameter processing logic in the `IRestDocTemplate` interface.
- Adjust the `Item` class to add a `valueObject` field for storing value objects.
- Optimize the enum parameter processing in `ParamsBuildHelper` and `ParamUtil`.
- Remove redundant enum type checks in `TornaUtil`.
  • Loading branch information
linwumingshi committed Nov 15, 2024
1 parent c18ff43 commit 4d8e86f
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 31 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/ly/doc/helper/ParamsBuildHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,8 @@ else if (JavaClassValidateUtil.isCollection(subTypeName)
projectBuilder, Boolean.FALSE);
if (Objects.nonNull(enumInfoAndValue)) {
param.setValue("[\"" + enumInfoAndValue.getValue() + "\"]")
.setEnumInfoAndValues(enumInfoAndValue);
.setEnumInfoAndValues(enumInfoAndValue)
.setType(enumInfoAndValue.getType());
}
}
else if (gName.length() == 1) {
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/ly/doc/model/torna/EnumInfoAndValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public class EnumInfoAndValues implements Serializable {
*/
private Object value;

/**
* ApiParam value default enum; when type is not enum type, will set this type to
* {@link ApiParam#setType(String)}
*/
private String type;

public static EnumInfoAndValues builder() {
return new EnumInfoAndValues();
}
Expand Down Expand Up @@ -67,4 +73,13 @@ public EnumInfoAndValues setValue(Object value) {
return this;
}

public String getType() {
return type;
}

public EnumInfoAndValues setType(String type) {
this.type = type;
return this;
}

}
18 changes: 18 additions & 0 deletions src/main/java/com/ly/doc/model/torna/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/
package com.ly.doc.model.torna;

import com.google.gson.annotations.Expose;

import java.io.Serializable;

/**
Expand Down Expand Up @@ -56,6 +58,13 @@ public class Item implements Serializable {
*/
private String description;

/**
* valueObject; A temporary variable used to store the object form of the value. This
* field will not be serialized or deserialized.
*/
@Expose(serialize = false, deserialize = false)
private Object valueObject;

public Item() {
}

Expand All @@ -64,6 +73,7 @@ public Item(String name, String type, String value, String description) {
this.type = type;
this.value = value;
this.description = description;
this.valueObject = name;
}

public String getName() {
Expand Down Expand Up @@ -98,4 +108,12 @@ public void setDescription(String description) {
this.description = description;
}

public Object getValueObject() {
return valueObject;
}

public void setValueObject(Object valueObject) {
this.valueObject = valueObject;
}

}
22 changes: 14 additions & 8 deletions src/main/java/com/ly/doc/template/IRestDocTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,11 @@ default String createDocRenderHeaders(List<ApiReqParam> headers, boolean isAdoc)
* @param apiMethodDocs A list of method documentation objects corresponding to the
* methods within the class.
* @param order The sorting order for the generated API documentation entry.
* @param isUseMD5 Flag indicating whether to use MD5 hashing to generate a unique
* @param isUseMd5 Flag indicating whether to use MD5 hashing to generate a unique
* alias for the documented class.
*/
default void handleApiDoc(JavaClass cls, List<ApiDoc> apiDocList, List<ApiMethodDoc> apiMethodDocs, int order,
boolean isUseMD5) {
boolean isUseMd5) {
String controllerName = cls.getName();
ApiDoc apiDoc = new ApiDoc();
String classAuthor = JavaClassUtil.getClassTagsValue(cls, DocTags.AUTHOR, Boolean.TRUE);
Expand All @@ -277,7 +277,7 @@ default void handleApiDoc(JavaClass cls, List<ApiDoc> apiDocList, List<ApiMethod
String[] tags = tagSet.toArray(new String[] {});
apiDoc.setTags(tags);

if (isUseMD5) {
if (isUseMd5) {
String name = DocUtil.generateId(apiDoc.getName());
apiDoc.setAlias(name);
}
Expand Down Expand Up @@ -1131,8 +1131,11 @@ default ApiMethodReqParam requestParams(final DocJavaMethod docJavaMethod, Proje
.setType(ParamTypeConstants.PARAM_TYPE_ARRAY);
EnumInfoAndValues enumInfoAndValue = JavaClassUtil.getEnumInfoAndValue(gicJavaClass, builder,
Boolean.TRUE);
param.setValue(StringUtil.removeDoubleQuotes(String.valueOf(enumInfoAndValue.getValue())))
.setEnumInfoAndValues(enumInfoAndValue);
if (Objects.nonNull(enumInfoAndValue)) {
param.setValue(StringUtil.removeDoubleQuotes(String.valueOf(enumInfoAndValue.getValue())))
.setEnumInfoAndValues(enumInfoAndValue)
.setType(enumInfoAndValue.getType());
}

paramList.add(param);
if (requestBodyCounter > 0) {
Expand Down Expand Up @@ -1237,9 +1240,12 @@ else if (javaClass.isEnum()) {
.setVersion(DocGlobalConstants.DEFAULT_VERSION);

EnumInfoAndValues enumInfoAndValue = JavaClassUtil.getEnumInfoAndValue(javaClass, builder,
isPathVariable || queryParam);
param.setValue(StringUtil.removeDoubleQuotes(String.valueOf(enumInfoAndValue.getValue())))
.setEnumInfoAndValues(enumInfoAndValue);
isPathVariable || queryParam || isRequestParam);
if (Objects.nonNull(enumInfoAndValue)) {
param.setValue(StringUtil.removeDoubleQuotes(String.valueOf(enumInfoAndValue.getValue())))
.setEnumInfoAndValues(enumInfoAndValue)
.setType(enumInfoAndValue.getType());
}

paramList.add(param);
}
Expand Down
46 changes: 33 additions & 13 deletions src/main/java/com/ly/doc/utils/JavaClassUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.ly.doc.constants.DocValidatorAnnotationEnum;
import com.ly.doc.constants.JSRAnnotationConstants;
import com.ly.doc.constants.JavaTypeConstants;
import com.ly.doc.constants.ParamTypeConstants;
import com.ly.doc.model.ApiConfig;
import com.ly.doc.model.ApiDataDictionary;
import com.ly.doc.model.DocJavaField;
Expand Down Expand Up @@ -404,7 +405,7 @@ public static Object getEnumValue(JavaClass javaClass, ProjectDocConfigBuilder b
}

// Default handling for enum values
return processDefaultEnumFields(javaClass.getEnumConstants(), formDataEnum);
return processDefaultEnumFields(javaClass.getEnumConstants(), formDataEnum, enumConstant);
}

/**
Expand Down Expand Up @@ -475,12 +476,17 @@ private static Optional<JavaField> findFieldWithJsonValue(JavaClass javaClass) {
* Handles the default logic for processing enum fields.
* @param javaFields The list of JavaField objects representing enum fields
* @param formDataEnum A boolean indicating if the enum is a form data enum
* @param enumConstant The JavaField object representing the enum constant
* @return The value based on the enum field processing logic
*/
private static Object processDefaultEnumFields(List<JavaField> javaFields, boolean formDataEnum) {
private static Object processDefaultEnumFields(List<JavaField> javaFields, boolean formDataEnum,
JavaField enumConstant) {
Object value = null;
int index = 0;
for (JavaField javaField : javaFields) {
if (!javaField.equals(enumConstant)) {
continue;
}
String simpleName = javaField.getType().getSimpleName();
StringBuilder valueBuilder = new StringBuilder();
valueBuilder.append("\"").append(javaField.getName()).append("\"");
Expand Down Expand Up @@ -638,15 +644,19 @@ public static EnumInfo getEnumInfo(JavaClass javaClass, ProjectDocConfigBuilder
String name = cons.getName();
String enumComment = cons.getComment();
item.setName(name);
if (formDataEnum) {
item.setValue(name);
item.setType("string");
}
else {

item.setValue(StringUtil.removeDoubleQuotes(name));
item.setType("string");
if (!formDataEnum) {
Object enumValue = getEnumValue(javaClass, builder, false, cons);
item.setValue(Objects.isNull(enumValue) ? null : String.valueOf(enumValue));
item.setType(Objects.isNull(enumValue) ? null
: DocClassUtil.processTypeNameForParams(enumValue.getClass().getCanonicalName()));
String stringValue = StringUtil.removeQuotes(String.valueOf(enumValue));
if (!StringUtils.equals(name, stringValue)) {
item.setValueObject(enumValue);
item.setValue(stringValue);
if (Objects.nonNull(enumValue)) {
item.setType(DocClassUtil.processTypeNameForParams(enumValue.getClass().getCanonicalName()));
}
}
}
item.setDescription(enumComment);
return item;
Expand Down Expand Up @@ -1465,12 +1475,22 @@ public static EnumInfoAndValues getEnumInfoAndValue(JavaClass javaClass, Project
// Step 2: Get the enum values based on whether it's formDataEnum or not
List<String> enumValues = enumInfo.getItems().stream().map(Item::getValue).collect(Collectors.toList());

Item item = enumInfo.getItems().get(0);
// Step 3: Create the EnumInfoAndValues result
return EnumInfoAndValues.builder()
.setEnumInfo(enumInfo)
Object valueObject = item.getValueObject();
EnumInfoAndValues result = EnumInfoAndValues.builder()
.setEnumValues(enumValues)
// Using the same method to get default value
.setValue(getEnumValue(javaClass, builder, formDataEnum));
.setValue(Objects.isNull(valueObject) ? item.getValue() : valueObject);

result.setType(ParamTypeConstants.PARAM_TYPE_ENUM);
if (!formDataEnum) {
if (Objects.nonNull(valueObject)) {
result.setType(DocClassUtil.processTypeNameForParams(valueObject.getClass().getCanonicalName()));
enumInfo.setName(enumInfo.getName() + " To " + result.getType());
}
}
return result.setEnumInfo(enumInfo);
}

}
3 changes: 2 additions & 1 deletion src/main/java/com/ly/doc/utils/ParamUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public static JavaClass handleSeeEnum(ApiParam param, JavaField javaField, Proje
EnumInfoAndValues enumInfoAndValue = JavaClassUtil.getEnumInfoAndValue(seeEnum, builder, !jsonRequest);
if (Objects.nonNull(enumInfoAndValue)) {
param.setValue(StringUtil.removeDoubleQuotes(String.valueOf(enumInfoAndValue.getValue())))
.setEnumInfoAndValues(enumInfoAndValue);
.setEnumInfoAndValues(enumInfoAndValue)
.setType(enumInfoAndValue.getType());
}
// If the @JsonFormat annotation's shape attribute value is specified, use it as
// the parameter value
Expand Down
8 changes: 0 additions & 8 deletions src/main/java/com/ly/doc/utils/TornaUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import com.ly.doc.model.torna.CommonErrorCode;
import com.ly.doc.model.torna.DebugEnv;
import com.ly.doc.model.torna.HttpParam;
import com.ly.doc.model.torna.Item;
import com.ly.doc.model.torna.TornaApi;
import com.ly.doc.model.torna.TornaDic;
import com.ly.doc.model.torna.TornaRequestInfo;
Expand Down Expand Up @@ -339,13 +338,6 @@ public static List<HttpParam> buildParams(List<ApiParam> apiParams) {
if (Objects.equals(type, ParamTypeConstants.PARAM_TYPE_FILE) && apiParam.isHasItems()) {
type = TornaConstants.PARAM_TYPE_FILE_ARRAY;
}
if (Objects.nonNull(apiParam.getEnumInfo())) {
// Get type from enum items if available, with null check for items
List<Item> items = apiParam.getEnumInfo().getItems();
if (Objects.nonNull(items) && !items.isEmpty()) {
type = items.stream().map(Item::getType).findFirst().orElse(type);
}
}

httpParam.setType(type);
httpParam.setVersion(apiParam.getVersion());
Expand Down

0 comments on commit 4d8e86f

Please sign in to comment.