eclipse-ee4j / eclipse-ee4j/yasson
Yasson ignored @JsonbTypeAdapter on field level for map keys
- Dominant language
- Java
- Stars
- 218
- Forks
- 109
- Avg merge
- 1d 5h
- Merged PRs (30d)
- 9
Description
**Describe the bug**
According to the JSON-B Specification v3 chapter 4.7.1...
>There are two ways how to register JsonbAdapter:
>* Using JsonbConfig::withAdapters method;
>* Annotating a class field with JsonbTypeAdapter annotation.
Yasson 3.0.3 and 3.0.4 inconsistently handles adapters for map keys:
* Using `JsonbConfig::withAdapters`, Yasson *correctly* respects the adapter for a map key.
* Using `@JsonbTypeAdapter` on the class field, Yasson *incorrectly* ignores the adapter for the map key but prints an empty object `{}` as the key value.
**To Reproduce**
```java
package de.quipsy.sandbox.yassonbugs;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import jakarta.json.bind.JsonbBuilder;
import jakarta.json.bind.adapter.JsonbAdapter;
import jakarta.json.bind.annotation.JsonbTypeAdapter;
public final class YassonBug {
public static final void main(final String[] arguments) throws IOException {
final var jsonb = JsonbBuilder.newBuilder().build();
final var b = new B();
final var c = new C();
System.out.println(jsonb.toJson(b)); // correctly prints `{"c":"C"}`
System.out.println(jsonb.toJson(List.of(b))); // correctly prints `[{"c":"C"}]`
System.out.println(jsonb.toJson(Map.of(c, b))); // prints `[{"key":{},"value":{"c":"C"}}]`, but should print `[{"key":{"c"},"value":{"c":"C"}}]`
}
public static final class B {
@JsonbTypeAdapter(CAdapter.class)
public final C c = new C();
}
public static final class C {
@Override
public final String toString() {
return "C";
}
}
public static final class CAdapter implements JsonbAdapter {
@Override
public final String adaptToJson(final C obj) throws Exception {
return obj.toString();
}
@Override
public final C adaptFromJson(final String obj) throws Exception {
throw new UnsupportedOperationException("Unimplemented method 'adaptFromJson'");
}
}
}
```
**Expected behavior**
Yasson should correctly respect adapter for map key registered using `@JsonbTypeAdapter` just in the same ways it already correctly respects adapters set using `::withAdapters`.
NB: Yasson should also respect adapter registered using `@JsonbTypeAdapter` at the *class* level for map key, just as it already correctly respects that adapter for any other use besides map key.
**System information:**
- OS: Windows
- Java Version: 19
- Yasson Version: 3.0.3, 3.0.4
**Additional context**
See also https://github.com/eclipse-ee4j/yasson/issues/652
Contributor guide
Research direction
Start by reproducing the behavior with the JsonbBuilder example in the issue, comparing field-level and class-level @JsonbTypeAdapter handling with JsonbConfig::withAdapters. Trace the map-key serialization path and add regression coverage for the reported case. Done means annotated adapters produce the expected map-key output consistently with explicitly configured adapters.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- backend-api-design
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100