alibaba / alibaba/fastjson2

[BUG] 反序列化异常, EnumMap 以及 为负值的LocalDateTime 反序列化异常

Open
#2,859 1 comment 0 reactions 0 assignees View on GitHub
bug fixed
Dominant language
Java
Stars
4.4k
Forks
613
Avg merge
1d 22h
Merged PRs (30d)
6

Description

### 问题描述
*简要描述您碰到的问题。*
1: 反序列化 **EnumMap**
2: 当**LocalDateTime 为负值**时报错

### 环境信息
*请填写以下信息:*

- OS信息: **Darwin Mac-mini.local 23.5.0 Darwin Kernel Version 23.5.0: Wed May 1 20:09:52 PDT 2024; root:xnu-10063.121.3~5/RELEASE_X86_64 x86_64**
- JDK信息:
```
openjdk version "1.8.0_372"
OpenJDK Runtime Environment (Temurin)(build 1.8.0_372-b07)
OpenJDK 64-Bit Server VM (Temurin)(build 25.372-b07, mixed mode)
```
- 版本信息:
```
Maven: com.alibaba.fastjson2:fastjson2:2.0.51
Maven: com.alibaba.fastjson2:fastjson2-extension:2.0.51
Maven: com.alibaba.fastjson2:fastjson2-extension-spring5:2.0.51
```

### 重现步骤
*如何操作可以重现该问题:*

1. 反序列化EnumMap
```java
public static void main(String[] args) {
RedisSerializer serializer = new GenericFastJsonRedisSerializer(new String[]{"cn.", "com.", "java."});
Map> a = new EnumMap<>(ApplicationType.class);
ApplicationDO code = new ApplicationDO();
code.setId(1L);
code.setEnabled(true);
code.setCreateTime(LocalDateTime.now());

a.computeIfAbsent(ApplicationType.AI, it -> new ArrayList<>()).add(code);
byte[] serialize = serializer.serialize(a);
System.out.println(new String(serialize));
HashMap> deserialize = (HashMap>) serializer.deserialize(serialize);
System.out.println(deserialize);
System.out.println(deserialize.getClass());
}
```
输出:
```
{"@type":"java.util.EnumMap","AI":[{"@type":"cn.iocoder.yudao.module.business.dal.dataobject.application.ApplicationDO","createTime":"2024-08-09 14:35:22.967","enabled":true,"id":1L}]}
Exception in thread "main" org.springframework.data.redis.serializer.SerializationException: Could not deserialize: java.lang.String cannot be cast to java.lang.Enum; nested exception is java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Enum
at com.alibaba.fastjson2.support.spring.data.redis.GenericFastJsonRedisSerializer.deserialize(GenericFastJsonRedisSerializer.java:71)
at cn.iocoder.yudao.FastJson2Test.main(FastJson2Test.java:25)
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Enum
at java.util.EnumMap.(EnumMap.java:178)
at com.alibaba.fastjson2.reader.ObjectReaderImplMap.lambda$static$0(ObjectReaderImplMap.java:21)
at com.alibaba.fastjson2.reader.ObjectReaderImplMap.readObject(ObjectReaderImplMap.java:459)
at com.alibaba.fastjson2.reader.ObjectReaderImplObject.readObject(ObjectReaderImplObject.java:129)
at com.alibaba.fastjson2.JSON.parseObject(JSON.java:1624)
at com.alibaba.fastjson2.support.spring.data.redis.GenericFastJsonRedisSerializer.deserialize(GenericFastJsonRedisSerializer.java:68)
... 1 more
```

2. 反序列化 LocalDateTime 当时间为负值

```java
public static void main(String[] args) {
RedisSerializer serializer = new GenericFastJsonRedisSerializer(new String[]{"cn.", "com.", "java."});
Map> a = new EnumMap<>(ApplicationType.class);
ApplicationDO code = new ApplicationDO();
code.setId(1L);
code.setEnabled(true);
code.setCreateTime(LocalDateTime.MIN); // 这里 <---<----<---------

a.computeIfAbsent(ApplicationType.AI, it -> new ArrayList<>()).add(code);
byte[] serialize = serializer.serialize(a);
System.out.println(new String(serialize));
HashMap> deserialize = (HashMap>) serializer.deserialize(serialize);
System.out.println(deserialize);
System.out.println(deserialize.getClass());
}
```

