Skip to content

JsonSetter nulls does not work with Kotlin data classes #526

@mengelbrecht

Description

@mengelbrecht

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
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions