Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mskacelik opened this issue Nov 29, 2024 · 2 comments
Labels
bug Something isn't working right

Comments

@mskacelik
Copy link

mskacelik commented Nov 29, 2024

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.

@realmarbro
Copy link

I ran into this one as well when I upgraded to JDK 21, most likely this is related to the following bug in 21 https://bugs.openjdk.org/browse/JDK-8320575 . I tested it by using OpenJDK 23 and the problem was gone, so not a bug in Yasson, but a side effect of the bug in the JDK. It is also fixed in OpenJDK 21.0.6 recently, but I think not many vendors have released their distribution of that version yet.

Better to close this one, or keep it open for a while for those who search the issue list.

@mskacelik
Copy link
Author

Oh, thank you for the info, I was not aware of the bug 😅.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working right
Projects
None yet
Development

No branches or pull requests

2 participants