alibaba / alibaba/fastjson2

[BUG]HashSet的元素未做引用检测

Open
#7,678 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

### 问题描述
*简要描述您碰到的问题。*
使用fastjson2的过程中发现,fastjson2 开启引用检测后, 不能检测到 HashSet 类型中的元素的引用关系,fastjson1是可以的。

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

- OS信息: CentOS 8.4.2105 4Core 3.10GHz 16 GB]
- JDK信息: Openjdk 1.8.0_312]
- 版本信息:Fastjson2 2.0.60

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

1. 使用 `JSON.toJsonString` 方法
2. 输入数据: 对HashSet 进行序列化, 发现fastjson2 无法检测到Set中的元素的引用关系。
```java
//可在此输入示例代码
```

### 期待的正确结果
*对您期望发生的结果进行清晰简洁的描述。*
{
"data": [
{
"Person": {
"testSet": [
{
"xxx1": "123",
"xxx2": "123",
"xxx3": "123"
}
]
}
}
],
"Person": {
"testSet": [
{
"$ref": "$.data[0].Person.testSet[0]"
}
]
}
}

实际结果:
{
"data": [
{
"Person": {
"testSet": [
{
"xxx1": "123",
"xxx2": "123",
"xxx3": "123"
}
]
}
}
],
"Person": {
"testSet": [
{
"xxx1": "123",
"xxx2": "123",
"xxx3": "123"
}
]
}
}

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

#### 附加信息
*如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。*
涉及代码位置: com.alibaba.fastjson2.writer.ObjectWriterImplCollection#write 未对HashSet中的元素做引用检测

Contributor guide

Open the contributing guide

Research direction

Start in com.alibaba.fastjson2.writer.ObjectWriterImplCollection#write and reproduce the issue by serializing a HashSet with reference detection enabled through JSON.toJsonString. Compare the output with the expected $ref structure shown in the report. Done means repeated HashSet elements are represented by the expected reference and the behavior has regression coverage.

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
Quiet
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.