jackson-databind: `@JsonValue` fails for Java Record
Version information 2.12.1 OpenJDK 15.0.1
To Reproduce
Given:
public final record GetLocations(@JsonValue Map<String, URI> nameToLocation)
{
@JsonCreator
public GetLocations(Map<String, URI> nameToLocation)
{
assertThat(nameToLocation, "nameToLocation").isNotNull();
this.nameToLocation = new HashMap<>(nameToLocation);
}
}
I am expecting Jackson to serialize the Map
to JSON but instead I get the following exception:
Problem with definition of [AnnotedClass GetLocations]: Multiple 'as-value' properties defined ([field GetLocations#nameToLocation] vs [method GetLocations#nameToLocation()])
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 7
- Comments: 15 (13 by maintainers)
Commits related to this issue
- Work around a JsonValue bug (https://github.com/FasterXML/jackson-databind/issues/3063) — committed to markelliot/allezgo by markelliot 3 years ago
- Fix #3063: resolve seeming `@JsonValue` conflict (useful with Records) — committed to FasterXML/jackson-databind by cowtowncoder a year ago
- Fix #3063: resolve seeming `@JsonValue` conflict (useful with Records) (#3735) — committed to FasterXML/jackson-databind by cowtowncoder a year ago
- [SPARK-42354][BUILD] Upgrade jackson to 2.14.2 ### What changes were proposed in this pull request? This pr aims upgrade `Jackson` related dependencies to 2.14.2 ### Why are the changes needed? This... — committed to apache/spark by LuciferYang a year ago
- [SPARK-42354][BUILD] Upgrade jackson to 2.14.2 ### What changes were proposed in this pull request? This pr aims upgrade `Jackson` related dependencies to 2.14.2 ### Why are the changes needed? This... — committed to apache/spark by LuciferYang a year ago
- [SPARK-42354][BUILD] Upgrade jackson to 2.14.2 ### What changes were proposed in this pull request? This pr aims upgrade `Jackson` related dependencies to 2.14.2 ### Why are the changes needed? This... — committed to snmvaughan/spark by LuciferYang a year ago
@cowwoc you can remove the
@JsonValue
annotation from the Record header, and annotate the accessor instead:A Trick
Since this issue is caused by (auto-)propagation of annotation on Records components, we learn that the decision to propagate the annotation to either field and/or accessor method is decided by the
@Target
supported by the annotation itself.Since
@JsonValue
can be annotated onElementType.FIELD
&ElementType.METHOD
, it gets propagated to both. Knowing this, you can create a custom meta-annotation for@JsonValue
that targets onlyElementType.METHOD
:Then this will then work: