jakartaee / jakartaee/jsonb-api

Unclear when to include polymorphic information when runtimeType for serialization is thrown away

Open
#306 11 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
95
Forks
41
Avg merge
1d 6h
Merged PRs (30d)
35

Description

At the top-level of [`Jsonb`](https://eclipse-ee4j.github.io/jsonb-api/docs/api/java.json.bind/javax/json/bind/Jsonb.html) you can specify a [`runtimeType`](https://eclipse-ee4j.github.io/jsonb-api/docs/api/java.json.bind/javax/json/bind/Jsonb.html#toJson%28java.lang.Object,java.lang.reflect.Type%29) when serializing but that information is lost as you go down the object graph via [`SerializationContext`](https://eclipse-ee4j.github.io/jsonb-api/docs/api/java.json.bind/javax/json/bind/serializer/SerializationContext.html) method [`serialize(...)`](https://eclipse-ee4j.github.io/jsonb-api/docs/api/java.json.bind/javax/json/bind/serializer/SerializationContext.html#serialize%28T,javax.json.stream.JsonGenerator%29).

With the upcoming addition of polymorphic types this will become an issues. What is expected to happen in the following scenarios;

```java
@JsonbPolymorphicType(
key = "@race",
value = {
@JsonbSubtype(alias = "cat", type = Cat.class),
@JsonbSubtype(alias = "dog", type = Dog.class)
}
)
interface Animal {}

record Cat(String color) implements Animal
record Dog(String name) implements Animal
record Person(Dog bestFriend)

Dog dog = new Dog("Fred")
List animals = List.of(new Cat("red"), dog)
Person p = new Person(dog)

// What is the expected output of the following
jsonb.toJson(animals) // scenario 1
jsonb.toJson(dog) // scenario 2
jsonb.toJson(dog, Animal.class) // scenario 3
jsonb.toJson(p) // scenario 4
```

1. Everyone would probably agree on `[{"@race": "cat", "color": "red"}, {"@race": "dog", "name": "Fred"}]`
2. Should polymorphic information be included here? We're serializing a dog, not an animal.
3. Polymorphic information should most likely be included.
4. In the final scenario it is not even possible to make the distinction because in the serializer for `Person` we have to call the `SerializationContext#serialize` method to serialize the dog which only takes an object and we can't even specify the runtimeType which could be based on, for example, the return type of the method that gave us the object.

Contributor guide

Open the contributing guide

Research direction

Start with the Jsonb and SerializationContext API documentation linked in the issue, then examine the four serialization scenarios and how runtimeType is passed through nested serialization. A decision is needed on when polymorphic metadata is emitted and how serializers propagate type information; done means the expected behavior is clearly specified for all scenarios.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.