jackson-module-kotlin: Jackson property annotations are not working with data classes
@JsonUnwrapped annotations are not working for data classes failing with not find creator property with name 'propertyname'.
Same structure with a regular class seems to work fine.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 4
- Comments: 15 (4 by maintainers)
Links to this issue
Commits related to this issue
- add test case for Github #56 — committed to FasterXML/jackson-module-kotlin by apatrida 7 years ago
- Trying something https://github.com/FasterXML/jackson-module-kotlin/issues/56#issuecomment-321932309 — committed to ctf/TEPID-commons by lilatomic 5 years ago
A workaround for this, in case anyone arrives at this issue while searching for a solution to the problem: Change
@JsonUnwrappedto@field:JsonUnwrapped, which will move the annotation to the field (instead of the constructor parameter) where Jackson will see it and correctly interpret it.I couldn’t get
@field:JsonUnwrappedto work. It works fine for serializing but not for deserializing.To summarize what works and what doesn’t.
This wont work Naturally this is what you would think would work
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot define Creator property “sku” as
@JsonUnwrapped: combination not yet supported1. hack to make it work This is the next best choice for it to work but is messy.
2. use lateinit
I don’t consider this a solution as it violates the proper way to instantiate an object. For example, if the constructor has all value objects (non primitives) then you need to put in a dummy property placeholder for it to work with a data class.
The issue is obscured in older versions of Jackson, with 2.9 you now get the correct error:
Databind does not support
JsonUnwrappedbeing used in a constructor or static creator method, it expects a 1-to-1 relationship between incoming parameters and available creator parameters. A solution would be to move that one property into the body of the class as alateinitproperty. I show the bad, and good versions here in a test case added for this issue:https://github.com/FasterXML/jackson-module-kotlin/blob/master/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/Github56.kt
i have only found the following “hack” to work properly and without too much dummy-fields-foobar:
coming from https://github.com/FasterXML/jackson-module-kotlin/issues/91#issuecomment-546741378
While looking for a stop-gap solution for this in GitHub, I found this (usage) rather straightforward solution for
@JsonUnwrappedsupport in Kotlin data classes without any need in adjusting fields/annotations. Support in creators is being worked on here: https://github.com/FasterXML/jackson-databind/issues/1467 (PR: https://github.com/FasterXML/jackson-databind/pull/4271).I am using second constructor with @JsonCreator as workaround:
You saved my day @hotzen ! Thanks