From 66f448ff4b09c6b12327784ee89c51039c4253fa Mon Sep 17 00:00:00 2001 From: shalousun <836575280@qq.com> Date: Thu, 5 Sep 2024 21:05:14 +0800 Subject: [PATCH] feat: Fix the issue where the JsonValue annotation cannot be used on an enum. --- pom.xml | 4 +-- .../ly/doc/helper/FormDataBuildHelper.java | 2 +- .../com/ly/doc/helper/JsonBuildHelper.java | 11 ++++---- .../com/ly/doc/helper/ParamsBuildHelper.java | 3 ++- .../com/ly/doc/template/IRestDocTemplate.java | 12 ++++----- .../doc/template/JAXRSDocBuildTemplate.java | 8 +++--- .../java/com/ly/doc/utils/JavaClassUtil.java | 27 +++++++++++++------ src/main/java/com/ly/doc/utils/ParamUtil.java | 2 +- 8 files changed, 41 insertions(+), 28 deletions(-) diff --git a/pom.xml b/pom.xml index 9d3ff1e1..6960598b 100644 --- a/pom.xml +++ b/pom.xml @@ -59,9 +59,9 @@ 1.4.0 - com.github.shalousun + io.github.shalousun common-util - 2.2.3 + 2.2.4 com.google.code.gson diff --git a/src/main/java/com/ly/doc/helper/FormDataBuildHelper.java b/src/main/java/com/ly/doc/helper/FormDataBuildHelper.java index e1916f30..c5a866ec 100644 --- a/src/main/java/com/ly/doc/helper/FormDataBuildHelper.java +++ b/src/main/java/com/ly/doc/helper/FormDataBuildHelper.java @@ -172,7 +172,7 @@ else if (JavaClassValidateUtil.isPrimitive(subTypeName)) { formDataList.add(formData); } else if (javaClass.isEnum()) { - Object value = JavaClassUtil.getEnumValue(javaClass, Boolean.TRUE); + Object value = JavaClassUtil.getEnumValue(javaClass, builder, Boolean.TRUE); if (tagsMap.containsKey(DocTags.MOCK) && StringUtil.isNotEmpty(tagsMap.get(DocTags.MOCK))) { value = ParamUtil.formatMockValue(tagsMap.get(DocTags.MOCK)); } diff --git a/src/main/java/com/ly/doc/helper/JsonBuildHelper.java b/src/main/java/com/ly/doc/helper/JsonBuildHelper.java index c939c7b9..a0457f27 100644 --- a/src/main/java/com/ly/doc/helper/JsonBuildHelper.java +++ b/src/main/java/com/ly/doc/helper/JsonBuildHelper.java @@ -59,7 +59,7 @@ public static String buildReturnJson(DocJavaMethod docJavaMethod, ProjectDocConf } if (method.getReturns().isEnum() && Objects.isNull(responseBodyAdvice)) { return StringUtil - .removeQuotes(String.valueOf(JavaClassUtil.getEnumValue(method.getReturns(), Boolean.FALSE))); + .removeQuotes(String.valueOf(JavaClassUtil.getEnumValue(method.getReturns(), builder, Boolean.FALSE))); } if (method.getReturns().isPrimitive() && Objects.isNull(responseBodyAdvice)) { String typeName = method.getReturnType().getCanonicalName(); @@ -150,7 +150,8 @@ public static String buildJson(String typeName, String genericCanonicalName, boo // Handle enum types if (javaClass.isEnum()) { - return StringUtil.removeQuotes(String.valueOf(JavaClassUtil.getEnumValue(javaClass, Boolean.FALSE))); + return StringUtil + .removeQuotes(String.valueOf(JavaClassUtil.getEnumValue(javaClass, builder, Boolean.FALSE))); } StringBuilder data0 = new StringBuilder(); @@ -493,7 +494,7 @@ else if (gicName.length() == 1) { } JavaClass arraySubClass = builder.getJavaProjectBuilder().getClassByName(gicName); if (arraySubClass.isEnum()) { - Object value = JavaClassUtil.getEnumValue(arraySubClass, Boolean.FALSE); + Object value = JavaClassUtil.getEnumValue(arraySubClass, builder, Boolean.FALSE); data0.append("[").append(value).append("],"); continue; } @@ -603,7 +604,7 @@ else if (typeName.equals(fieldGicName)) { // if has Annotation @JsonSerialize And using // ToStringSerializer && isResp else if (toStringSerializer && isResp) { - Object value = JavaClassUtil.getEnumValue(javaClass, Boolean.FALSE); + Object value = JavaClassUtil.getEnumValue(javaClass, builder, Boolean.FALSE); data0.append(value).append(","); } // if has @JsonFormat @@ -611,7 +612,7 @@ else if (StringUtil.isNotEmpty(jsonFormatString)) { data0.append(jsonFormatString).append(","); } else { - Object value = JavaClassUtil.getEnumValue(javaClass, Boolean.FALSE); + Object value = JavaClassUtil.getEnumValue(javaClass, builder, Boolean.FALSE); data0.append(value).append(","); } } diff --git a/src/main/java/com/ly/doc/helper/ParamsBuildHelper.java b/src/main/java/com/ly/doc/helper/ParamsBuildHelper.java index dedb7dd5..f8e8dbde 100644 --- a/src/main/java/com/ly/doc/helper/ParamsBuildHelper.java +++ b/src/main/java/com/ly/doc/helper/ParamsBuildHelper.java @@ -520,7 +520,8 @@ else if (JavaClassValidateUtil.isCollection(subTypeName) if (!simpleName.equals(gName)) { JavaClass arraySubClass = projectBuilder.getJavaProjectBuilder().getClassByName(gName); if (arraySubClass.isEnum()) { - Object value = JavaClassUtil.getEnumValue(arraySubClass, Boolean.FALSE); + Object value = JavaClassUtil.getEnumValue(arraySubClass, projectBuilder, + Boolean.FALSE); param.setValue("[\"" + value + "\"]") .setEnumInfo(JavaClassUtil.getEnumInfo(arraySubClass, projectBuilder)) .setEnumValues(JavaClassUtil.getEnumValues(arraySubClass)); diff --git a/src/main/java/com/ly/doc/template/IRestDocTemplate.java b/src/main/java/com/ly/doc/template/IRestDocTemplate.java index d3df5c97..dfd10be0 100644 --- a/src/main/java/com/ly/doc/template/IRestDocTemplate.java +++ b/src/main/java/com/ly/doc/template/IRestDocTemplate.java @@ -1064,7 +1064,7 @@ default ApiMethodReqParam requestParams(final DocJavaMethod docJavaMethod, Proje } JavaClass gicJavaClass = builder.getJavaProjectBuilder().getClassByName(gicName); if (gicJavaClass.isEnum()) { - Object value = JavaClassUtil.getEnumValue(gicJavaClass, Boolean.TRUE); + Object value = JavaClassUtil.getEnumValue(gicJavaClass, builder, Boolean.TRUE); ApiParam param = ApiParam.of() .setField(paramName) .setDesc(comment + ",[array of enum]") @@ -1209,7 +1209,7 @@ else if (Objects.nonNull(mapKeyClass) && mapKeyClass.isEnum() // param is enum else if (javaClass.isEnum()) { String enumName = JavaClassUtil.getEnumParams(javaClass); - Object value = JavaClassUtil.getEnumValue(javaClass, isPathVariable || queryParam); + Object value = JavaClassUtil.getEnumValue(javaClass, builder, isPathVariable || queryParam); if (Boolean.TRUE.equals(builder.getApiConfig().getInlineEnum())) { comment.append("
[Enum: ").append(StringUtil.removeQuotes(enumName)).append("]"); } @@ -1399,7 +1399,7 @@ else if (frameworkAnnotations.getPathVariableAnnotation() .getAnnotationName() .contains(annotationName)) { if (javaClass.isEnum()) { - Object value = JavaClassUtil.getEnumValue(javaClass, Boolean.TRUE); + Object value = JavaClassUtil.getEnumValue(javaClass, configBuilder, Boolean.TRUE); mockValue = StringUtil.removeQuotes(String.valueOf(value)); } if (pathParamsMap.containsKey(paramName)) { @@ -1412,7 +1412,7 @@ else if (frameworkAnnotations.getRequestParamAnnotation() .getAnnotationName() .contains(annotationName)) { if (javaClass.isEnum()) { - Object value = JavaClassUtil.getEnumValue(javaClass, Boolean.TRUE); + Object value = JavaClassUtil.getEnumValue(javaClass, configBuilder, Boolean.TRUE); mockValue = StringUtil.removeQuotes(String.valueOf(value)); } if (queryParamsMap.containsKey(paramName)) { @@ -1486,7 +1486,7 @@ else if (JavaClassValidateUtil.isArray(fullyQualifiedName) String value; JavaClass javaClass1 = configBuilder.getClassByName(gicName); if (Objects.nonNull(javaClass1) && javaClass1.isEnum()) { - value = String.valueOf(JavaClassUtil.getEnumValue(javaClass1, Boolean.TRUE)); + value = String.valueOf(JavaClassUtil.getEnumValue(javaClass1, configBuilder, Boolean.TRUE)); } else { value = RandomUtil.randomValueByType(gicName); @@ -1503,7 +1503,7 @@ else if (JavaClassValidateUtil.isArray(fullyQualifiedName) } else if (javaClass.isEnum()) { // do nothing - Object value = JavaClassUtil.getEnumValue(javaClass, Boolean.TRUE); + Object value = JavaClassUtil.getEnumValue(javaClass, configBuilder, Boolean.TRUE); String strVal = StringUtil.removeQuotes(String.valueOf(value)); FormData formData = new FormData(); formData.setKey(paramName); diff --git a/src/main/java/com/ly/doc/template/JAXRSDocBuildTemplate.java b/src/main/java/com/ly/doc/template/JAXRSDocBuildTemplate.java index 1bfd3eea..6b9a8e27 100644 --- a/src/main/java/com/ly/doc/template/JAXRSDocBuildTemplate.java +++ b/src/main/java/com/ly/doc/template/JAXRSDocBuildTemplate.java @@ -401,7 +401,7 @@ private ApiMethodReqParam requestParams(final DocJavaMethod docJavaMethod, Proje } JavaClass gicJavaClass = builder.getJavaProjectBuilder().getClassByName(gicName); if (gicJavaClass.isEnum()) { - Object value = JavaClassUtil.getEnumValue(gicJavaClass, Boolean.TRUE); + Object value = JavaClassUtil.getEnumValue(gicJavaClass, builder, Boolean.TRUE); ApiParam param = ApiParam.of() .setField(paramName) .setDesc(comment + ",[array of enum]") @@ -511,7 +511,7 @@ else if (JavaClassValidateUtil.isFile(typeName)) { // param is enum else if (javaClass.isEnum()) { String o = JavaClassUtil.getEnumParams(javaClass); - Object value = JavaClassUtil.getEnumValue(javaClass, true); + Object value = JavaClassUtil.getEnumValue(javaClass, builder, true); ApiParam param = ApiParam.of() .setField(paramName) .setId(paramList.size() + 1) @@ -615,7 +615,7 @@ private ApiRequestExample buildReqJson(DocJavaMethod javaMethod, ApiMethodDoc ap || JakartaJaxrsAnnotations.JAXB_REST_PATH_FULLY.equals(annotationName) || JAXRSAnnotations.JAX_PATH_PARAM_FULLY.equals(annotationName)) { if (javaClass.isEnum()) { - Object value = JavaClassUtil.getEnumValue(javaClass, Boolean.TRUE); + Object value = JavaClassUtil.getEnumValue(javaClass, configBuilder, Boolean.TRUE); mockValue = StringUtil.removeQuotes(String.valueOf(value)); } pathParamsMap.put(paramName, mockValue); @@ -666,7 +666,7 @@ else if (JavaClassValidateUtil.isArray(fullyQualifiedName) } else if (javaClass.isEnum()) { // do nothing - Object value = JavaClassUtil.getEnumValue(javaClass, Boolean.TRUE); + Object value = JavaClassUtil.getEnumValue(javaClass, configBuilder, Boolean.TRUE); String strVal = StringUtil.removeQuotes(String.valueOf(value)); FormData formData = new FormData(); formData.setDescription(comment); diff --git a/src/main/java/com/ly/doc/utils/JavaClassUtil.java b/src/main/java/com/ly/doc/utils/JavaClassUtil.java index 54d7468d..32f57053 100644 --- a/src/main/java/com/ly/doc/utils/JavaClassUtil.java +++ b/src/main/java/com/ly/doc/utils/JavaClassUtil.java @@ -312,7 +312,7 @@ public static String getSameSignatureMethodCommonFromInterface(JavaClass cls, Ja * @param formDataEnum is return method * @return Object */ - public static Object getEnumValue(JavaClass javaClass, boolean formDataEnum) { + public static Object getEnumValue(JavaClass javaClass, ProjectDocConfigBuilder builder, boolean formDataEnum) { List javaFields = javaClass.getEnumConstants(); if (Objects.isNull(javaFields)) { throw new RuntimeException(javaClass.getName() + " enum not existed"); @@ -331,6 +331,23 @@ public static Object getEnumValue(JavaClass javaClass, boolean formDataEnum) { } } } + if (Objects.nonNull(methodName)) { + ApiConfig apiConfig = builder.getApiConfig(); + ClassLoader classLoader = apiConfig.getClassLoader(); + Class enumClass = null; + try { + if (Objects.nonNull(classLoader)) { + enumClass = classLoader.loadClass(javaClass.getFullyQualifiedName()); + } + else { + enumClass = Class.forName(javaClass.getFullyQualifiedName()); + } + } + catch (ClassNotFoundException e) { + e.printStackTrace(); + } + return EnumUtil.getFieldValueByMethod(enumClass, methodName); + } Object value = null; int index = 0; for (JavaField javaField : javaFields) { @@ -342,13 +359,7 @@ public static Object getEnumValue(JavaClass javaClass, boolean formDataEnum) { return value; } if (!JavaClassValidateUtil.isPrimitive(simpleName) && index < 1) { - if (CollectionUtil.isNotEmpty(javaField.getEnumConstantArguments()) && Objects.nonNull(methodName)) { - // enum serialize while use JsonValue - value = javaField.getEnumConstantArguments().get(0); - } - else { - value = valueBuilder.toString(); - } + value = valueBuilder.toString(); } index++; } diff --git a/src/main/java/com/ly/doc/utils/ParamUtil.java b/src/main/java/com/ly/doc/utils/ParamUtil.java index cabf0f51..0d2353cc 100644 --- a/src/main/java/com/ly/doc/utils/ParamUtil.java +++ b/src/main/java/com/ly/doc/utils/ParamUtil.java @@ -52,7 +52,7 @@ public static JavaClass handleSeeEnum(ApiParam param, JavaField javaField, Proje javaField.getType().getGenericFullyQualifiedName())) { param.setType(ParamTypeConstants.PARAM_TYPE_ENUM); } - Object value = JavaClassUtil.getEnumValue(seeEnum, !jsonRequest); + Object value = JavaClassUtil.getEnumValue(seeEnum, builder, !jsonRequest); param.setValue(StringUtil.removeDoubleQuotes(String.valueOf(value))); param.setEnumValues(JavaClassUtil.getEnumValues(seeEnum)); param.setEnumInfo(JavaClassUtil.getEnumInfo(seeEnum, builder));