You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Try to deserialize the following json to the data class:
{
"name": null,
"value": null
}
This leads to the exception:
com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException: Instantiation of [simple type, class org.example.Test] value failed for JSON property name due to missing (therefore NULL) value for creator parameter name which is a non-nullable type
at [Source: (String)"
{
"name": null,
"value": null
}
"; line: 5, column: 1] (through reference chain: org.example.Test["name"])
Expected behavior
No exception is thrown and instead the null values in the json should be skipped and the fields of the data class keep their default value.
I'm having the same issue half a year later since this was opened. Are there any updates?
EDIT: I think this is basically a fix. Enabling KotlinFeature.NullIsSameAsDefault while registering Kotlin module guarantees treating "null values as absent when deserializing, thereby using the default value provided in Kotlin" per docs. It worked for me.
It's not exactly a fix - it's a workaround - because it's possible that the value in your data class might be nullable with a non-null default. In that case, setting the property to null might be intended.
k163377
added a commit
to k163377/jackson-module-kotlin
that referenced
this issue
Aug 25, 2023
Describe the bug
The annotation
@JsonSetter(nulls = ...)
does not work with Kotlin data classes. (Related to #250?)To Reproduce
Use the following data class:
Try to deserialize the following json to the data class:
This leads to the exception:
I attached a project to reproduce this issue: jsonsetter-repro.zip.
Expected behavior
No exception is thrown and instead the null values in the json should be skipped and the fields of the data class keep their default value.
Versions
Kotlin: 1.6.10
Jackson-module-kotlin: 2.13.0
Jackson-databind: 2.13.0
Additional context
The use case of this is to ignore null values for non-nullable fields and let those fields retain their default value.
Note: The
JsonSetter
annotation works if the class is not a data class:The text was updated successfully, but these errors were encountered: