alibaba / alibaba/fastjson2

[BUG] [SupportSmartMatch] 使用JSON.to或者JSONObject.getObject JSONObject.getObject时;反序列化的Bean中包含对象数组,SupportSmartMatch对于对象数组中的对象不生效

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

### 问题描述
使用JSON.to或者JSONObject.getObject JSONObject.getObject时;反序列化的Bean中包含对象数组,SupportSmartMatch对于对象数组中的对象不生效

涉及api:
com.alibaba.fastjson2.JSONObject#getObject(java.lang.String, java.lang.Class, com.alibaba.fastjson2.JSONReader.Feature...)
com.alibaba.fastjson2.JSON#to

### 环境信息
- OS信息: Win11
- JDK信息: 1.8
- 版本信息:Fastjson2 2.0.53

### 重现步骤

```java
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;

import org.assertj.core.api.Assertions;
import org.junit.Before;
import org.junit.Test;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONReader;

import lombok.Data;

public class ObjToBeanFeatureTest {

@Before
public void setUp() throws Exception {
JSON.config(JSONReader.Feature.SupportSmartMatch, false);
}

@Test
public void arrayTest() {
String text = "{\"someInfoArr\":[{\"siteID\":1}, {\"siteID\":2}]}";

JSON.config(JSONReader.Feature.SupportSmartMatch, true);
// 反序列化,无问题
Bean bean2 = JSON.parseObject(text, Bean.class);
Assertions.assertThat(bean2.getSomeInfoArr()[0].siteId).isEqualTo(1);

Map jsonObject = JSON.parseObject(text);
Assertions.assertThat(((Map) ((List) jsonObject.get("someInfoArr")).get(0))
.get("siteID")).isEqualTo(1);

JSONObject obj = new JSONObject();
obj.put("v1", jsonObject);

// 两个API均无法正确反序列化
Bean bean3 = obj.getObject("v1", Bean.class, JSONReader.Feature.SupportSmartMatch);
Assertions.assertThat(bean3.getSomeInfoArr()[0].siteId).isEqualTo(null);

Bean bean4 = JSON.to(Bean.class, jsonObject);
Assertions.assertThat(bean4.getSomeInfoArr()[0].siteId).isEqualTo(null);
}

@Test
public void listTest() {
String text = "{\"someInfoList\":[{\"siteID\":1}, {\"siteID\":5}]}";

// 反序列化,无问题
JSON.config(JSONReader.Feature.SupportSmartMatch, true);
Bean bean2 = JSON.parseObject(text, Bean.class);
Assertions.assertThat(bean2.getSomeInfoList().get(0).siteId).isEqualTo(1);

Map jsonObject = JSON.parseObject(text);
Assertions.assertThat(((Map) ((List) jsonObject.get("someInfoList")).get(0))
.get("siteID")).isEqualTo(1);

JSONObject obj = new JSONObject();
obj.put("v1", jsonObject);

// 无问题
Bean bean3 = obj.getObject("v1", Bean.class, JSONReader.Feature.SupportSmartMatch);
Assertions.assertThat(bean3.getSomeInfoList().get(0).siteId).isEqualTo(1);

Bean bean4 = JSON.to(Bean.class, jsonObject);
Assertions.assertThat(bean4.getSomeInfoList().get(0).siteId).isEqualTo(1);
}

@Test
public void setTest() {
String text = "{\"someInfoSet\":[{\"siteID\":1}, {\"siteID\":5}]}";

// 反序列化,无问题
JSON.config(JSONReader.Feature.SupportSmartMatch, true);
Bean bean2 = JSON.parseObject(text, Bean.class);
Assertions.assertThat(bean2.getSomeInfoSet().stream().findFirst().get().siteId).isEqualTo(1);

Map jsonObject = JSON.parseObject(text);
Assertions.assertThat(((Map) ((List) jsonObject.get("someInfoSet")).get(0))
.get("siteID")).isEqualTo(1);

JSONObject obj = new JSONObject();
obj.put("v1", jsonObject);

// 无问题
Bean bean3 = obj.getObject("v1", Bean.class, JSONReader.Feature.SupportSmartMatch);
Assertions.assertThat(bean3.getSomeInfoSet().stream().findFirst().get().siteId).isEqualTo(1);

Bean bean4 = JSON.to(Bean.class, jsonObject);
Assertions.assertThat(bean4.getSomeInfoSet().stream().findFirst().get().siteId).isEqualTo(1);
}

@Data
private static class Bean {

private SomeInfo[] someInfoArr;
private List someInfoList;
private LinkedHashSet someInfoSet;
}

@Data
private static class SomeInfo {
private Integer siteId;
}
}
```

### 期待的正确结果
2.0.51 修复了List的问题, 数组也应该需要做相同处理。

Contributor guide

Open the contributing guide

Research direction

Start with the ObjToBeanFeatureTest reproduction and inspect the JSON.to and JSONObject#getObject conversion paths, comparing their handling of arrays with the already-working List case. Run the arrayTest with SupportSmartMatch enabled; done means nested SomeInfo objects receive siteID as siteId and both APIs produce a siteId value of 1.

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
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.