[BUG]fastjson2开启ReferenceDetection序列化一个对象后再反序列化,不同域对引用的同一个对象反序列化后不是相同的(==)对象
- Dominant language
- Java
- Stars
- 4.4k
- Forks
- 613
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 6
Description
### 问题描述
fastjson2开启ReferenceDetection序列化一个对象后再反序列化,不同域对同一个对象的引用反序列化后不是相同的对象
### 环境信息
- OS信息: Linux version 4.19.112-2.el8.x86_64 (root@3e6e2f794d6849b0823b9cb19064f400) (gcc version 8.3.1 20190507 (Red Hat 8.3.1-4) (GCC))
- JDK信息: Java(TM) SE Runtime Environment (build 1.8.0_271-b09)
- 版本信息:Fastjson2 2.0.32
### 重现步骤
以下单测,第二个断言失败,使用fastjson1是可以通过的
```java
@Test
void serialize_fastjson2_test() {
TestBean testBean = new TestBean();
testBean.setMap(newMap());
Assertions.assertThat(((AWrapper) testBean.getMap().get("wrapper")).getA()).isSameAs(testBean.getMap().get(
"inner"));
String jsonString = JSON.toJSONString(testBean, JSONWriter.Feature.WriteClassName,
JSONWriter.Feature.ReferenceDetection);
System.out.println(jsonString);
TestBean testBean1 = JSON.parseObject(jsonString, TestBean.class,
JSONReader.Feature.SupportAutoType);
// 第二个断言
Assertions.assertThat(((AWrapper) testBean1.getMap().get("wrapper")).getA()).isSameAs(testBean1.getMap().get(
"inner"));
}
private Map newMap() {
Map map = new HashMap<>();
A inner = new A();
map.put("inner", inner);
AWrapper aWrapper = new AWrapper();
aWrapper.setA(inner);
map.put("wrapper", aWrapper);
return map;
}
@Getter
@Setter
static class TestBean {
private Map map = new HashMap<>();
}
static class A{
}
@Getter
@Setter
static class AWrapper extends A{
private A a;
}
```
### 期待的正确结果
期望第二个断言正确,即相同引用反序列化后是同一个对象
### 相关日志输出
序列化后的输出:
```
{
"@type": "com.zte.paas.lcap.infrastructure.config.GzipFastjson2RedisSerializerTest$TestBean",
"map": {
"@type": "java.util.HashMap",
"wrapper": {
"@type": "com.zte.paas.lcap.infrastructure.config.GzipFastjson2RedisSerializerTest$AWrapper",
"a": {}
},
"inner": {
"$ref": "$.map.wrapper.a"
}
}
}
```
#### 附加信息
Contributor guide
Research direction
Start by turning the provided serialize_fastjson2_test reproduction into a test using JSON.toJSONString with ReferenceDetection and JSON.parseObject with SupportAutoType. Trace reference handling across serialization and deserialization, using the emitted $ref path as the guide. Done means the second isSameAs assertion passes for the map's wrapper.a and inner objects.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- backend-api-design
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100