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] 使用JSONPath.set对未知属性赋值失败 #897

Closed
quicklyfast opened this issue Nov 3, 2022 · 4 comments
Closed

[BUG] 使用JSONPath.set对未知属性赋值失败 #897

quicklyfast opened this issue Nov 3, 2022 · 4 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@quicklyfast
Copy link

问题描述

使用JSONPath.set对未知属性赋值失败

环境信息

  • OS信息: windows10
  • JDK信息: openjdk 11.0.15 2022-04-19 LTS
  • 版本信息:2.0.17

重现步骤

在以下的测试用例中,期望使用JSONPath.set(p, "$.age", 13)修改属性age的值为13,但读到age值仍然为12

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

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

public class MapperTest {
  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 test() {
    String json = "{\"name\": \"lisi\", \"age\": 12}";
    People p = JSON.parseObject(json, People.class);

    Assertions.assertEquals("lisi", p.name); // 成功
    Assertions.assertEquals(12, p.extra.get("age")); // 成功, 使用JSON.parseObject对未知属性赋值成功

    JSONPath.set(p, "$.age", 13);
    Assertions.assertEquals(13, p.extra.get("age")); // 失败: 使用JSONPath.set对未知属性赋值失败, p.extra.get("age") == 12
  }
}

期待的正确结果

p.extra.get("age") == 13

@quicklyfast quicklyfast added the bug Something isn't working label Nov 3, 2022
@ghost
Copy link

ghost commented Nov 3, 2022

.bi

@wenshao wenshao added this to the 2.0.18 milestone Nov 3, 2022
@wenshao wenshao added the fixed label Nov 3, 2022
@wenshao
Copy link
Member

wenshao commented Nov 3, 2022

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.18-SNAPSHOT/
问题已修复,请帮忙用2.0.18-SNAPSHOT版本验证,2.0.18版本预计在月底发布

@quicklyfast
Copy link
Author

quicklyfast commented Nov 4, 2022

2.0.18-SNAPSHOT版本测试失败,这个版本的JSON.parseObject对未知属性的赋值也不生效,以下是测试结果描述

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

    Assertions.assertEquals("lisi", p.name); // 成功
    Assertions.assertEquals(12, p.extra.get("age")); // 失败: p.extra.get("age") == null

    JSONPath.set(p, "$.age", 13);
    Assertions.assertEquals(13, p.extra.get("age")); // 失败: p.extra.get("age") == null
  }

完整的测试代码见重现步骤

wenshao added a commit that referenced this issue Nov 4, 2022
@wenshao
Copy link
Member

wenshao commented Nov 6, 2022

https://github.com/alibaba/fastjson2/releases/tag/2.0.18
问题已修复,请用新版本

@wenshao wenshao closed this as completed Nov 6, 2022
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