Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Fix the issue where the JsonValue annotation cannot be used on an enum. #910

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>com.github.shalousun</groupId>
<groupId>io.github.shalousun</groupId>
<artifactId>common-util</artifactId>
<version>2.2.3</version>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/ly/doc/helper/FormDataBuildHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/com/ly/doc/helper/JsonBuildHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -603,15 +604,15 @@ 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
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(",");
}
}
Expand Down
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 @@ -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));
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/ly/doc/template/IRestDocTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -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]")
Expand Down Expand Up @@ -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("<br/>[Enum: ").append(StringUtil.removeQuotes(enumName)).append("]");
}
Expand Down Expand Up @@ -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)) {
Expand All @@ -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)) {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/ly/doc/template/JAXRSDocBuildTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -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]")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
27 changes: 19 additions & 8 deletions src/main/java/com/ly/doc/utils/JavaClassUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<JavaField> javaFields = javaClass.getEnumConstants();
if (Objects.isNull(javaFields)) {
throw new RuntimeException(javaClass.getName() + " enum not existed");
Expand All @@ -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) {
Expand All @@ -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++;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/ly/doc/utils/ParamUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down