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]嵌套类过滤指定字段,SimplePropertyPreFilter使用时序列化错误 #2384

Closed
tangyouwo opened this issue Mar 30, 2024 · 6 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@tangyouwo
Copy link

tangyouwo commented Mar 30, 2024

问题描述

有嵌套类时,A类里有B和C对象,对A的数组进行序列化时,添加了对B和C的字段过滤,但是不好用,直接返回[{}]

环境信息

请填写以下信息:

  • OS信息: [e.g.:Windows 11]
  • JDK信息: [e.g.:Openjdk 17]
  • 版本信息:[e.g.:Fastjson2 2.0.48]

重现步骤

如何操作可以重现该问题:

  1. 完整测试代码
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.filter.PropertyPreFilter;
import com.alibaba.fastjson2.filter.SimplePropertyPreFilter;
import lombok.Data;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Test {

    @Data
    static class BeanA {
        private BeanB  b;
        private BeanC c;
        private int number;
    }

    @Data
    static class BeanB {
        private String uname;
        private String id;
        private String age;
    }

    @Data
    static class BeanC {
        private String season;
        private String year;
        private String event;
    }
    public static void main(String[] args) {
        PropertyPreFilter[] filters = new PropertyPreFilter[2];
        SimplePropertyPreFilter filter1 = new SimplePropertyPreFilter(BeanB.class);
        String[] inc1 = {"uname","age"};
        filter1.getIncludes().addAll(Arrays.asList(inc1));
        filters[0] = filter1;
        SimplePropertyPreFilter filter2 = new SimplePropertyPreFilter(BeanC.class);
        String[] inc2 = {"season"};
        filter2.getIncludes().addAll(Arrays.asList(inc2));
        filters[1] = filter2;
        
        List<BeanA> list = new ArrayList<>();
        BeanA beanA = new BeanA();
        BeanB beanB = new BeanB();
        BeanC beanC = new BeanC();
        beanB.setUname("user1");
        beanB.setId("1");
        beanB.setAge("20");
        beanC.setSeason("2024");
        beanC.setYear("2023");
        beanC.setEvent("nba");
        beanA.setB(beanB);
        beanA.setC(beanC);
        beanA.setNumber(2);
        list.add(beanA);
        
        String str = JSON.toJSONString(list, filters, JSONWriter.Feature.WriteMapNullValue);

        System.out.println(str);
    }
}

期待的正确结果

  • [{"number": "2", b: {"uname": "test", "age":20},c:{"season":"2024"}}]*

相关日志输出

[{}]

@tangyouwo tangyouwo added the bug Something isn't working label Mar 30, 2024
@wenshao
Copy link
Member

wenshao commented Mar 30, 2024

上面的例子不能运行,可以帮忙提供能运行重新问题的带么?

@tangyouwo
Copy link
Author

@wenshao 重新修改了代码,可以直接运行的。之前是手打的伪代码

@tangyouwo
Copy link
Author

在1.2.70版本这么写是可以用的。只不过之前是SerializeFilter,现在变成了PropertyPreFilter

@wenshao wenshao added this to the 2.0.49 milestone Apr 1, 2024
@wenshao
Copy link
Member

wenshao commented Apr 13, 2024

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.49-SNAPSHOT/
问题已经修复,请帮忙用2.0.49-SNAPSHOT版本验证,2.0.49版本预计在明天(4月14日)发布

@wenshao wenshao added the fixed label Apr 13, 2024
wenshao added a commit that referenced this issue Apr 13, 2024
@wenshao
Copy link
Member

wenshao commented Apr 14, 2024

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

@wenshao wenshao closed this as completed Apr 14, 2024
@tangyouwo
Copy link
Author

ok 多谢,我试试

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