From 4d8e86f474f18e4117fbcd202ea8c54796db6b82 Mon Sep 17 00:00:00 2001 From: linwumingshi Date: Fri, 15 Nov 2024 21:17:20 +0800 Subject: [PATCH] refactor (Enum): Optimize the handling logic for enum parameter types - 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`. --- .../com/ly/doc/helper/ParamsBuildHelper.java | 3 +- .../ly/doc/model/torna/EnumInfoAndValues.java | 15 ++++++ .../java/com/ly/doc/model/torna/Item.java | 18 ++++++++ .../com/ly/doc/template/IRestDocTemplate.java | 22 +++++---- .../java/com/ly/doc/utils/JavaClassUtil.java | 46 +++++++++++++------ src/main/java/com/ly/doc/utils/ParamUtil.java | 3 +- src/main/java/com/ly/doc/utils/TornaUtil.java | 8 ---- 7 files changed, 84 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/ly/doc/helper/ParamsBuildHelper.java b/src/main/java/com/ly/doc/helper/ParamsBuildHelper.java index de4278eb..7ddf07f2 100644 --- a/src/main/java/com/ly/doc/helper/ParamsBuildHelper.java +++ b/src/main/java/com/ly/doc/helper/ParamsBuildHelper.java @@ -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) { diff --git a/src/main/java/com/ly/doc/model/torna/EnumInfoAndValues.java b/src/main/java/com/ly/doc/model/torna/EnumInfoAndValues.java index 10724f58..12a9ef82 100644 --- a/src/main/java/com/ly/doc/model/torna/EnumInfoAndValues.java +++ b/src/main/java/com/ly/doc/model/torna/EnumInfoAndValues.java @@ -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(); } @@ -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; + } + } diff --git a/src/main/java/com/ly/doc/model/torna/Item.java b/src/main/java/com/ly/doc/model/torna/Item.java index 16f441c7..e4675b38 100644 --- a/src/main/java/com/ly/doc/model/torna/Item.java +++ b/src/main/java/com/ly/doc/model/torna/Item.java @@ -20,6 +20,8 @@ */ package com.ly.doc.model.torna; +import com.google.gson.annotations.Expose; + import java.io.Serializable; /** @@ -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() { } @@ -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() { @@ -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; + } + } diff --git a/src/main/java/com/ly/doc/template/IRestDocTemplate.java b/src/main/java/com/ly/doc/template/IRestDocTemplate.java index 0847cb20..d18a66a4 100644 --- a/src/main/java/com/ly/doc/template/IRestDocTemplate.java +++ b/src/main/java/com/ly/doc/template/IRestDocTemplate.java @@ -252,11 +252,11 @@ default String createDocRenderHeaders(List 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 apiDocList, List apiMethodDocs, int order, - boolean isUseMD5) { + boolean isUseMd5) { String controllerName = cls.getName(); ApiDoc apiDoc = new ApiDoc(); String classAuthor = JavaClassUtil.getClassTagsValue(cls, DocTags.AUTHOR, Boolean.TRUE); @@ -277,7 +277,7 @@ default void handleApiDoc(JavaClass cls, List apiDocList, List 0) { @@ -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); } diff --git a/src/main/java/com/ly/doc/utils/JavaClassUtil.java b/src/main/java/com/ly/doc/utils/JavaClassUtil.java index 8d5529dc..d4b12482 100644 --- a/src/main/java/com/ly/doc/utils/JavaClassUtil.java +++ b/src/main/java/com/ly/doc/utils/JavaClassUtil.java @@ -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; @@ -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); } /** @@ -475,12 +476,17 @@ private static Optional 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 javaFields, boolean formDataEnum) { + private static Object processDefaultEnumFields(List 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("\""); @@ -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; @@ -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 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); } } diff --git a/src/main/java/com/ly/doc/utils/ParamUtil.java b/src/main/java/com/ly/doc/utils/ParamUtil.java index b2cddebb..51e7e363 100644 --- a/src/main/java/com/ly/doc/utils/ParamUtil.java +++ b/src/main/java/com/ly/doc/utils/ParamUtil.java @@ -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 diff --git a/src/main/java/com/ly/doc/utils/TornaUtil.java b/src/main/java/com/ly/doc/utils/TornaUtil.java index bfbee576..38173506 100644 --- a/src/main/java/com/ly/doc/utils/TornaUtil.java +++ b/src/main/java/com/ly/doc/utils/TornaUtil.java @@ -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; @@ -339,13 +338,6 @@ public static List buildParams(List 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 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());