alibaba / alibaba/fastjson2

[BUG] JSON.toJavaObject跨平台行为不一致,特殊名称的多余字段在ARM JDK8抛出IntrospectionException

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

### 问题描述
使用 fastjson‑2.0.64(compat 兼容包)的静态方法 JSON.toJavaObject(JSON json, Class clazz) 将 JSONObject 转换为实体类时,当 JSON 对象包含实体类不存在的多余字段,会出现跨平台行为不一致。
当多余字段的 key 为特殊名称/*时:
x86‑64 JDK8:正常执行,直接忽略该多余字段;
ARM (aarch64) JDK8:抛出IntrospectionException,整个对象转换失败。
相同输入在 fastjson‑1.2.83 版本,ARM 环境可以正常跳过该不存在字段,不会抛出异常。fastjson‑1.2.83 内部捕获了IntrospectionException,但 fastjson‑2.0.64 兼容层移植时遗漏该异常捕获逻辑,导致不同 CPU 架构行为不一致。

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

- OS 信息: Windows (x86‑64)、Linux (aarch64 ARM)
- JDK 信息: Corretto‑8u502(x86 本地),Corretto‑8u421(ARM 线上)
- 版本信息: fastjson‑2.0.64,maven 坐标 com.alibaba:fastjson:2.0.64

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

1. 调用 JSON.toJavaObject(JSON json, Class clazz) 方法
2. JSONObject 放入正常业务字段,同时加入实体类不存在的多余字段,key 为/*
3. x86‑64 JDK8 环境正常执行;ARM JDK8 抛出 IntrospectionException 异常
```java
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class Demo {
static class User {
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}

public static void main(String[] args) {
JSONObject jo = new JSONObject();
jo.put("name", "test");
jo.put("/*", "some_value"); // 实体类不存在的多余字段

// 存在问题的方法 JSON.toJavaObject
User user = JSON.toJavaObject(jo, User.class);
System.out.println(user.getName());
}
}
```

### 期待的正确结果
与 fastjson‑1.2.83 行为保持一致,遇到实体类不存在的多余字段直接跳过忽略,ARM 架构 JDK8 环境不抛出IntrospectionException,保证跨平台行为统一。

### 相关日志输出
Exception in thread "main" java.beans.IntrospectionException
at java.desktop/java.beans.Introspector.getBeanInfo(Introspector.java:262)
at java.desktop/java.beans.Introspector.getBeanInfo(Introspector.java:173)
...

#### 附加信息
1. 问题根因:compat 兼容包 JSON.toJavaObject(JSON json, Class clazz) 直接依赖 JDK Introspector 做属性匹配,未捕获IntrospectionException;部分特殊命名的多余字段,不同架构 JDK 的 Introspector 内部处理行为存在差异。
临时规避方案:替换为 fastjson2 核心包 API
2. com.alibaba.fastjson.JSON.parseObject(jsonObject.toJSONString(), T.class)
该方式绕开 JDK Introspector,x86 与 ARM 平台行为完全一致,多余字段默认忽略。
3. 该问题仅存在于兼容包com.alibaba:fastjson:2.0.64,fastjson2 主包不受影响。
4. Windows x86‑64 环境无法复现该异常,仅能在 ARM aarch64 环境复现。

Contributor guide

Open the contributing guide

Research direction

Start at the compat API entry point JSON.toJavaObject(JSON json, Class clazz) and reproduce the supplied User example with the extra "/*" field on JDK 8, including an aarch64 environment if available. Compare the compat behavior with fastjson 1.2.83 and the suggested fastjson2 parseObject workaround. Done means unknown fields are ignored and the conversion no longer fails with IntrospectionException across the reported platforms.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.