Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonSetter nulls does not work with Kotlin data classes #526

Closed
mengelbrecht opened this issue Dec 17, 2021 · 3 comments · Fixed by #707
Closed

JsonSetter nulls does not work with Kotlin data classes #526

mengelbrecht opened this issue Dec 17, 2021 · 3 comments · Fixed by #707
Assignees

Comments

@mengelbrecht
Copy link

Describe the bug
The annotation @JsonSetter(nulls = ...) does not work with Kotlin data classes. (Related to #250?)

To Reproduce
Use the following data class:

data class Test(
    @JsonSetter(nulls = Nulls.SKIP)
    val name: String = "foobar",
    @JsonSetter(nulls = Nulls.SKIP)
    val value: Int = 1,
)

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"])

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:

class Test {
    @JsonSetter(nulls = Nulls.SKIP)
    var name: String = "foobar"

    @JsonSetter(nulls = Nulls.SKIP)
    var value: Int = 1
}
@dinomite dinomite self-assigned this Dec 22, 2021
@getsadzeg
Copy link

getsadzeg commented May 23, 2022

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.

@MT-Jacobs
Copy link

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
Copy link
Contributor

k163377 commented Sep 2, 2023

This feature will be available starting with 2.16.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants