alibaba / fastjson2

🚄 FASTJSON2 is a Java JSON library with excellent performance.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG] JSON.parseObject with @JSONFIeld not working

Cooper-Zhong opened this issue · comments

问题描述

Based on #1506,Fastjson没有反序列化@JSONFIeld的字段,并且以下的testcase中会不定反序列化A3 or A4。

环境信息

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

重现步骤

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

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

public class Issue1506Mutated_452 {
    @Test
    public void test() {
        Bean bean = JSON.parseObject("{\"a\":{}}", Bean.class);
        assertEquals(A2.class, bean.type);
    }

    @Test
    public void testfj() {
        Bean1 bean1 = com.alibaba.fastjson.JSON.parseObject("{\"a\":{}}", Bean1.class);
        assertEquals(A2.class, bean1.type);
    }

    public static class Bean {
        private Class type;

        public void setA(A a) {
            type = a.getClass();
        }

        public void setA(A1 a) {
            type = a.getClass();
        }

        @JSONField
        public void setA(A2 a) {
            type = a.getClass();
        }

        public void setA(A3 a) {
            type = a.getClass();
        }

        public void setA(A4 a) {
            type = a.getClass();
        }

        public void setA(A5 a) {
            type = a.getClass();
        }
        
        public void setA(A6 a) {
            type = a.getClass();
        }
    }

    public static class Bean1 {
        private Class type;

        public void setA(A a) {
            type = a.getClass();
        }

        public void setA(A1 a) {
            type = a.getClass();
        }

        @com.alibaba.fastjson.annotation.JSONField
        public void setA(A2 a) {
            type = a.getClass();
        }

        public void setA(A3 a) {
            type = a.getClass();
        }

        public void setA(A4 a) {
            type = a.getClass();
        }

        public void setA(A5 a) {
            type = a.getClass();
        }

        public void setA(A6 a) {
            type = a.getClass();
        }
    }

    public static class A {
    }

    public static class A1 {
    }

    public static class A2 {
    }

    public static class A3 {
    }

    public static class A4 {
    }

    public static class A5 {
    }
    
    public static class A6 {
    }
}

期待的正确结果

Expected :class issues_20230510.Issue1506Mutated_452$A2

相关日志输出

Expected :class issues_20230510.Issue1506Mutated_452$A2
Actual :class issues_20230510.Issue1506Mutated_452$A4

Expected :class issues_20230510.Issue1506Mutated_452$A2
Actual :class issues_20230510.Issue1506Mutated_452$A3

这个例子在2.0.50依然存在问题