alibaba / alibaba/fastjson2

[BUG]fastjson1.2.83升级到2.0.47,原先序列化引用对象的数据,再进行反序列化报错

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

### 问题描述
*fastjson1.2.83升级到2.0.47,原先序列化引用对象的数据,再进行反序列化报错*

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

- OS信息: [e.g.:Windows10 ]
- JDK信息: [e.g.:Openjdk 17]
- 版本信息:[e.g.:Fastjson2 2.0.47]

### 重现步骤
1、分别定义1个类(一点要是公共类)

```java
package com.metas.hanger.model.valueobject;

import com.metas.common.dto.BaseDTO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
public class StationNode extends BaseDTO {
private Long id;
private String stationAlias;
private Integer nextStationDistance;
private Long nextStationId;
private StationNode nextStation;

public StationNode(Long id, String stationAlias, Integer nextStationDistance, Long nextStationId) {
this.id = id;
this.stationAlias = stationAlias;
this.nextStationDistance = nextStationDistance;
this.nextStationId = nextStationId;
}
}

```

2、执行测试用例

```java
package com.metas.common.redis;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONReader;
import com.metas.hanger.model.valueobject.StationNode;

import java.nio.charset.StandardCharsets;
import java.util.List;

public class T1 {
public static void main(String[] args) {
String s = "[{\"@type\":\"com.metas.hanger.model.valueobject.StationNode\",\"id\":644,\"nextStation\":{\"id\":646,\"nextStation\":{\"id\":648,\"nextStation\":{\"id\":650,\"nextStation\":{\"id\":652,\"nextStation\":{\"id\":654,\"nextStation\":{\"$ref\":\"$[0]\"},\"nextStationDistance\":1,\"nextStationId\":644,\"stationAlias\":\"15_6\"},\"nextStationDistance\":1,\"nextStationId\":654,\"stationAlias\":\"15_5\"},\"nextStationDistance\":1,\"nextStationId\":652,\"stationAlias\":\"15_4\"},\"nextStationDistance\":1,\"nextStationId\":650,\"stationAlias\":\"15_3\"},\"nextStationDistance\":1,\"nextStationId\":648,\"stationAlias\":\"15_2\"},\"nextStationDistance\":1,\"nextStationId\":646,\"stationAlias\":\"15_1\"},{\"$ref\":\"$[0].nextStation\"},{\"$ref\":\"$[0].nextStation.nextStation\"},{\"$ref\":\"$[0].nextStation.nextStation.nextStation\"},{\"$ref\":\"$[0].nextStation.nextStation.nextStation.nextStation\"},{\"$ref\":\"$[0].nextStation.nextStation.nextStation.nextStation.nextStation\"}]";
List stationNode = JSON.parseObject(s.getBytes(StandardCharsets.UTF_8), List.class, JSONReader.Feature.SupportAutoType);
System.out.println(stationNode);
}
}

```
出现异常
```java
Exception in thread "main" java.lang.StackOverflowError
at java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453)
at com.metas.hanger.model.valueobject.StationNode.toString(StationNode.java:9)
at java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453)
at com.metas.hanger.model.valueobject.StationNode.toString(StationNode.java:9)
at java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453)
at com.metas.hanger.model.valueobject.StationNode.toString(StationNode.java:9)
at java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453)
```
![image](https://github.com/alibaba/fastjson2/assets/10411913/70af3d1e-a0bd-480c-bb63-33bac8069181)
强转类型后能获得对象数据
![image](https://github.com/alibaba/fastjson2/assets/10411913/af6db80e-68f4-469f-8acd-0b6243ced982)

Contributor guide

Open the contributing guide

Research direction

Start with the T1 main example and the StationNode class, using the reported fastjson2 2.0.47 setup. Determine whether the StackOverflowError occurs during deserialization or while printing the resulting list through StationNode.toString(). Done means isolating a reproducible library failure from the cyclic-object display behavior and documenting it with a focused regression case.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.