alibaba / alibaba/fastjson2

[BUG] com.alibaba.fastjson2.JSONException: reference path invalid : ..

Open
#3,945 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

### 问题描述
反序列化对象的时候报错com.alibaba.fastjson2.JSONException: reference path invalid : ..

at com.alibaba.fastjson2.JSONReader.handleResolveTasks(JSONReader.java:320)
at com.alibaba.fastjson2.JSON.parse(JSON.java:75)
at com.alibaba.fastjson2.JSON.toJSON(JSON.java:3937)

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

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

### 重现步骤
*如何操作可以重现该问题:*

```java
@Test
public void test() {
class A {
A a;
String b;

public String getB() {
return b;
}

public A getA() {
return a;
}
}
class Request {
List list;

public List getList() {
return list;
}
}

A a = new A();
a.a = a;
a.b = "test";
List
list = new ArrayList<>();
list.add(a);
Request request = new Request();
request.list = list;
System.out.println(JSON.toJSON(request, JSONWriter.Feature.ReferenceDetection));
// System.out.println(JSON.parse(JSON.toJSONString(request,JSONWriter.Feature.ReferenceDetection), JSONReader.Feature.DisableReferenceDetect));

```

### 期待的正确结果
期望能够解析成功
目前通过JSONObject.parse(JSON.toJSONString(a, JSONWriter.Feature.ReferenceDetection), JSONReader.Feature.DisableReferenceDetect);进行规避,但是性能有较大劣化

### 相关日志输出
*请复制并粘贴任何相关的日志输出。*

#### 附加信息
*如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。*

Contributor guide

Open the contributing guide

Research direction

Reproduce the supplied self-referential A and Request example, then inspect JSONReader.handleResolveTasks, JSON.parse, and JSON.toJSON around the reported stack trace. Add a regression test for ReferenceDetection and verify that the serialized value can be parsed successfully without the DisableReferenceDetect workaround.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.