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] 复杂的嵌套bean,使用filter且map为null时将引发NPE #347

Closed
timnick-snow opened this issue May 25, 2022 · 2 comments
Closed
Labels
bug Something isn't working
Milestone

Comments

@timnick-snow
Copy link

问题描述

嵌套bean,使用filter,比如ValueFilter,当bean中有map且其值为null时,序列化将引发NPE

环境信息

请填写以下信息:

  • OS信息: win11,win7,centos7
  • JDK信息: jdk8,openjdk-17.0.2
  • 版本信息:Fastjson2 2.0.4

重现步骤

public class CommonTest {

    @Getter
    @Setter
    public class DemoDto {
        private String var1;
        private Map<String, Object> var2;
    }

    @Test
    public void fun() throws Exception {
        DemoDto d0 = new DemoDto();
        d0.setVar1("d0");

        List<DemoDto> list = new ArrayList<>();
        list.add(d0);

        ValueFilter demoFilter = (object, name, value) -> value;
        System.out.println(JSON.toJSONString(list, demoFilter));
    }
}

期待的正确结果

正确序列化,输出json
该代码在fastjson1中正常运行

相关日志输出

java.lang.NullPointerException: Cannot invoke "java.util.Map.entrySet()" because "map" is null

	at com.alibaba.fastjson2.writer.ObjectWriterImplMap.writeWithFilter(ObjectWriterImplMap.java:402)
	at com.alibaba.fastjson2.writer.ObjectWriter.writeWithFilter(ObjectWriter.java:121)
	at com.alibaba.fastjson2.writer.ObjectWriterImplMap.write(ObjectWriterImplMap.java:322)
	at com.alibaba.fastjson2.writer.ObjectWriterAdapter.writeWithFilter(ObjectWriterAdapter.java:399)
	at com.alibaba.fastjson2.writer.ObjectWriter_1.write(Unknown Source)
	at com.alibaba.fastjson2.writer.ObjectWriterImplList.write(ObjectWriterImplList.java:254)
	at com.alibaba.fastjson2.JSON.toJSONString(JSON.java:980)
@timnick-snow timnick-snow added the bug Something isn't working label May 25, 2022
wenshao added a commit that referenced this issue May 25, 2022
@wenshao wenshao added this to the 2.0.5 milestone May 25, 2022
@wenshao
Copy link
Member

wenshao commented May 25, 2022

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson/2.0.5-SNAPSHOT/
这个兼容问题已经修复,你可以用2.0.5-SNAPSHOT验证,预计本周内会发布2.0.5版本

@wenshao
Copy link
Member

wenshao commented May 28, 2022

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

@wenshao wenshao closed this as completed May 28, 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
Projects
None yet
Development

No branches or pull requests

2 participants