alibaba / alibaba/fastjson2

[BUG] 当 field 和 getter方法 均为 public 时,默认以 Field 优先输出

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

### 问题描述
```java
@Getter
@Setter
public static class AccountVO {

public long amount;

public BigDecimal getAmount() {
return BigDecimal.valueOf(amount).movePointLeft(2);
}

}

@Test
public void test() throws Exception {
AccountVO vo = new AccountVO();
vo.setAmount(10000L);
System.out.println(JSON.toJSONString(vo)); // {"amount":10000}
}
```

### 环境信息

- OS信息: CentOS 8.x / Windows 11 64bit
- JDK信息:Openjdk/Oracle JDK 17.0.11
- 版本信息:Fastjson2 2.0.51

### 重现步骤

直接执行上述单元测试即可,稳定复现。

### 期待的正确结果

按照 JavaBean 的规范,似乎应该以 getter 方法 的返回值优先输出 ?

#### 附加信息
这个修复改动可能会有向后**兼容性**的影响,请审慎评估。

这种写法可能不推荐,但在业务实现中具有现实意义。
比如, 我们想通过 `total.amount += vo.amount` 实现快速累加,而 `getAmount()` 用于对外输出。
如果不将 `amount` 字段保持 `public`,就需要 `total.setAmount( total.getAmount().add( vo.getAmount() ).movePointRight(2) )` 。

Contributor guide

Open the contributing guide

Research direction

Start with the provided AccountVO reproducer and the JSON.toJSONString(vo) entry point, then trace how serialization chooses between a public field and its getter. Add or run a regression test for the expected output and verify the compatibility implications described in the issue before considering the behavior complete.

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.