alibaba / alibaba/fastjson2

[BUG] parse `"$ref"` then get() triggers ClassCastException

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

### 问题描述
https://github.com/alibaba/fastjson2/blob/334d25696654bb4e0593db33c4ec47afaa217cd9/core/src/test/java/com/alibaba/fastjson2/v1issues/issue_3900/Issue3922.java#L13
fastjson兼容解析`"$ref"`的json对象后使用get出现ClassCastException。

### 环境信息

- OS信息: [MacOS 12.7.4 M1 Pro 16 GB]
- JDK信息: [Openjdk 17.0.6]
- 版本信息:[Fastjson 2.0.51]

### 重现步骤

```java

import com.alibaba.fastjson.JSONObject;
import org.json.JSONException;
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;

public class Issue3922 {
@Test
public void test_for_issue3922() throws JSONException {
String jsonString = "{\"this0\":{\"$ref\":\"1\"}}";
JSONObject jsonObject = JSONObject.parseObject(jsonString, JSONObject.class);
JSONObject innerObject = (JSONObject) jsonObject.get("this0");
System.out.println(innerObject.toString());
JSONAssert.assertEquals("{\"$ref\":\"1\"}", innerObject.toString(), true);
}
}
```

### 相关日志输出
java.lang.ClassCastException: class com.alibaba.fastjson2.JSONObject cannot be cast to class com.alibaba.fastjson.JSONObject (com.alibaba.fastjson2.JSONObject and com.alibaba.fastjson.JSONObject are in unnamed module of loader 'app')

Contributor guide

Open the contributing guide

Research direction

Start with the reproducer in core/src/test/java/com/alibaba/fastjson2/v1issues/issue_3900/Issue3922.java, especially parseObject and the subsequent get("this0") cast. Run test_for_issue3922 to confirm the ClassCastException. Done means the compatibility path handles the "$ref" object without the reported cast failure and the existing JSONAssert check passes.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.