diff --git a/core/src/main/java/com/alibaba/fastjson2/util/BeanUtils.java b/core/src/main/java/com/alibaba/fastjson2/util/BeanUtils.java index 293dfb5037..47c34be9c0 100644 --- a/core/src/main/java/com/alibaba/fastjson2/util/BeanUtils.java +++ b/core/src/main/java/com/alibaba/fastjson2/util/BeanUtils.java @@ -696,13 +696,28 @@ public static String[] getEnumAnnotationNames(Class enumClass) { String fieldName = field.getName(); for (int i = 0; i < enumConstants.length; i++) { Enum e = enumConstants[i]; + final int enumIndex = i; String enumName = e.name(); if (fieldName.equals(enumName)) { - JSONField annotation = field.getAnnotation(JSONField.class); - if (annotation != null) { - String annotationName = annotation.name(); - if (annotationName.length() != 0 && !annotationName.equals(enumName)) { - annotationNames[i] = annotationName; + for (Annotation annotation : field.getAnnotations()) { + Class annotationType = annotation.annotationType(); + String annotationTypeName = annotationType.getName(); + if ("com.alibaba.fastjson2.annotation.JSONField".equals(annotationTypeName) + || "com.alibaba.fastjson.annotation.JSONField".equals(annotationTypeName)) { + BeanUtils.annotationMethods(annotationType, m -> { + String name = m.getName(); + try { + Object result = m.invoke(annotation); + if ("name".equals(name)) { + String annotationName = (String) result; + if (annotationName.length() != 0 && !annotationName.equals(enumName)) { + annotationNames[enumIndex] = annotationName; + } + } + } catch (Exception ignored) { + // ignored + } + }); } } break; diff --git a/fastjson1-compatible/src/test/java/com/alibaba/fastjson/v2issues/Issue2521.java b/fastjson1-compatible/src/test/java/com/alibaba/fastjson/v2issues/Issue2521.java new file mode 100644 index 0000000000..e49ba2071d --- /dev/null +++ b/fastjson1-compatible/src/test/java/com/alibaba/fastjson/v2issues/Issue2521.java @@ -0,0 +1,63 @@ +package com.alibaba.fastjson.v2issues; + +import com.alibaba.fastjson.serializer.SerializerFeature; +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONWriter; +import com.alibaba.fastjson2.annotation.JSONField; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class Issue2521 { + public enum Type { + @JSONField(name = "Rejected") + REJECTED, + + @JSONField(name = "Pending") + PENDING, + } + + public static class Bean { + public Type type; + } + + public enum Type1 { + @com.alibaba.fastjson.annotation.JSONField(name = "Rejected") + REJECTED, + + @com.alibaba.fastjson.annotation.JSONField(name = "Pending") + PENDING, + } + + public static class Bean1 { + public Type1 type; + } + + @Test + public void testMutated() { + String str = JSON.toJSONString(Type.REJECTED); + assertEquals("\"Rejected\"", str); + } + + @Test + public void testMutatedfj() { + String str = com.alibaba.fastjson.JSON.toJSONString(Type1.REJECTED); + assertEquals("\"Rejected\"", str); + } + + @Test + public void testMutated0() { + Bean bean = new Bean(); + bean.type = Type.REJECTED; + String str = JSON.toJSONString(bean, JSONWriter.Feature.WriteEnumsUsingName); + assertEquals("{\"type\":\"Rejected\"}", str); + } + + @Test + public void testMutated0fj() { + Bean1 bean1 = new Bean1(); + bean1.type = Type1.REJECTED; + String str = com.alibaba.fastjson.JSON.toJSONString(bean1, SerializerFeature.WriteEnumUsingName); + assertEquals("{\"type\":\"Rejected\"}", str); + } +}