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

Commit fe7f070

Browse files
committed
fix: 🐛 Support for @JsonSerialize using ToStringSerializer
- Update IRestDocTemplate.java with new process methods for API data and documentation generation.- Enhance JavaClassUtil.java and JsonBuildHelper.java to handle @JsonSerialize annotation with ToStringSerializer. - Refactor ParamsBuildHelper.java to support new serialization features. - Remove deprecated code and streamline documentation generation flow. See [issues #833](#833) See [issues #714](#714)
1 parent 9f9436f commit fe7f070

5 files changed

Lines changed: 229 additions & 22 deletions

File tree

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,18 @@ else if (JavaClassValidateUtil.isReactor(typeName)) {
271271
List<JavaAnnotation> annotations = docField.getAnnotations();
272272

273273
String jsonFormatString = null;
274+
// has Annotation @JsonSerialize And using ToStringSerializer
275+
boolean toStringSerializer = false;
274276
// Handle annotations on the field
275277
for (JavaAnnotation annotation : annotations) {
276278
String annotationName = annotation.getType().getValue();
279+
// if the field is annotated with @JsonSerialize
280+
if (DocAnnotationConstants.SHORT_JSON_SERIALIZE.equals(annotationName) &&
281+
DocAnnotationConstants.TO_STRING_SERIALIZER_USING.equals(annotation.getNamedParameter("using"))) {
282+
toStringSerializer = true;
283+
continue;
284+
}
285+
277286
if (JSRAnnotationConstants.NULL.equals(annotationName) && !isResp) {
278287
if (CollectionUtil.isEmpty(groupClasses)) {
279288
continue out;
@@ -340,9 +349,13 @@ else if (DocAnnotationConstants.JSON_FORMAT.equals(annotationName)) {
340349
if (JavaClassValidateUtil.isPrimitive(subTypeName)) {
341350
int data0Length = data0.length();
342351
if (StringUtil.isEmpty(fieldValue)) {
343-
fieldValue = StringUtil.isNotEmpty(jsonFormatString)
344-
? jsonFormatString
345-
: DocUtil.getValByTypeAndFieldName(typeSimpleName, field.getName());
352+
String valueByTypeAndFieldName = DocUtil.getValByTypeAndFieldName(typeSimpleName, field.getName());
353+
if (toStringSerializer && isResp) {
354+
fieldValue = valueByTypeAndFieldName.startsWith("\"") && valueByTypeAndFieldName.endsWith("\"")
355+
? valueByTypeAndFieldName : DocUtil.handleJsonStr(valueByTypeAndFieldName);
356+
} else {
357+
fieldValue = StringUtil.isNotEmpty(jsonFormatString) ? jsonFormatString : valueByTypeAndFieldName;
358+
}
346359
}
347360
if (Objects.nonNull(customRequestField) && !isResp && typeName.equals(customRequestField.getOwnerClassName())) {
348361
JavaFieldUtil.buildCustomField(data0, typeSimpleName, customRequestField);
@@ -479,6 +492,11 @@ else if (JavaTypeConstants.JAVA_OBJECT_FULLY.equals(fieldGicName)) {
479492
if (tagsMap.containsKey(DocTags.MOCK) && StringUtil.isNotEmpty(tagsMap.get(DocTags.MOCK))) {
480493
data0.append(tagsMap.get(DocTags.MOCK)).append(",");
481494
}
495+
// if has Annotation @JsonSerialize And using ToStringSerializer && isResp
496+
else if (toStringSerializer && isResp) {
497+
Object value = JavaClassUtil.getEnumValue(javaClass, Boolean.FALSE);
498+
data0.append(value).append(",");
499+
}
482500
// if has @JsonFormat
483501
else if (StringUtil.isNotEmpty(jsonFormatString)) {
484502
data0.append(jsonFormatString).append(",");
@@ -487,7 +505,10 @@ else if (StringUtil.isNotEmpty(jsonFormatString)) {
487505
data0.append(value).append(",");
488506
}
489507
} else {
490-
if (StringUtil.isNotEmpty(jsonFormatString)) {
508+
// if has Annotation @JsonSerialize And using ToStringSerializer && isResp
509+
if (toStringSerializer && isResp) {
510+
data0.append(" ").append(",");
511+
} else if (StringUtil.isNotEmpty(jsonFormatString)) {
491512
data0.append(jsonFormatString).append(",");
492513
} else {
493514
fieldGicName = DocUtil.formatFieldTypeGicName(genericMap, fieldGicName);

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,14 @@ public static List<ApiParam> buildParams(String className, String pre, int level
192192
continue;
193193
}
194194
String fieldJsonFormatType = null;
195+
// has Annotation @JsonSerialize And using ToStringSerializer
196+
boolean toStringSerializer = false;
195197
for (JavaAnnotation annotation : javaAnnotations) {
198+
if (DocAnnotationConstants.SHORT_JSON_SERIALIZE.equals(annotation.getType().getSimpleName()) &&
199+
DocAnnotationConstants.TO_STRING_SERIALIZER_USING.equals(annotation.getNamedParameter("using"))) {
200+
toStringSerializer = true;
201+
continue;
202+
}
196203
if (JavaClassValidateUtil.isIgnoreFieldJson(annotation, isResp)) {
197204
continue out;
198205
}
@@ -298,7 +305,7 @@ public static List<ApiParam> buildParams(String className, String pre, int level
298305
ApiParam param = ApiParam.of().setClassName(className).setField(pre + fieldName);
299306
param.setPid(pid).setMaxLength(maxLength).setValue(fieldValue);
300307
param.setId(atomicOrDefault(atomicInteger, paramList.size() + param.getPid() + 1));
301-
String processedType = StringUtil.isNotEmpty(fieldJsonFormatType)
308+
String processedType = (isResp && toStringSerializer) ? "string" : StringUtil.isNotEmpty(fieldJsonFormatType)
302309
? fieldJsonFormatType
303310
: processFieldTypeName(isShowJavaType, subTypeName);
304311
param.setType(processedType);

0 commit comments

Comments
 (0)