[BUG]Feature.ErrorOnNotSupportAutoType在list场景下反序列化会报错的问题
- Dominant language
- Java
- Stars
- 4.4k
- Forks
- 613
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 6
Description
### 问题描述
我们正在升级 fastjson2,发现Feature.ErrorOnNotSupportAutoType在list场景下反序列化会报错,而单个元素的则不会
### 环境信息
*请填写以下信息:*
- 版本信息:fastjson 2.0.63 和 fastjson2 2.0.63 均复现
### 重现步骤
```java
public static void main(String[] args) {
record Data(String id) {
}
JSONReader.Context context = JSONFactory.createReadContext(JSONReader.Feature.ErrorOnNotSupportAutoType);
context.getProvider().addAutoTypeAccept("com.example.");
// 单元素验证
Data data = new Data("test-data");
String jsonEl = JSON.toJSONString(data, JSONWriter.Feature.WriteClassName);
System.out.println(jsonEl);
Object objEl = JSON.parse(jsonEl, context);
System.out.println(objEl);
// list验证
List list = List.of(data);
String jsonLi = JSON.toJSONString(list, JSONWriter.Feature.WriteClassName);
System.out.println(jsonLi);
// 此处报错
Object objLi = JSON.parse(jsonLi, context);
System.out.println(objLi);
}
```
输出
```
{"@type":"com.example.FastJsonTest$1Data","id":"test-data"}
Data[id=test-data]
[{"@type":"com.example.FastJsonTest$1Data","id":"test-data"}]
Exception in thread "main" com.alibaba.fastjson2.JSONException: autoType not support : com.example.FastJsonTest$1Data
at com.alibaba.fastjson2.JSONReader.readObject(JSONReader.java:3746)
at com.alibaba.fastjson2.JSONReader.readArray(JSONReader.java:4119)
at com.alibaba.fastjson2.reader.ObjectReaderImplObject.readObject(ObjectReaderImplObject.java:293)
at com.alibaba.fastjson2.JSON.parse(JSON.java:234)
at com.example.FastJsonTest.main(FastJsonTest.java:28)
```
如果去掉Feature.ErrorOnNotSupportAutoType则list部分的反序列化是正常的
Contributor guide
Research direction
Reproduce the difference between single-object and list deserialization with JSONReader.Feature.ErrorOnNotSupportAutoType, then start from JSONReader.readArray and JSONReader.readObject in the reported stack trace. Check the related flow through ObjectReaderImplObject.readObject and JSON.parse. Done means both the individual object and list cases deserialize consistently, with regression coverage for the supplied example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100