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] JSONField的unwrapped问题 #2525

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

[BUG] JSONField的unwrapped问题 #2525

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

Comments

@Cooper-Zhong
Copy link

问题描述

类似#1158, fastjson 在@JSONField(unwrapped = true)在JSON.parseObject时 与fastjson2结果不同

环境信息

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

重现步骤

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.annotation.JSONField;
import lombok.Data;
import org.junit.jupiter.api.Test;

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

public class Issue1158TestMutated_221 {

    @Test
    public void testMutated() {
        StandardGetQry standardGetQry = new StandardGetQry();
        StandardBaseModel standardBaseModel = new StandardBaseModel();
        standardBaseModel.setCode("2");
        standardBaseModel.setCategory("NEW_CALL_STATUS");
        standardGetQry.setStandardBaseModel(standardBaseModel);
        String jsonStr = JSON.toJSONString(standardGetQry);
        assertEquals("{\"standardCategory\":\"NEW_CALL_STATUS\",\"standardCode\":\"2\"}", jsonStr);

        StandardGetQry standardGetQry2 = JSON.parseObject(jsonStr, StandardGetQry.class);
        assertEquals("{\"standardCategory\":\"NEW_CALL_STATUS\",\"standardCode\":\"2\"}", JSON.toJSONString(standardGetQry2));
    }

    @Data
    public static class StandardGetQry {
        /**
         * 基础数据
         */
        @JSONField(unwrapped = true)
        private StandardBaseModel standardBaseModel;
    }

    @Data
    public static class StandardBaseModel {
        /**
         * 逻辑编码-标准KEY
         */
        @JSONField(name = "standardCode")
        private String code;

        /**
         * 标准类别
         */
        @JSONField(name = "standardCategory")
        private String category;
    }

    @Test
    public void testMutated1() {
        StandardGetQry1 standardGetQry = new StandardGetQry1();
        StandardBaseModel1 standardBaseModel = new StandardBaseModel1();
        standardBaseModel.setCode("2");
        standardBaseModel.setCategory("NEW_CALL_STATUS");
        standardGetQry.setStandardBaseModel(standardBaseModel);
        String jsonStr = com.alibaba.fastjson.JSON.toJSONString(standardGetQry);
        assertEquals("{\"standardCategory\":\"NEW_CALL_STATUS\",\"standardCode\":\"2\"}", jsonStr);

        StandardGetQry1 standardGetQry2 = com.alibaba.fastjson.JSON.parseObject(jsonStr, StandardGetQry1.class);
        assertEquals("{\"standardCategory\":\"NEW_CALL_STATUS\",\"standardCode\":\"2\"}", com.alibaba.fastjson.JSON.toJSONString(standardGetQry2));
    }

    @Data
    public static class StandardGetQry1 {
        /**
         * 基础数据
         */
        @com.alibaba.fastjson.annotation.JSONField(unwrapped = true)
        private StandardBaseModel1 standardBaseModel;
    }

    @Data
    public static class StandardBaseModel1 {
        /**
         * 逻辑编码-标准KEY
         */
        @com.alibaba.fastjson.annotation.JSONField(name = "standardCode")
        private String code;

        /**
         * 标准类别
         */
        @com.alibaba.fastjson.annotation.JSONField(name = "standardCategory")
        private String category;
    }
}

期待的正确结果

期望两者的处理结果应保持一致

相关日志输出

Expected :{"standardCategory":"NEW_CALL_STATUS","standardCode":"2"}
Actual :{}
at Issue1158TestMutated_221.testMutated1(Issue1158TestMutated_221.java:62)

@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/fastjson2/fastjson2/2.0.50-SNAPSHOT/
问题已修复,请帮忙用2.0.50-SNAPSHOT版本验证,2.0.50版本预计在5月12日前发布

@Cooper-Zhong
Copy link
Author

经验证该样例无问题,#2551 似乎存在问题

@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