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

[BUG] Inconsistency in toJSONString for Enum #2521

Closed
Cooper-Zhong opened this issue May 6, 2024 · 3 comments
Closed

[BUG] Inconsistency in toJSONString for Enum #2521

Cooper-Zhong opened this issue May 6, 2024 · 3 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@Cooper-Zhong
Copy link

问题描述

fastjson 和 fastjson2 对Enum使用toJSONString 不一致

环境信息

请填写以下信息:

  • OS信息: [e.g.:MacOS 12.7.4 M1 Pro 16 GB]
  • JDK信息: [e.g.:Openjdk 17.0.6]
  • 版本信息:[e.g.:Fastjson 2.0.49]

重现步骤

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 Issue972Mutated_141 {
    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);
    }
}

期待的正确结果

两者结果不一致

  • fastjson使用了toJSONString(bean, SerializerFeature.WriteEnumUsingName);
  • fastjson2使用toJSONString(bean, JSONWriter.Feature.WriteEnumsUsingName);

result:
org.opentest4j.AssertionFailedError:
Expected :"Rejected"
Actual :"REJECTED"

@Cooper-Zhong Cooper-Zhong added the bug Something isn't working label May 6, 2024
@wenshao wenshao added this to the 2.0.50 milestone May 7, 2024
@wenshao
Copy link
Member

wenshao commented May 7, 2024

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson/2.0.50-SNAPSHOT/
问题已修复,请帮忙用2.0.50-SNAPSHOT版本验证,2.0.50版本预计在5月12日前发布

@Cooper-Zhong
Copy link
Author

经验证已修复,辛苦wenshao

@wenshao
Copy link
Member

wenshao commented May 12, 2024

https://github.com/alibaba/fastjson2/releases/tag/2.0.50
2.0.50已发布,请用新版本

@wenshao wenshao closed this as completed May 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

2 participants