alibaba / alibaba/fastjson2

[BUG] 反序列化集合List,智能匹配失效

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

Description

### 问题描述
*反序列化集合List,智能匹配失效*

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

- OS信息: [e.g.:CentOS 8.4.2105 4Core 3.10GHz 16 GB]
- JDK信息: [e.g.:Openjdk 1.8.0_312] JDK8
- 版本信息:[e.g.:Fastjson2 2.x.x] fastjson2.2.53

### 重现步骤
*如何操作可以重现该问题:*
1. 使用 `com.alibaba.fastjson.JSONArray#toJavaList` 方法
2. 输入 `{\"data\":[{\"username\":\"mike\"}, {\"username\":\"john\"}]}` 数据
3. 出现 `反序列化集合List,智能匹配失效` 错误

```java
//示例代码
@Test
public void test() throws AuthException {
String str = "{\"data\":[{\"username\":\"mike\"}, {\"user_name\":\"john\"}]}";
// data1集合中userName为null
List data1 = JSON.parseObject(str).getJSONArray("data").toJavaList(User.class);

JSONArray jsonArray = JSON.parseObject(str).getJSONArray("data");
// data2集合中userName正常
List data2 = JSONObject.parseArray(JSON.toJSONString(jsonArray), User.class);
}

@Getter
@Setter
public static class User {
String userName;
}

### 期待的正确结果
data1集合中userName能正确获取mike和john

### 相关日志输出
[Test.User(userName=null), Test.User(userName=john)]
[Test.User(userName=mike), Test.User(userName=john)]

Contributor guide

Open the contributing guide

Research direction

Start with the reproduced test and the JSONArray#toJavaList entry point, then compare its collection deserialization path with JSONObject.parseArray. The fix is complete when both list elements map user_name to User.userName and the regression test passes without the intermediate JSON round trip.

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
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.