3030import com .ly .doc .constants .DocValidatorAnnotationEnum ;
3131import com .ly .doc .constants .JSRAnnotationConstants ;
3232import com .ly .doc .constants .JavaTypeConstants ;
33+ import com .ly .doc .constants .ParamTypeConstants ;
3334import com .ly .doc .model .ApiConfig ;
3435import com .ly .doc .model .ApiDataDictionary ;
3536import com .ly .doc .model .DocJavaField ;
5657import com .thoughtworks .qdox .model .expression .TypeRef ;
5758import com .thoughtworks .qdox .model .impl .DefaultJavaField ;
5859import com .thoughtworks .qdox .model .impl .DefaultJavaParameterizedType ;
60+ import net .datafaker .BojackHorseman ;
5961import org .apache .commons .lang3 .StringUtils ;
6062
6163import java .lang .reflect .Field ;
@@ -351,11 +353,12 @@ public static String getSameSignatureMethodCommonFromInterface(JavaClass cls, Ja
351353 * @param javaClass The JavaClass object representing the enum class
352354 * @param builder A ProjectDocConfigBuilder object used to retrieve API configuration
353355 * and the class loader
356+ * @param jsonEnum Enum is json or not
354357 * @return Object The enum value, whose type depends on the specific enum definition
355358 * @throws RuntimeException If the enum constants do not exist
356359 */
357- public static Object getEnumValue (JavaClass javaClass , ProjectDocConfigBuilder builder ) {
358- EnumInfoAndValues enumInfoAndValue = getEnumInfoAndValue (javaClass , builder );
360+ public static Object getEnumValue (JavaClass javaClass , ProjectDocConfigBuilder builder , boolean jsonEnum ) {
361+ EnumInfoAndValues enumInfoAndValue = getEnumInfoAndValue (javaClass , builder , jsonEnum );
359362 if (enumInfoAndValue == null ) {
360363 return null ;
361364 }
@@ -367,6 +370,30 @@ public static Object getEnumValue(JavaClass javaClass, ProjectDocConfigBuilder b
367370 /**
368371 * Get the value of an enumConstant
369372 * <p>
373+ * First look for it using the @JsonValue annotation, and if you can't find it, look
374+ * for the default value of the enumeration field
375+ * @param javaClass The JavaClass object representing the enum class
376+ * @param builder A ProjectDocConfigBuilder object used to retrieve API configuration
377+ * and the class loader
378+ * @param enumConstant The JavaField object representing the enum constant
379+ * @return Object The enum value, whose type depends on the specific enum definition
380+ * @throws RuntimeException If the enum constants do not exist
381+ */
382+ public static Object getDefaultEnumValue (JavaClass javaClass , ProjectDocConfigBuilder builder ,
383+ JavaField enumConstant ) {
384+ // Try getting value from method with JsonValue annotation
385+ Object enumValue = getEnumValueWithJsonValue (javaClass , builder , enumConstant );
386+ if (enumValue != null ) {
387+ return enumValue ;
388+ }
389+
390+ // Default handling for enum values
391+ return processDefaultEnumFields (enumConstant );
392+ }
393+
394+ /**
395+ * Get the value with @JsonValue annotation
396+ * <p>
370397 * This method retrieves the value of an enum based on its fields or methods. It
371398 * supports loading the enum class via reflection and determines the enum value based
372399 * on the presence of specific annotations such as {@code JsonValue}
@@ -377,9 +404,8 @@ public static Object getEnumValue(JavaClass javaClass, ProjectDocConfigBuilder b
377404 * @return Object The enum value, whose type depends on the specific enum definition
378405 * @throws RuntimeException If the enum constants do not exist
379406 */
380- public static Object getDefaultEnumValue (JavaClass javaClass , ProjectDocConfigBuilder builder ,
407+ private static Object getEnumValueWithJsonValue (JavaClass javaClass , ProjectDocConfigBuilder builder ,
381408 JavaField enumConstant ) {
382- // Try getting value from method with JsonValue annotation
383409 String methodName = findMethodWithJsonValue (javaClass );
384410 if (Objects .nonNull (methodName )) {
385411 Class <?> enumClass = loadEnumClass (javaClass , builder );
@@ -399,8 +425,7 @@ public static Object getDefaultEnumValue(JavaClass javaClass, ProjectDocConfigBu
399425 return null ;
400426 }
401427
402- // Default handling for enum values
403- return processDefaultEnumFields (enumConstant );
428+ return null ;
404429 }
405430
406431 /**
@@ -1399,10 +1424,12 @@ public static List<AnnotationValue> getAnnotationValues(JavaAnnotation javaAnnot
13991424 * @param javaClass The Java class object representing the enum.
14001425 * @param builder The project documentation configuration builder, used to access
14011426 * project-specific documentation settings.
1427+ * @param jsonEnum Whether it is an enum in JSON
14021428 * @return An EnumInfoAndValues object containing both the enum information and its
14031429 * values.
14041430 */
1405- public static EnumInfoAndValues getEnumInfoAndValue (JavaClass javaClass , ProjectDocConfigBuilder builder ) {
1431+ public static EnumInfoAndValues getEnumInfoAndValue (JavaClass javaClass , ProjectDocConfigBuilder builder ,
1432+ boolean jsonEnum ) {
14061433 // Step 1: Retrieve EnumInfo (general enum info like name, description, etc.)
14071434 EnumInfo enumInfo = getEnumInfo (javaClass , builder );
14081435
@@ -1411,7 +1438,7 @@ public static EnumInfoAndValues getEnumInfoAndValue(JavaClass javaClass, Project
14111438 return null ;
14121439 }
14131440
1414- return generateEnumInfoAndValues (enumInfo , builder );
1441+ return generateEnumInfoAndValues (enumInfo , javaClass , builder , jsonEnum );
14151442 }
14161443
14171444 /**
@@ -1451,7 +1478,7 @@ private static List<Item> getEnumItemList(JavaClass enumClass, ApiDataDictionary
14511478 String enumComment = cons .getComment ();
14521479 item .setName (name );
14531480 item .setValue (name );
1454- item .setType ("string" );
1481+ item .setType (ParamTypeConstants . PARAM_TYPE_ENUM );
14551482 item .setDescription (enumComment );
14561483
14571484 Object defaultEnumValue = getDefaultEnumValue (enumClass , builder , cons );
@@ -1473,26 +1500,41 @@ private static List<Item> getEnumItemList(JavaClass enumClass, ApiDataDictionary
14731500 * Generate EnumInfoAndValues, and determine the enumNameExample configuration in
14741501 * apiConfig
14751502 * @param enumInfo the enum info
1503+ * @param javaClass The Java class object representing the enum.
14761504 * @param builder builder
1505+ * @param jsonEnum Whether it is an enum in JSON
14771506 * @return List<Item>
14781507 * @author JasonKung22
14791508 */
1480- private static EnumInfoAndValues generateEnumInfoAndValues (EnumInfo enumInfo , ProjectDocConfigBuilder builder ) {
1509+ private static EnumInfoAndValues generateEnumInfoAndValues (EnumInfo enumInfo , JavaClass javaClass ,
1510+ ProjectDocConfigBuilder builder , boolean jsonEnum ) {
14811511 ApiConfig apiConfig = builder .getApiConfig ();
14821512 List <Item > items = enumInfo .getItems ();
1483- String enumValue ;
1484- List <String > enumValues ;
1485- String type ;
1486- if (apiConfig .isEnumNameExample ()) {
1487- enumValues = items .stream ().map (Item ::getName ).collect (Collectors .toList ());
1488- enumValue = enumValues .get (0 );
1489- type = "string" ;
1513+ String enumValue = null ;
1514+ List <String > enumValues = null ;
1515+ String type = null ;
1516+
1517+ if (jsonEnum || apiConfig .isEnumConvertor ()) {
1518+ List <JavaField > enumConstants = javaClass .getEnumConstants ();
1519+ List <Object > enumValueList = enumConstants .stream ()
1520+ .map (cons -> getEnumValueWithJsonValue (javaClass , builder , cons ))
1521+ .filter (Objects ::nonNull )
1522+ .collect (Collectors .toList ());
1523+
1524+ if (CollectionUtil .isNotEmpty (enumValueList )) {
1525+ Object enumValueWithJsonValue = enumValueList .get (0 );
1526+ type = DocClassUtil .processTypeNameForParams (enumValueWithJsonValue .getClass ().getCanonicalName ());
1527+ enumValue = String .valueOf (enumValueWithJsonValue );
1528+ enumValues = enumValueList .stream ().map (String ::valueOf ).collect (Collectors .toList ());
1529+ }
14901530 }
1491- else {
1492- enumValues = items .stream ().map (Item ::getValue ).collect (Collectors .toList ());
1531+
1532+ if (enumValue == null ) {
1533+ enumValues = items .stream ().map (Item ::getName ).collect (Collectors .toList ());
14931534 enumValue = enumValues .get (0 );
1494- type = items . get ( 0 ). getType () ;
1535+ type = ParamTypeConstants . PARAM_TYPE_ENUM ;
14951536 }
1537+
14961538 return EnumInfoAndValues .builder ()
14971539 .setEnumInfo (enumInfo )
14981540 .setEnumValues (enumValues )
0 commit comments