jackson-module-kotlin: Missing empty constructor issue: deserialization breaking change from 2.17 to 2.18
Search before asking
- I searched in the issues and found nothing similar.
- I have confirmed that the same problem is not reproduced if I exclude the KotlinModule.
- I searched in the issues of databind and other modules used and found nothing similar.
- I have confirmed that the problem does not reproduce in Java and only occurs when using Kotlin and KotlinModule.
Describe the bug
It appears that 2.18 introduced a change to the constructor detection, causing existing use cases to fail. I observed it in a case where a class extending a Map without an empty constructor can no longer be instantiated. See the test case example.
To Reproduce
With jackson 2.18.0 onboard:
@Test
fun test() {
assertThrows<MissingKotlinParameterException> {
jacksonObjectMapper().readValue<Old>("""{ "key":"value" }""")
}
assertDoesNotThrow {
jacksonObjectMapper().readValue<New>("""{ "key":"value" }""")
}
}
// what was working prior to 2.18
class Old : TreeMap<String, String> {
constructor(map: Map<String, String>) : super(map)
}
// what has to be changed to work with 2.18
class New : TreeMap<String, String> {
constructor() : super()
constructor(map: Map<String, String>) : super(map)
}
Expected behavior
Changes should be backward-compatible per the versioning standard (minor version changed).
Versions
Kotlin: Jackson-module-kotlin: 2.18.0 Jackson-databind: 2.18.0
Additional context
I’m not 100% sure if this is a Kotlin module issue or not, as I operate in Kotlin codebase only and not invested enough to test it in plane java. Generally, the fix to the issue is simple; the questionable part is that, if this is intentional, this is technically a breaking change in a minor version change.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 13
- Comments: 21 (12 by maintainers)
I can confirm the bug with these versions of jackson-module-kotlin:
I also tested with version 2.17.2 and the behavior to assure that the
MissingKotlinParameterExceptionis not thrown with the older version, as described.This seems to be breaking for
@JsonSubTypesclasses too, specifically where there is a private variable value which is of a function type and it has@JsonIgnoreadded to it, it seems like@JsonIgnoreis not ignoring that property for deserialization.The above test passes with jackson
2.17.2but fails with the following error in2.18.3:To workaround this, I had to add a custom noop deserializer and use it as the custom deserializer for this ignored property. This passes with both jackson
2.17.2and2.18.3:Ah. I only saw the email notification and replied without reading the context.
As to fix, it is not clear behavior can or will be changed.
But instead of adding an empty constructor (which really shouldn’t be needed) please considering adding annotation – something I have mentioned multiple times in this thread:
on constructor. That will indicate that the incoming value should match the full argument value of 1-arg constructor, and not 1 property of JSON Object with name matching that argument.
I will also file an issue on
jackson-databindto see if there is a way to create Java-only reproduction, and if so if the behavior could be changed.