eclipse-ee4j / eclipse-ee4j/yasson

List deserialization fail with a subtypes configuration.

Open
#592 0 comments 0 reactions 1 assignee Assigned to @remimarechal View on GitHub
bug
Dominant language
Java
Stars
218
Forks
109
Avg merge
1d 5h
Merged PRs (30d)
9

Description

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](https://user-images.githubusercontent.com/859908/220170938-6108a667-0a34-44ae-963a-1fa319819795.png)

[YassonBugTest.txt](https://github.com/eclipse-ee4j/yasson/files/10786738/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 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 {

}
}
```

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.