输出:
```
{"@type":"java.util.EnumMap","AI":[{"@type":"cn.iocoder.yudao.module.business.dal.dataobject.application.ApplicationDO","createTime":"-999999999-01-01 00:00:00","enabled":true,"id":1L}]}
Exception in thread "main" org.springframework.data.redis.serializer.SerializationException: Could not deserialize: read LocalDateTime error -999999999-01-01 00:00:00, offset 162, character ", line 1, column 162, fastjson-version 2.0.51 {"@type":"java.util.EnumMap","AI":[{"@type":"cn.iocoder.yudao.module.business.dal.dataobject.application.ApplicationDO","createTime":"-999999999-01-01 00:00:00","enabled":true,"id":1L}]}; nested exception is com.alibaba.fastjson2.JSONException: read LocalDateTime error -999999999-01-01 00:00:00, offset 162, character ", line 1, column 162, fastjson-version 2.0.51 {"@type":"java.util.EnumMap","AI":[{"@type":"cn.iocoder.yudao.module.business.dal.dataobject.application.ApplicationDO","createTime":"-999999999-01-01 00:00:00","enabled":true,"id":1L}]}
at com.alibaba.fastjson2.support.spring.data.redis.GenericFastJsonRedisSerializer.deserialize(GenericFastJsonRedisSerializer.java:71)
at cn.iocoder.yudao.FastJson2Test.main(FastJson2Test.java:25)
Caused by: com.alibaba.fastjson2.JSONException: read LocalDateTime error -999999999-01-01 00:00:00, offset 162, character ", line 1, column 162, fastjson-version 2.0.51 {"@type":"java.util.EnumMap","AI":[{"@type":"cn.iocoder.yudao.module.business.dal.dataobject.application.ApplicationDO","createTime":"-999999999-01-01 00:00:00","enabled":true,"id":1L}]}
at com.alibaba.fastjson2.JSONReader.readLocalDateTime(JSONReader.java:1244)
at com.alibaba.fastjson2.reader.ObjectReaderImplLocalDateTime.readObject(ObjectReaderImplLocalDateTime.java:57)
at com.alibaba.fastjson2.reader.ORG_1_14_ApplicationDO.readObject(Unknown Source)
at com.alibaba.fastjson2.reader.ObjectReaderImplObject.readObject(ObjectReaderImplObject.java:129)
at com.alibaba.fastjson2.JSONReader.readArray(JSONReader.java:2559)
at com.alibaba.fastjson2.JSONReader.read(JSONReader.java:2090)
at com.alibaba.fastjson2.reader.ObjectReaderImplMap.readObject(ObjectReaderImplMap.java:453)
at com.alibaba.fastjson2.reader.ObjectReaderImplObject.readObject(ObjectReaderImplObject.java:129)
at com.alibaba.fastjson2.JSON.parseObject(JSON.java:1624)
at com.alibaba.fastjson2.support.spring.data.redis.GenericFastJsonRedisSerializer.deserialize(GenericFastJsonRedisSerializer.java:68)
... 1 more
```

### 期待的正确结果
*对您期望发生的结果进行清晰简洁的描述。*

### 相关日志输出
*请复制并粘贴任何相关的日志输出。*

#### 附加信息
*如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。*

Contributor guide

Open the contributing guide

Research direction

Start with the reported stack traces in ObjectReaderImplMap.readObject and JSONReader.readLocalDateTime, then inspect ObjectReaderImplLocalDateTime and GenericFastJsonRedisSerializer. Reproduce both EnumMap and LocalDateTime.MIN cases using the examples in the issue. Done means both cases deserialize successfully without the reported ClassCastException or date parsing error.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.