eclipse-ee4j / yasson

Eclipse Yasson project
https://projects.eclipse.org/projects/ee4j.yasson
Other
204 stars 96 forks source link

Unexpected Serialization Behavior with Records Containing Lists in Java 21 #656

Open mskacelik opened 3 days ago

mskacelik commented 3 days ago

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/

///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<TestRecord> testRecords) {
        @JsonbCreator
        public ParentRecordWithList {
        }
    }

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

output for Java 17:

>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:

>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.