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 with unwrapped on set/get #2551

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

[BUG] @JSONField with unwrapped on set/get #2551

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

Comments

@Cooper-Zhong
Copy link
Contributor

问题描述

Fastjson中的@JSONField with unwrapped 异常,compared with fastjson2.

环境信息

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

重现步骤

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

import java.util.HashMap;
import java.util.Map;

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

public class MutatedIssue897_44 {
    @Test
    public void test() {
        String json = "{\"name\": \"zhangsan\", \"age\": 18}";
        People p = JSON.parseObject(json, People.class);

        assertEquals("zhangsan", p.getName());
        assertEquals(18, p.getExtra().get("age"));

        JSONPath.set(p, "$.age", 20);
        assertEquals(20, p.getExtra().get("age"));
    }

    static class People {
        private String name;
        private Map<String, Object> extra = new HashMap<>();

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Map<String, Object> getExtra() {
            return extra;
        }

        public void setExtra(Map<String, Object> extra) {
            this.extra = extra;
        }

        @JSONField(unwrapped = true)
        public void set(String key, Object value) {
            this.extra.put(key, value);
        }

        @JSONField(unwrapped = true)
        public Object get(String key) {
            return this.extra.get(key);
        }
    }

    @Test
    public void testfj() {
        String json = "{\"name\": \"zhangsan\", \"age\": 18}";
        People1 p = com.alibaba.fastjson.JSON.parseObject(json, People1.class);

        assertEquals("zhangsan", p.getName());
        assertEquals(18, p.getExtra().get("age"));

        com.alibaba.fastjson.JSONPath.set(p, "$.age", 20);
        assertEquals(20, p.getExtra().get("age"));
    }

    static class People1 {
        private String name;
        private Map<String, Object> extra = new HashMap<>();

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Map<String, Object> getExtra() {
            return extra;
        }

        public void setExtra(Map<String, Object> extra) {
            this.extra = extra;
        }

        @com.alibaba.fastjson.annotation.JSONField(unwrapped = true)
        public void set(String key, Object value) {
            this.extra.put(key, value);
        }

        @com.alibaba.fastjson.annotation.JSONField(unwrapped = true)
        public Object get(String key) {
            return this.extra.get(key);
        }
    }

}

期待的正确结果

正常解析

相关日志输出

Expected :18
Actual :null

@Cooper-Zhong Cooper-Zhong added the bug Something isn't working label May 10, 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