Skip to content
This repository was archived by the owner on Dec 4, 2025. It is now read-only.

Commit 4ab90e9

Browse files
authored
Merge pull request #954 from linwumingshi/reactor/enum
refactor (Enum): Optimize the handling logic for enum parameter types
2 parents c18ff43 + 4d8e86f commit 4ab90e9

7 files changed

Lines changed: 84 additions & 31 deletions

File tree

src/main/java/com/ly/doc/helper/ParamsBuildHelper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,8 @@ else if (JavaClassValidateUtil.isCollection(subTypeName)
509509
projectBuilder, Boolean.FALSE);
510510
if (Objects.nonNull(enumInfoAndValue)) {
511511
param.setValue("[\"" + enumInfoAndValue.getValue() + "\"]")
512-
.setEnumInfoAndValues(enumInfoAndValue);
512+
.setEnumInfoAndValues(enumInfoAndValue)
513+
.setType(enumInfoAndValue.getType());
513514
}
514515
}
515516
else if (gName.length() == 1) {

src/main/java/com/ly/doc/model/torna/EnumInfoAndValues.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ public class EnumInfoAndValues implements Serializable {
3636
*/
3737
private Object value;
3838

39+
/**
40+
* ApiParam value default enum; when type is not enum type, will set this type to
41+
* {@link ApiParam#setType(String)}
42+
*/
43+
private String type;
44+
3945
public static EnumInfoAndValues builder() {
4046
return new EnumInfoAndValues();
4147
}
@@ -67,4 +73,13 @@ public EnumInfoAndValues setValue(Object value) {
6773
return this;
6874
}
6975

76+
public String getType() {
77+
return type;
78+
}
79+
80+
public EnumInfoAndValues setType(String type) {
81+
this.type = type;
82+
return this;
83+
}
84+
7085
}

src/main/java/com/ly/doc/model/torna/Item.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
*/
2121
package com.ly.doc.model.torna;
2222

23+
import com.google.gson.annotations.Expose;
24+
2325
import java.io.Serializable;
2426

2527
/**
@@ -56,6 +58,13 @@ public class Item implements Serializable {
5658
*/
5759
private String description;
5860

61+
/**
62+
* valueObject; A temporary variable used to store the object form of the value. This
63+
* field will not be serialized or deserialized.
64+
*/
65+
@Expose(serialize = false, deserialize = false)
66+
private Object valueObject;
67+
5968
public Item() {
6069
}
6170

@@ -64,6 +73,7 @@ public Item(String name, String type, String value, String description) {
6473
this.type = type;
6574
this.value = value;
6675
this.description = description;
76+
this.valueObject = name;
6777
}
6878

6979
public String getName() {
@@ -98,4 +108,12 @@ public void setDescription(String description) {
98108
this.description = description;
99109
}
100110

111+
public Object getValueObject() {
112+
return valueObject;
113+
}
114+
115+
public void setValueObject(Object valueObject) {
116+
this.valueObject = valueObject;
117+
}
118+
101119
}

src/main/java/com/ly/doc/template/IRestDocTemplate.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,11 @@ default String createDocRenderHeaders(List<ApiReqParam> headers, boolean isAdoc)
252252
* @param apiMethodDocs A list of method documentation objects corresponding to the
253253
* methods within the class.
254254
* @param order The sorting order for the generated API documentation entry.
255-
* @param isUseMD5 Flag indicating whether to use MD5 hashing to generate a unique
255+
* @param isUseMd5 Flag indicating whether to use MD5 hashing to generate a unique
256256
* alias for the documented class.
257257
*/
258258
default void handleApiDoc(JavaClass cls, List<ApiDoc> apiDocList, List<ApiMethodDoc> apiMethodDocs, int order,
259-
boolean isUseMD5) {
259+
boolean isUseMd5) {
260260
String controllerName = cls.getName();
261261
ApiDoc apiDoc = new ApiDoc();
262262
String classAuthor = JavaClassUtil.getClassTagsValue(cls, DocTags.AUTHOR, Boolean.TRUE);
@@ -277,7 +277,7 @@ default void handleApiDoc(JavaClass cls, List<ApiDoc> apiDocList, List<ApiMethod
277277
String[] tags = tagSet.toArray(new String[] {});
278278
apiDoc.setTags(tags);
279279

280-
if (isUseMD5) {
280+
if (isUseMd5) {
281281
String name = DocUtil.generateId(apiDoc.getName());
282282
apiDoc.setAlias(name);
283283
}
@@ -1131,8 +1131,11 @@ default ApiMethodReqParam requestParams(final DocJavaMethod docJavaMethod, Proje
11311131
.setType(ParamTypeConstants.PARAM_TYPE_ARRAY);
11321132
EnumInfoAndValues enumInfoAndValue = JavaClassUtil.getEnumInfoAndValue(gicJavaClass, builder,
11331133
Boolean.TRUE);
1134-
param.setValue(StringUtil.removeDoubleQuotes(String.valueOf(enumInfoAndValue.getValue())))
1135-
.setEnumInfoAndValues(enumInfoAndValue);
1134+
if (Objects.nonNull(enumInfoAndValue)) {
1135+
param.setValue(StringUtil.removeDoubleQuotes(String.valueOf(enumInfoAndValue.getValue())))
1136+
.setEnumInfoAndValues(enumInfoAndValue)
1137+
.setType(enumInfoAndValue.getType());
1138+
}
11361139

11371140
paramList.add(param);
11381141
if (requestBodyCounter > 0) {
@@ -1237,9 +1240,12 @@ else if (javaClass.isEnum()) {
12371240
.setVersion(DocGlobalConstants.DEFAULT_VERSION);
12381241

12391242
EnumInfoAndValues enumInfoAndValue = JavaClassUtil.getEnumInfoAndValue(javaClass, builder,
1240-
isPathVariable || queryParam);
1241-
param.setValue(StringUtil.removeDoubleQuotes(String.valueOf(enumInfoAndValue.getValue())))
1242-
.setEnumInfoAndValues(enumInfoAndValue);
1243+
isPathVariable || queryParam || isRequestParam);
1244+
if (Objects.nonNull(enumInfoAndValue)) {
1245+
param.setValue(StringUtil.removeDoubleQuotes(String.valueOf(enumInfoAndValue.getValue())))
1246+
.setEnumInfoAndValues(enumInfoAndValue)
1247+
.setType(enumInfoAndValue.getType());
1248+
}
12431249

12441250
paramList.add(param);
12451251
}

src/main/java/com/ly/doc/utils/JavaClassUtil.java

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.ly.doc.constants.DocValidatorAnnotationEnum;
3131
import com.ly.doc.constants.JSRAnnotationConstants;
3232
import com.ly.doc.constants.JavaTypeConstants;
33+
import com.ly.doc.constants.ParamTypeConstants;
3334
import com.ly.doc.model.ApiConfig;
3435
import com.ly.doc.model.ApiDataDictionary;
3536
import com.ly.doc.model.DocJavaField;
@@ -404,7 +405,7 @@ public static Object getEnumValue(JavaClass javaClass, ProjectDocConfigBuilder b
404405
}
405406

406407
// Default handling for enum values
407-
return processDefaultEnumFields(javaClass.getEnumConstants(), formDataEnum);
408+
return processDefaultEnumFields(javaClass.getEnumConstants(), formDataEnum, enumConstant);
408409
}
409410

410411
/**
@@ -475,12 +476,17 @@ private static Optional<JavaField> findFieldWithJsonValue(JavaClass javaClass) {
475476
* Handles the default logic for processing enum fields.
476477
* @param javaFields The list of JavaField objects representing enum fields
477478
* @param formDataEnum A boolean indicating if the enum is a form data enum
479+
* @param enumConstant The JavaField object representing the enum constant
478480
* @return The value based on the enum field processing logic
479481
*/
480-
private static Object processDefaultEnumFields(List<JavaField> javaFields, boolean formDataEnum) {
482+
private static Object processDefaultEnumFields(List<JavaField> javaFields, boolean formDataEnum,
483+
JavaField enumConstant) {
481484
Object value = null;
482485
int index = 0;
483486
for (JavaField javaField : javaFields) {
487+
if (!javaField.equals(enumConstant)) {
488+
continue;
489+
}
484490
String simpleName = javaField.getType().getSimpleName();
485491
StringBuilder valueBuilder = new StringBuilder();
486492
valueBuilder.append("\"").append(javaField.getName()).append("\"");
@@ -638,15 +644,19 @@ public static EnumInfo getEnumInfo(JavaClass javaClass, ProjectDocConfigBuilder
638644
String name = cons.getName();
639645
String enumComment = cons.getComment();
640646
item.setName(name);
641-
if (formDataEnum) {
642-
item.setValue(name);
643-
item.setType("string");
644-
}
645-
else {
647+
648+
item.setValue(StringUtil.removeDoubleQuotes(name));
649+
item.setType("string");
650+
if (!formDataEnum) {
646651
Object enumValue = getEnumValue(javaClass, builder, false, cons);
647-
item.setValue(Objects.isNull(enumValue) ? null : String.valueOf(enumValue));
648-
item.setType(Objects.isNull(enumValue) ? null
649-
: DocClassUtil.processTypeNameForParams(enumValue.getClass().getCanonicalName()));
652+
String stringValue = StringUtil.removeQuotes(String.valueOf(enumValue));
653+
if (!StringUtils.equals(name, stringValue)) {
654+
item.setValueObject(enumValue);
655+
item.setValue(stringValue);
656+
if (Objects.nonNull(enumValue)) {
657+
item.setType(DocClassUtil.processTypeNameForParams(enumValue.getClass().getCanonicalName()));
658+
}
659+
}
650660
}
651661
item.setDescription(enumComment);
652662
return item;
@@ -1465,12 +1475,22 @@ public static EnumInfoAndValues getEnumInfoAndValue(JavaClass javaClass, Project
14651475
// Step 2: Get the enum values based on whether it's formDataEnum or not
14661476
List<String> enumValues = enumInfo.getItems().stream().map(Item::getValue).collect(Collectors.toList());
14671477

1478+
Item item = enumInfo.getItems().get(0);
14681479
// Step 3: Create the EnumInfoAndValues result
1469-
return EnumInfoAndValues.builder()
1470-
.setEnumInfo(enumInfo)
1480+
Object valueObject = item.getValueObject();
1481+
EnumInfoAndValues result = EnumInfoAndValues.builder()
14711482
.setEnumValues(enumValues)
14721483
// Using the same method to get default value
1473-
.setValue(getEnumValue(javaClass, builder, formDataEnum));
1484+
.setValue(Objects.isNull(valueObject) ? item.getValue() : valueObject);
1485+
1486+
result.setType(ParamTypeConstants.PARAM_TYPE_ENUM);
1487+
if (!formDataEnum) {
1488+
if (Objects.nonNull(valueObject)) {
1489+
result.setType(DocClassUtil.processTypeNameForParams(valueObject.getClass().getCanonicalName()));
1490+
enumInfo.setName(enumInfo.getName() + " To " + result.getType());
1491+
}
1492+
}
1493+
return result.setEnumInfo(enumInfo);
14741494
}
14751495

14761496
}

src/main/java/com/ly/doc/utils/ParamUtil.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ public static JavaClass handleSeeEnum(ApiParam param, JavaField javaField, Proje
7171
EnumInfoAndValues enumInfoAndValue = JavaClassUtil.getEnumInfoAndValue(seeEnum, builder, !jsonRequest);
7272
if (Objects.nonNull(enumInfoAndValue)) {
7373
param.setValue(StringUtil.removeDoubleQuotes(String.valueOf(enumInfoAndValue.getValue())))
74-
.setEnumInfoAndValues(enumInfoAndValue);
74+
.setEnumInfoAndValues(enumInfoAndValue)
75+
.setType(enumInfoAndValue.getType());
7576
}
7677
// If the @JsonFormat annotation's shape attribute value is specified, use it as
7778
// the parameter value

src/main/java/com/ly/doc/utils/TornaUtil.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import com.ly.doc.model.torna.CommonErrorCode;
4646
import com.ly.doc.model.torna.DebugEnv;
4747
import com.ly.doc.model.torna.HttpParam;
48-
import com.ly.doc.model.torna.Item;
4948
import com.ly.doc.model.torna.TornaApi;
5049
import com.ly.doc.model.torna.TornaDic;
5150
import com.ly.doc.model.torna.TornaRequestInfo;
@@ -339,13 +338,6 @@ public static List<HttpParam> buildParams(List<ApiParam> apiParams) {
339338
if (Objects.equals(type, ParamTypeConstants.PARAM_TYPE_FILE) && apiParam.isHasItems()) {
340339
type = TornaConstants.PARAM_TYPE_FILE_ARRAY;
341340
}
342-
if (Objects.nonNull(apiParam.getEnumInfo())) {
343-
// Get type from enum items if available, with null check for items
344-
List<Item> items = apiParam.getEnumInfo().getItems();
345-
if (Objects.nonNull(items) && !items.isEmpty()) {
346-
type = items.stream().map(Item::getType).findFirst().orElse(type);
347-
}
348-
}
349341

350342
httpParam.setType(type);
351343
httpParam.setVersion(apiParam.getVersion());

0 commit comments

Comments
 (0)