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] Exception in JSONObject.toJavaObject concerning Enum #2536

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

[BUG] Exception in JSONObject.toJavaObject concerning Enum #2536

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

Comments

@Cooper-Zhong
Copy link
Contributor

问题描述

类似 #2531,fastjson2和fastjson 在JSONObject.toJavaObject 对Enum的处理不一致。fastjson2没有抛异常而fastjson报错。

环境信息

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

重现步骤

import com.alibaba.fastjson2.JSON;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class Issue1106Mutated_127 {
    @Test
    public void test() {
        assertSame(UserStatus.NORMAL, JSON.parseObject("{\"state\":0}").toJavaObject(User.class).state);
        assertEquals(UserStatus.LOCKED, JSON.parseObject("{\"state\":1}").toJavaObject(User.class).state);
        assertNull(JSON.parseObject("{\"state\":-1}").toJavaObject(User.class).state);
    }

    @Test
    public void testfj() {
        assertSame(UserStatus.NORMAL, com.alibaba.fastjson.JSON.parseObject("{\"state\":0}").toJavaObject(User.class).state);
        assertEquals(UserStatus.LOCKED, com.alibaba.fastjson.JSON.parseObject("{\"state\":1}").toJavaObject(User.class).state);
        assertNull(com.alibaba.fastjson.JSON.parseObject("{\"state\":-1}").toJavaObject(User.class).state);
    }

    public enum UserStatus {
        NORMAL(0),
        LOCKED(1);

        private final byte code;

        UserStatus(int code) {
            this.code = (byte) code;
        }

        public byte getCode() {
            return this.code;
        }
    }

    public class User {
        public UserStatus state;
    }
}

期待的正确结果

期望两者统一出一个合理的处理方式

相关日志输出

com.alibaba.fastjson2.JSONException: No enum ordinal issues.Issue1106Mutated_127.UserStatus.-1

at com.alibaba.fastjson2.reader.ObjectReaderImplEnum.readObject(ObjectReaderImplEnum.java:238)
at com.alibaba.fastjson2.reader.FieldReader.acceptAny(FieldReader.java:456)
at com.alibaba.fastjson2.reader.ObjectReaderAdapter.createInstance(ObjectReaderAdapter.java:659)
at com.alibaba.fastjson.JSONObject.toJavaObject(JSONObject.java:1011)
at Issue1106Mutated_127.testfj(Issue1106Mutated_127.java:20)
@Cooper-Zhong Cooper-Zhong added the bug Something isn't working label May 7, 2024
@wenshao wenshao added this to the 2.0.50 milestone May 10, 2024
@wenshao wenshao added the fixed label May 10, 2024
@wenshao
Copy link
Member

wenshao commented May 10, 2024

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson/2.0.50-SNAPSHOT/
问题已修复,请帮忙用2.0.50-SNAPSHOT版本验证下

@Cooper-Zhong
Copy link
Contributor 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