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

List deserialization fail with a subtypes configuration. #592

Open
remimarechal opened this issue Feb 20, 2023 · 0 comments
Open

List deserialization fail with a subtypes configuration. #592

remimarechal opened this issue Feb 20, 2023 · 0 comments
Assignees
Labels
bug Something isn't working right

Comments

@remimarechal
Copy link

remimarechal commented Feb 20, 2023

PR #590 fixes this issue

I get the next exception :
Unable to deserialize property 'elements' because of: Unexpected state: KEY_NAME

I don't know how to describe exactly the issue, but i provide some tests cases with failure and successes.

image

YassonBugTest.txt

package yasson.testcase;

import jakarta.json.Json;
import jakarta.json.JsonStructure;
import jakarta.json.bind.JsonbBuilder;
import jakarta.json.bind.annotation.JsonbSubtype;
import jakarta.json.bind.annotation.JsonbTypeInfo;
import org.eclipse.yasson.YassonJsonb;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.StringReader;
import java.util.List;

public class YassonBugTest {

    YassonJsonb jsonb;

    @BeforeEach
    void beforeEach() {
        jsonb = (YassonJsonb) JsonbBuilder.create();
    }

    @Test
    void test00_work_with_MyCanvas_type_and_list() {
        // We deserialize a List
        // But we give MyCanvas as type
        System.out.println(jsonb.toJson(jsonb.fromJson("""            
            {
                "@type": "canvas",
                "elements": [
                    {
                       "@type": "note"
                    }
                ]
            }
            """, MyCanvas.class)));
    }


    @Test
    void test01_work_with_MyObject_without_list() {
        // We don't deserialize a List
        System.out.println(jsonb.toJson(jsonb.fromJson("""            
            {
                "@type": "canvas",
                "element": {
                   "@type": "note"
                }
            }
            """, MyObject.class)));
    }

    @Test
    void test02_doesnt_work_with_MyObject_type_with_list() {
        // We deserialize a List
        System.out.println(jsonb.toJson(jsonb.fromJson("""            
            {
                "@type": "canvas",
                "elements": [
                    {
                       "@type": "note"
                    }
                ]
            }
            """, MyObject.class)));
    }


    @Test
    void test03_doesnt_work_with_structure_and_MyCanvas_type_and_list() {
        // We deserialize a List
        JsonStructure structure = Json.createReader(new StringReader("""            
            {
                "@type": "canvas",
                "elements": [
                    {
                       "@type": "note"
                    }
                ]
            }
            """)).read();

        System.out.println(jsonb.toJson(jsonb.fromJsonStructure(structure, MyCanvas.class)));
    }


    @Test
    void test04_work_with_structure_and_MyCanvas_type_without_list() {
        // We don't deserialize a List
        JsonStructure structure = Json.createReader(new StringReader("""            
            {
                "@type": "canvas",
                "element": {
                   "@type": "note"
                }
            }
            """)).read();

        System.out.println(jsonb.toJson(jsonb.fromJsonStructure(structure, MyCanvas.class)));
    }


    @JsonbTypeInfo({
        @JsonbSubtype(alias = "canvas", type = MyCanvas.class)})
    public static class MyObject {

    }

    public static class MyCanvas extends MyObject {

        public List<MyElement> elements;

        public MyElement element;
    }

    @JsonbTypeInfo({
        @JsonbSubtype(alias = "element", type = MyElement.class),
        @JsonbSubtype(alias = "note", type = MyNote.class)})
    public static class MyElement  {

        public Double x;
        public Double y;

    }

    public static class MyNote extends MyElement {

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