alibaba / alibaba/fastjson2

[BUG]嵌套范型没有成功反序列化出数据

Open
#2,841 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

### 问题描述
一段在fastjson1中可以反序列化的数据,升级了fastjson2后不行了,但是也没有报错

### 环境信息
- OS信息: macOS 12.6.1 Intel Core i7 16 GB
- JDK信息: jdk 1.8.0_275
- 版本信息:Fastjson 2.0.37

### 重现步骤
以下测试类在fastjson1下能通过,fastjson2不能通过
```java
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.util.ParameterizedTypeImpl;
import com.google.common.collect.Lists;
import org.junit.Test;

import java.lang.reflect.Type;
import java.util.LinkedHashMap;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class FastJsonTest {

public static class BaseElement extends LinkedHashMap {}

public static class ParentElement extends LinkedHashMap> {}

@Test
public void test_fastJson() {
BaseElement a = new BaseElement();
a.put("k1", "v1");
a.put("k2", "v2");
ParentElement b = new ParentElement();
b.put("k3", Lists.newArrayList(a));
String str = JSON.toJSONString(b);
ParentElement b2 = JSON.parseObject(str, new ParameterizedTypeImpl(
new Type[]{String.class, new ParameterizedTypeImpl(new Type[]{BaseElement.class}, null, List.class)},
null, ParentElement.class));
System.out.println(b);
System.out.println(b2);
assertEquals(b, b2);
}
}
```

#### 附加信息
多级ParameterizedTypeImpl嵌套的情况下,fastjson1解析使用了java.lang.Class#isAssignableFrom寻找到了正确的反序列化器,而fastjson2使用rawType == LinkedHashMap.class寻找对象解析器,debug看是没有找到的。怀疑和这个原因有关

Contributor guide

Open the contributing guide

Research direction

Start with the provided FastJsonTest reproduction and trace deserialization for the nested ParameterizedTypeImpl involving ParentElement, BaseElement, List, and LinkedHashMap. Compare the fastjson1 and fastjson2 behavior around rawType handling; done means the supplied assertion passes on fastjson2 2.0.37.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.