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

Commit 217a0c2

Browse files
authored
Merge pull request #836 from linwumingshi/fix/enum-format-number
fix(openapi): 🐛 support @jsonformat(shape = JsonFormat.Shape.NUMBER) annotation for enum types example
2 parents 5c8b5b0 + 359b8a8 commit 217a0c2

3 files changed

Lines changed: 43 additions & 7 deletions

File tree

src/main/java/com/ly/doc/builder/openapi/AbstractOpenApiBuilder.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
import java.util.*;
4141
import java.util.stream.Collectors;
4242

43-
import static com.ly.doc.constants.DocGlobalConstants.*;
43+
import static com.ly.doc.constants.DocGlobalConstants.OPENAPI_2_COMPONENT_KRY;
44+
import static com.ly.doc.constants.DocGlobalConstants.OPENAPI_3_COMPONENT_KRY;
4445

4546

4647
/**
@@ -162,6 +163,7 @@ public Map<String, Object> buildPaths(ApiConfig apiConfig, ApiSchema<ApiDoc> api
162163
* @param apiConfig ApiConfig
163164
* @param apiMethodDoc Method
164165
* @param apiDoc ApiDoc
166+
* @param apiExceptionStatuses Exception status list
165167
* @return Map of path urls
166168
*/
167169
public Map<String, Object> buildPathUrls(ApiConfig apiConfig, ApiMethodDoc apiMethodDoc, ApiDoc apiDoc, List<ApiExceptionStatus> apiExceptionStatuses) {

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,10 @@ public static List<ApiParam> buildParams(String className, String pre, int level
191191
&& (customRequestField.isIgnore()) && !isResp) {
192192
continue;
193193
}
194+
// the param type from @JsonFormat
194195
String fieldJsonFormatType = null;
196+
// the param value from @JsonFormat
197+
String fieldJsonFormatValue = null;
195198
// has Annotation @JsonSerialize And using ToStringSerializer
196199
boolean toStringSerializer = false;
197200
for (JavaAnnotation annotation : javaAnnotations) {
@@ -237,6 +240,7 @@ public static List<ApiParam> buildParams(String className, String pre, int level
237240
}
238241
} else if (DocAnnotationConstants.JSON_FORMAT.equals(simpleAnnotationName)) {
239242
fieldJsonFormatType = DocUtil.processFieldTypeNameByJsonFormat(isShowJavaType, subTypeName, annotation);
243+
fieldJsonFormatValue = DocUtil.getJsonFormatString(field, annotation);
240244
}
241245
}
242246
comment.append(JavaFieldUtil.getJsrComment(apiConfig.isShowValidation(), classLoader, javaAnnotations));
@@ -300,10 +304,16 @@ public static List<ApiParam> buildParams(String className, String pre, int level
300304
}
301305
if (JavaClassValidateUtil.isPrimitive(subTypeName)) {
302306
if (StringUtil.isEmpty(fieldValue)) {
303-
fieldValue = StringUtil.removeQuotes(DocUtil.getValByTypeAndFieldName(subTypeName, field.getName()));
307+
fieldValue = StringUtil.isNotEmpty(fieldJsonFormatValue)
308+
? fieldJsonFormatValue
309+
: StringUtil.removeQuotes(DocUtil.getValByTypeAndFieldName(subTypeName, field.getName()));
304310
}
305-
ApiParam param = ApiParam.of().setClassName(className).setField(pre + fieldName);
306-
param.setPid(pid).setMaxLength(maxLength).setValue(fieldValue);
311+
312+
ApiParam param = ApiParam.of().setClassName(className)
313+
.setField(pre + fieldName)
314+
.setPid(pid)
315+
.setMaxLength(maxLength)
316+
.setValue(fieldValue);
307317
param.setId(atomicOrDefault(atomicInteger, paramList.size() + param.getPid() + 1));
308318
String processedType = (isResp && toStringSerializer) ? "string" : StringUtil.isNotEmpty(fieldJsonFormatType)
309319
? fieldJsonFormatType
@@ -313,7 +323,7 @@ public static List<ApiParam> buildParams(String className, String pre, int level
313323
// handle param
314324
commonHandleParam(paramList, param, isRequired, comment.toString(), since, strRequired);
315325

316-
JavaClass enumClass = ParamUtil.handleSeeEnum(param, field, projectBuilder, jsonRequest, tagsMap);
326+
JavaClass enumClass = ParamUtil.handleSeeEnum(param, field, projectBuilder, jsonRequest, tagsMap, fieldJsonFormatValue);
317327
if (Objects.nonNull(enumClass)) {
318328
String enumClassComment = DocGlobalConstants.EMPTY;
319329
if (StringUtil.isNotEmpty(enumClass.getComment())) {
@@ -370,7 +380,7 @@ public static List<ApiParam> buildParams(String className, String pre, int level
370380
JavaClass javaClass = field.getType();
371381
if (javaClass.isEnum()) {
372382
comment.append(handleEnumComment(javaClass, projectBuilder));
373-
ParamUtil.handleSeeEnum(param, field, projectBuilder, jsonRequest, tagsMap);
383+
ParamUtil.handleSeeEnum(param, field, projectBuilder, jsonRequest, tagsMap, fieldJsonFormatValue);
374384
// hand Param
375385
commonHandleParam(paramList, param, isRequired, comment + appendComment, since, strRequired);
376386
} else if (JavaClassValidateUtil.isCollection(subTypeName) || JavaClassValidateUtil.isArray(subTypeName)) {

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,32 @@
1010
import com.thoughtworks.qdox.model.JavaField;
1111

1212
import java.util.*;
13+
import java.util.stream.Collectors;
14+
import java.util.stream.IntStream;
1315

1416
/**
1517
* @author <a href="mailto:cqmike0315@gmail.com">chenqi</a>
1618
* @version 1.0
1719
*/
1820
public class ParamUtil {
1921

22+
/**
23+
* Handles enum types in API parameters.
24+
* <p>
25+
* This method is primarily used to process enum types, setting the corresponding parameter type, value, and enumeration information in the ApiParam object.
26+
* If the enum has a @JsonFormat annotation with a shape attribute value of NUMBER, the value is processed accordingly.
27+
* Additionally, it supports setting mock values for parameters through tag mappings.
28+
*
29+
* @param param The ApiParam object, used to store parameter information.
30+
* @param javaField The JavaField object, representing the field of a class, used to obtain the type and other information of the field.
31+
* @param builder The ProjectDocConfigBuilder object, used to obtain configuration information for generating documentation.
32+
* @param jsonRequest A boolean value indicating whether the request is in JSON format, used to determine how to obtain the enum value.
33+
* @param tagsMap A map containing tag names and their descriptions, used to override parameter values with mock data.
34+
* @param jsonFormatValue The value of the @JsonFormat annotation's shape attribute, used to handle special cases of numeric enums.
35+
* @return Returns the processed JavaClass object representing the enum, or null if the input field is not an enum.
36+
*/
2037
public static JavaClass handleSeeEnum(ApiParam param, JavaField javaField, ProjectDocConfigBuilder builder, boolean jsonRequest,
21-
Map<String, String> tagsMap) {
38+
Map<String, String> tagsMap, String jsonFormatValue) {
2239
JavaClass seeEnum = JavaClassUtil.getSeeEnum(javaField, builder);
2340
if (Objects.isNull(seeEnum)) {
2441
return null;
@@ -32,6 +49,13 @@ public static JavaClass handleSeeEnum(ApiParam param, JavaField javaField, Proje
3249
param.setValue(StringUtil.removeDoubleQuotes(String.valueOf(value)));
3350
param.setEnumValues(JavaClassUtil.getEnumValues(seeEnum));
3451
param.setEnumInfo(JavaClassUtil.getEnumInfo(seeEnum, builder));
52+
// If the @JsonFormat annotation's shape attribute value is specified, use it as the parameter value
53+
if (StringUtil.isNotEmpty(jsonFormatValue)) {
54+
param.setValue(jsonFormatValue);
55+
param.setEnumValues(IntStream.rangeClosed(0, param.getEnumValues().size() - 1)
56+
.mapToObj(Integer::toString).collect(Collectors.toList()));
57+
}
58+
// If the tagsMap contains a mock tag and the value is not empty, use the value of the mock tag as the parameter value
3559
// Override old value
3660
if (tagsMap.containsKey(DocTags.MOCK) && StringUtil.isNotEmpty(tagsMap.get(DocTags.MOCK))) {
3761
param.setValue(tagsMap.get(DocTags.MOCK));

0 commit comments

Comments
 (0)