alibaba / alibaba/fastjson2

[BUG] @JSONCompiled ignores @JSONField(serialize = false)

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

## Summary

`@JSONField(serialize = false)` suppresses a field when
using runtime serialization, but the same DTO leaks the field when
`@JSONCompiled` codegen is enabled.

## Runtime-only DTO

```java
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.annotation.JSONField;

public class RuntimeDefaultRepro {
public static class Person {
@JSONField(serialize = false)
public String internalCode;
public String name;

public Person() {
}

public Person(String internalCode, String name) {
this.internalCode = internalCode;
this.name = name;
}
}

public static void main(String[] args) {
System.out.println(JSON.toJSONString(new Person("secret", "Ada")));
}
}
```

## Codegen DTO

```java
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.annotation.JSONCompiled;
import com.alibaba.fastjson2.annotation.JSONField;

public class CodegenDefaultRepro {
@JSONCompiled
public static class Person {
@JSONField(serialize = false)
public String internalCode;
public String name;

public Person() {
}

public Person(String internalCode, String name) {
this.internalCode = internalCode;
this.name = name;
}
}

public static void main(String[] args) {
System.out.println(JSON.toJSONString(new Person("secret", "Ada")));
}
}
```

## Expected Result

Both programs should print:

```json
{"name":"Ada"}
```

## Actual Result

Runtime-only serialization prints:

```json
{"name":"Ada"}
```

Codegen serialization prints:

```json
{"internalCode":"secret","name":"Ada"}
```

## Environment

- JDK: `javac 21.0.5`
- FastJSON runtime: `com.alibaba.fastjson2:fastjson2:2.0.62`
- FastJSON codegen: `com.alibaba.fastjson2:fastjson2-codegen:2.0.62`

Contributor guide

Open the contributing guide

Research direction

Start with the two minimal DTO reproducers and compare JSON.toJSONString output with and without @JSONCompiled. Trace the code-generation path for @JSONField(serialize = false); done means the codegen example produces {"name":"Ada"}, matching runtime serialization.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend-api-design
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.