eclipse-ee4j / eclipse-ee4j/yasson

Unexpected Serialization Behavior with Records Containing Lists in Java 21

Open
#656 2 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
218
Forks
109
Avg merge
1d 5h
Merged PRs (30d)
9

Description

I have come across a weird behavior after updating Java to version 21.

When having a record with a `List` field, the serialization of the object seems to behave differently.

I have made a reproducer using **JBang** for simplicity. For more info, see: https://www.jbang.dev/
```java
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS org.eclipse:yasson:3.0.4

import jakarta.json.bind.Jsonb;
import jakarta.json.bind.JsonbBuilder;
import jakarta.json.bind.annotation.JsonbCreator;
import java.util.List;

public class RecordsWithList {

public static void main(String... args) {
// print java version
System.out.println("java.version = " + System.getProperty("java.version"));
Jsonb jsonb = JsonbBuilder.create();

String jsonString = "{\"s\":\"testParentWithList\",\"testRecords\":[{\"needed\":\"1\",\"notNeeded\":\"2\"},{\"needed\":\"3\"}]}";
ParentRecordWithList result = jsonb.fromJson(jsonString, ParentRecordWithList.class);
System.out.println("TestRecords: " + result);

}

public record ParentRecordWithList(String s,
List testRecords) {
@JsonbCreator
public ParentRecordWithList {
}
}

public record TestRecord(String needed,
String notNeeded) {
@JsonbCreator
public TestRecord {
}
}
}

```
output for Java 17:
```shell
>jbang --java 17 --fresh RecordsWithList.java
[jbang] Resolving dependencies...
[jbang] org.eclipse:yasson:3.0.4
[jbang] Dependencies resolved
[jbang] Building jar...
java.version = 17.0.9
TestRecords: ParentRecordWithList[s=testParentWithList, testRecords=[TestRecord[needed=1, notNeeded=2], TestRecord[needed=3, notNeeded=null]]]
```

Output for Java 21:
```sh
>jbang --java 21 --fresh RecordsWithList.java
[jbang] Resolving dependencies...
[jbang] org.eclipse:yasson:3.0.4
[jbang] Dependencies resolved
[jbang] Building jar...
java.version = 21.0.5
TestRecords: ParentRecordWithList[s=testParentWithList, testRecords=[{needed=1, notNeeded=2}, {needed=3}]]
```

For some reason, the elements of `testRecords` seem to have map instances in Java 21.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.