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] Deserialization of EnumMap with WriteClassName and autotype #2583

Closed
Cooper-Zhong opened this issue May 16, 2024 · 3 comments
Closed
Labels
bug Something isn't working fixed
Milestone

Comments

@Cooper-Zhong
Copy link
Contributor

问题描述

EnumMap在配置WriteClassName和SupportAutoType后,无法使用JSON.parseObject反序列化。使用JSONB.parseObject以及在fastjson 1.2.83中正常

环境信息

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

重现步骤

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONB;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import org.junit.jupiter.api.Test;

import java.nio.charset.StandardCharsets;
import java.util.EnumMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.alibaba.fastjson.parser.Feature.SupportAutoType;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class Issue1679Mutated_763 {

    @Test
    public void testAll() {
        Bean bean = new Bean();
        bean.enumMap = new EnumMap(TimeUnit.class);

        byte[] bytes = JSONB.toBytes(bean);
        Bean bean1 = (Bean) JSONB.parseObject(bytes, Bean.class);
        assertEquals(0, bean1.enumMap.size());

        String json = JSON.toJSONString(bean);
        Bean bean2 = (Bean) JSON.parseObject(json, Bean.class);
        assertEquals(0, bean2.enumMap.size());

        byte[] bytes2 = JSON.toJSONString(bean).getBytes(StandardCharsets.UTF_8);
        Bean bean3 = (Bean) JSON.parseObject(bytes2, Bean.class);
        assertEquals(0, bean3.enumMap.size());

        String json2 = com.alibaba.fastjson.JSON.toJSONString(bean);
        Bean bean4 = (Bean) com.alibaba.fastjson.JSON.parseObject(json2, Bean.class);
        assertEquals(0, bean4.enumMap.size());
    }

    @Test
    public void test_jsonb() {
        Bean bean = new Bean();
        bean.enumMap = new EnumMap(TimeUnit.class);

        byte[] bytes = JSONB.toBytes(bean, JSONWriter.Feature.WriteClassName);
        Bean bean1 = (Bean) JSONB.parseObject(bytes, Bean.class, JSONReader.Feature.SupportAutoType);
        assertEquals(0, bean1.enumMap.size());
    }

    @Test
    public void test_json() {
        Bean bean = new Bean();
        bean.enumMap = new EnumMap(TimeUnit.class);

        String json = JSON.toJSONString(bean, JSONWriter.Feature.WriteClassName);
        Bean bean1 = (Bean) JSON.parseObject(json, Bean.class, JSONReader.Feature.SupportAutoType);
        assertEquals(0, bean1.enumMap.size());
    }

    @Test
    public void test_json_getBytes() {
        Bean bean = new Bean();
        bean.enumMap = new EnumMap(TimeUnit.class);

        byte[] bytes = JSON.toJSONString(bean, JSONWriter.Feature.WriteClassName).getBytes(StandardCharsets.UTF_8);
        Bean bean1 = (Bean) JSON.parseObject(bytes, Bean.class, JSONReader.Feature.SupportAutoType);
        assertEquals(0, bean1.enumMap.size());
    }


    @Test
    public void test_json_fj() {
        Bean bean = new Bean();
        bean.enumMap = new EnumMap(TimeUnit.class);

        String json = com.alibaba.fastjson.JSON.toJSONString(bean, SerializerFeature.WriteClassName);
        Bean bean1 = (Bean) com.alibaba.fastjson.JSON.parseObject(json, Bean.class, SupportAutoType);
        assertEquals(0, bean1.enumMap.size());
    }

    @Test
    public void test_json_getBytes_fj() {
        Bean bean = new Bean();
        bean.enumMap = new EnumMap(TimeUnit.class);

        byte[] bytes = com.alibaba.fastjson.JSON.toJSONString(bean, SerializerFeature.WriteClassName).getBytes(StandardCharsets.UTF_8);
        Bean bean1 = (Bean) com.alibaba.fastjson.JSON.parseObject(bytes, Bean.class, SupportAutoType);
        assertEquals(0, bean1.enumMap.size());
    }



    public static class Bean {
        public Map<TimeUnit, Object> enumMap;
    }
}

期待的正确结果

能够反序列化

相关日志输出

java.lang.NullPointerException: Cannot invoke "Object.equals(Object)" because "name" is null
at com.alibaba.fastjson2.reader.ObjectReaderImplMapTyped.readObject(ObjectReaderImplMapTyped.java:379)
at com.alibaba.fastjson2.reader.ORG_1_1_Bean.readObject(Unknown Source)
at com.alibaba.fastjson2.reader.ObjectReaderAdapter.autoType(ObjectReaderAdapter.java:247)
at com.alibaba.fastjson2.reader.ORG_1_1_Bean.readObject(Unknown Source)
at com.alibaba.fastjson2.JSON.parseObject(JSON.java:1577)
at Issue1679Mutated_763.test_json_getBytes(Issue1679Mutated_763.java:69)

java.lang.NullPointerException: Cannot invoke "Object.equals(Object)" because "name" is null
at com.alibaba.fastjson2.reader.ObjectReaderImplMapTyped.readObject(ObjectReaderImplMapTyped.java:379)
at com.alibaba.fastjson2.reader.ORG_1_1_Bean.readObject(Unknown Source)
at com.alibaba.fastjson2.reader.ObjectReaderAdapter.autoType(ObjectReaderAdapter.java:247)
at com.alibaba.fastjson2.reader.ORG_1_1_Bean.readObject(Unknown Source)
at com.alibaba.fastjson2.JSON.parseObject(JSON.java:1062)
at Issue1679Mutated_763.test_json(Issue1679Mutated_763.java:59)

@Cooper-Zhong Cooper-Zhong added the bug Something isn't working label May 16, 2024
@wenshao wenshao added this to the 2.0.51 milestone May 17, 2024
@wenshao wenshao added the fixed label May 17, 2024
@wenshao
Copy link
Member

wenshao commented May 17, 2024

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.51-SNAPSHOT/
问题已修复,请帮忙用2.0.51-SNAPSHOT版本验证

@Cooper-Zhong
Copy link
Contributor Author

验证已修复,辛苦温少

@wenshao
Copy link
Member

wenshao commented Jun 1, 2024

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

@wenshao wenshao closed this as completed Jun 1, 2024
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