alibaba / alibaba/fastjson2

[BUG] parseObject方法如果使用TypeReference参数中带有泛型,无法注入字段值,但是FastJson可以work

Open
#2,704 1 comment 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

### 问题描述
*简要描述您碰到的问题。*

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

- OS信息: [e.g.:Windows11 16 GB]
- JDK信息: [e.g.:Oraclejdk 1.8.0_202]
- 版本信息:[e.g.:Fastjson2 2.0.51]

### 重现步骤
*如何操作可以重现该问题:*
#### 对象定义
public class DataApiResponse {

private String code;

private String msg;

private List data; // T对象中字段值都为有效注入

}

@Data
public class DfTableMetaModel {

private String tbName;

private String tbComment;

private final List colMetaList = new ArrayList<>();

public void addColMeta(DfColumnMetaModel colMetaInfo) {
this.colMetaList.add(colMetaInfo);
}

}
#### 处理如下类似json数据:
{
"msg": "调用成功",
"code": "200",
"data": [
{
"tb_comment": "bbb",
"tb_name": "aaa"
}
]
}

#### 执行如下代码无法将泛型DfTableMetaModel的值注入,但是改回FastJson可以注入
DataApiResponse tableMetaResult = JSON.parseObject(dataApiRespStr, new TypeReference>() {});

### 期待的正确结果
*与FastJson结果一致*

Contributor guide

Open the contributing guide

Research direction

Start at JSON.parseObject with the shown TypeReference> and reproduce the input using the provided DataApiResponse and model definitions. Compare generic field injection with the reported FastJson behavior; done means the DfTableMetaModel fields are populated consistently with FastJson.

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
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.