alibaba / fastjson2

🚄 FASTJSON2 is a Java JSON library with excellent performance.
Apache License 2.0
3.79k stars 495 forks source link

[BUG]fastjson2.0.53传入多个SimplePropertyPreFilter的实现类时,只生效最后一个 #2979

Open xiaoqianglovetsl opened 1 month ago

xiaoqianglovetsl commented 1 month ago

问题描述

简要描述您碰到的问题。

环境信息

请填写以下信息:

重现步骤

public static void main(String[] args) {
        Filter[] filters = new Filter[2];
        SimplePropertyPreFilter filter1 = new SimplePropertyPreFilter(BeanB.class);
        String[] exc1 = {"uname"};
        filter1.getExcludes().addAll(Arrays.asList(exc1));
        filters[0] = filter1;
        SimplePropertyPreFilter filter2 = new SimplePropertyPreFilter(BeanB.class);
        String[] exc2 = {"age"};
        filter2.getExcludes().addAll(Arrays.asList(exc2));
        filters[1] = filter2;

        List<BeanB> list = new ArrayList<>();
        BeanB beanB = new BeanB();
        beanB.setUname("user1");
        beanB.setId("1");
        beanB.setAge("20");
        list.add(beanB);

        String str = JSON.toJSONString(list, filters, JSONWriter.Feature.WriteMapNullValue);
        System.out.println(str);
    }

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

期待的正确结果

[{"id":"1"}]

实际输出

[{"id":"1","uname":"user1"}]

dsomehan commented 1 month ago
    public static void main(String[] args) {
        Filter[] filters = new Filter[2];
        SimplePropertyPreFilter filter1 = new SimplePropertyPreFilter(BeanB.class);
        filters[0] = filter1;
        filter1.getExcludes().add("uname");

        filter1.getExcludes().add("age");

        List<BeanB> list = new ArrayList<>();
        BeanB beanB = new BeanB();
        beanB.setUname("user1");
        beanB.setId("1");
        beanB.setAge("20");
        list.add(beanB);

        String str = JSON.toJSONString(list, filters, JSONWriter.Feature.WriteMapNullValue);
        System.out.println(str);
    }

    static class BeanB {
        private String uname;
        private String id;
        private String age;
        public String getUname() {
            return uname;
        }
        /**
         * 写所有属性的get和set
         *
         */
        public void setUname(String uname) {
            this.uname = uname;
        }
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }

        public String getAge() {
            return age;
        }

        public void setAge(String age) {
            this.age = age;
        }
    }

为啥不写成这样呢,可以看看源码,同一个类型的filter在数组中只生效一次

public void configFilter(Filter... filters) {
            for (int i = 0; i < filters.length; i++) {
                Filter filter = filters[i];
                if (filter instanceof NameFilter) {
                    if (this.nameFilter == null) {
                        this.nameFilter = (NameFilter) filter;
                    } else {
                        this.nameFilter = NameFilter.compose(this.nameFilter, (NameFilter) filter);
                    }
                }

                if (filter instanceof ValueFilter) {
                    if (this.valueFilter == null) {
                        this.valueFilter = (ValueFilter) filter;
                    } else {
                        this.valueFilter = ValueFilter.compose(this.valueFilter, (ValueFilter) filter);
                    }
                }

                if (filter instanceof PropertyFilter) {
                    this.propertyFilter = (PropertyFilter) filter;
                }

                if (filter instanceof PropertyPreFilter) {
                    this.propertyPreFilter = (PropertyPreFilter) filter;
                }

                if (filter instanceof BeforeFilter) {
                    this.beforeFilter = (BeforeFilter) filter;
                }

                if (filter instanceof AfterFilter) {
                    this.afterFilter = (AfterFilter) filter;
                }

                if (filter instanceof LabelFilter) {
                    this.labelFilter = (LabelFilter) filter;
                }

                if (filter instanceof ContextValueFilter) {
                    this.contextValueFilter = (ContextValueFilter) filter;
                }

                if (filter instanceof ContextNameFilter) {
                    this.contextNameFilter = (ContextNameFilter) filter;
                }
            }

            hasFilter = propertyPreFilter != null
                    || propertyFilter != null
                    || nameFilter != null
                    || valueFilter != null
                    || beforeFilter != null
                    || afterFilter != null
                    || labelFilter != null
                    || contextValueFilter != null
                    || contextNameFilter != null;
        }
xiaoqianglovetsl commented 1 month ago

是可以的、要变成全局了。以前用1的时候是分散各个地方判断。