eclipse-ee4j / eclipse-ee4j/yasson

Deserialize class with bounded type parameter

Open
#393 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
218
Forks
109
Avg merge
1d 5h
Merged PRs (30d)
9

Description

**Describe the bug**
Section 3.17 of the JSON-B specification describes Generics and in particular how to deal with bounds on type parameters. If a type parameter has multiple bounds, then every bound should be resolved separately. The following example shows a class where the type variable `T` has two bounds `Child` and `Parent`. Moreover, `Child implements Parent` such that `Child` is the most specific type.

**To Reproduce**
```java
public static class OverlappingBoundsContainer {
public T instance;

public OverlappingBoundsContainer() {
}

public OverlappingBoundsContainer(T instance) {
this.instance = instance;
}
}

public interface Parent {}

public static class Child implements Parent {
public int number;

public Child() {
}

public Child(int number) {
this.number = number;
}
}
```

Now run:

```java
Jsonb jsonb = JsonbBuilder.create();
jsonb.fromJson("{\"instance\": {\"number\": 42}}", OverlappingBoundsContainer.class);
```

Yields

```
Caused by: java.lang.IllegalArgumentException: Can not set yasson.example.App$Child field yasson.example.App$OverlappingBoundsContainer.instance to java.util.HashMap
```

**Expected behavior**
`Jsonb#fromJson` should return an object that is an instance of `OverlappingBoundsContainer`. It's field `instance` should be set to an object that is an instance of `Child`. This instance should have it's field `number` set to 42.

**System information:**
- OS: macOS
- Java Version: 8
- Yasson Version: 1.0.6

**Additional context**
Note that if you change the class definition to the following, the problem still exists:
```java
public static class OverlappingBoundsContainer {
...
}
```

That means that this problem is not specific to _multiple_ bounds, but bounded type parameters on itself.

Contributor guide

Open the contributing guide

Research direction

Start with the OverlappingBoundsContainer reproducer and trace Jsonb#fromJson through Yasson's generic type resolution and deserialization path. Confirm the failure with the bounded Child type parameter, then verify that the returned container has a Child instance with number set to 42.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.