eclipse-ee4j / eclipse-ee4j/yasson
Polymorphic deserialization fails with a JsonValue property
- Dominant language
- Java
- Stars
- 218
- Forks
- 109
- Avg merge
- 1d 5h
- Merged PRs (30d)
- 9
Description
**Describe the bug**
Polymorphic deserialization throws exception when deserializing to a class with a `JsonValue` property.
> jakarta.json.bind.JsonbException: Internal error: null
>
> at org.eclipse.yasson.internal.DeserializationContextImpl.deserializeItem(DeserializationContextImpl.java:142)
> at org.eclipse.yasson.internal.DeserializationContextImpl.deserialize(DeserializationContextImpl.java:127)
> at org.eclipse.yasson.internal.JsonBinding.deserialize(JsonBinding.java:55)
> at org.eclipse.yasson.internal.JsonBinding.fromJson(JsonBinding.java:62)
**To Reproduce**
```Java
@JsonbTypeInfo(
key = "type",
value = {@JsonbSubtype(alias = "jsv", type = PolyContainer.JsValueContainer.class)}
)
public interface PolyContainer {
class JsValueContainer implements PolyContainer {
public JsonValue jsValue;
}
}
```
```Java
public void test() throws Exception {
try (Jsonb jsonb = JsonbBuilder.create()) {
var container = new PolyContainer.JsValueContainer();
container.jsValue = Json.createValue("a json string");
String containerSerialized = jsonb.toJson(container);
assertNotNull(containerSerialized);
var deserializedDirectly = jsonb.fromJson(containerSerialized, PolyContainer.JsValueContainer.class);//good
assertNotNull(deserializedDirectly);
var deserializedFromPoly = jsonb.fromJson(containerSerialized, PolyContainer.class);//bad
assertNotNull(deserializedFromPoly);
}
}
```
**Expected behavior**
The object to be deserialized the same way as it does when not using polymorphism.
**System information:**
- OS: Windows 11
- Java Version: 21
- Yasson Version: 3.0.3
**Additional context**
Using a custom deserializer also does not work:
```Java
public class JSONGenericDeserializer implements JsonbDeserializer {
@Override
public JsonValue deserialize(JsonParser jsonParser, DeserializationContext deserializationContext, Type type) {
return jsonParser.getValue();
}
}
```
Contributor guide
Research direction
Start with the failing path in org.eclipse.yasson.internal.DeserializationContextImpl.deserializeItem and compare it with the direct class path through JsonBinding. Add a regression test using the polymorphic JsonValue example from the issue, then verify deserialization completes without an exception and preserves the JsonValue property.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- backend-api-design
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100