eclipse-ee4j / eclipse-ee4j/yasson
Json silently returns an empty list instead of failing
- Dominant language
- Java
- Stars
- 218
- Forks
- 109
- Avg merge
- 1d 5h
- Merged PRs (30d)
- 9
Description
**Describe the bug**
When providing a json object instead of a json array and asking Jsonb to produce a list, the library returns an empty list; whereas it should throw an exception to indicate to the caller that something is wrong.
**To Reproduce**
```
String json = "{\"name\": \"John Doe\"}";
try (Jsonb jsonb = JsonbBuilder.create()) {
List list = jsonb.fromJson(json, new ArrayList() {
private static final long serialVersionUID = -7485196487128234751L;
}.getClass().getGenericSuperclass());
/** The deserialization process returns an empty list instead of failing fast: */
assertEquals(List.of(), list);
}
```
See [Person](https://github.com/Sarah-Elhelw/teach_spreadsheets/blob/master/src/test/java/io/github/oliviercailloux/teach_spreadsheets/json/Person.java) and [PersonTests](https://github.com/Sarah-Elhelw/teach_spreadsheets/blob/master/src/test/java/io/github/oliviercailloux/teach_spreadsheets/json/PersonTests.java) for full details.
**Expected behavior**
I expect that the call `jsonb.fromJson` in the above example throws an exception indicating that the input string is not in a format that allows to extract a list of persons (as a json array is expected and not a json object).
**System information:**
- Yasson Version: 1.0.7
**Additional context**
This bug was found by students in my course, whose GitHub usernames are `Sarah-Elhelw` and `Semida-Buzdugan`. Credits go to them.
Contributor guide
Research direction
Start by reproducing the example through the jsonb.fromJson call, using the linked Person and PersonTests cases as reference. Trace the deserialization path for an object supplied where a JSON array is expected, then add or update coverage so the call fails with an exception rather than returning an empty list.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100