Open
Description
Describe the bug
If a Dto contains fields of value class type and the value is null, it is not excluded from serialization with @JsonInclude(JsonInclude.Include.NON_NULL).
The only working workaround I've found is to use JsonInclude.Include.CUSTOM with valueFilter { object == null }
Version information
2.14.2
To Reproduce
@JvmInline
value class Wrapped(val rawValue: String)
@JsonInclude(JsonInclude.Include.NON_NULL)
data class NonNullDto (
val string: String? = null,
val wrapped: Wrapped? = null,
)
@JsonInclude(JsonInclude.Include.CUSTOM, valueFilter = NullFilter::class)
data class CustomDto (
val string: String? = null,
val wrapped: Wrapped? = null,
)
class NullFilter {
override fun equals(other: Any?) = other == null
}
@Test
fun `should not serialize null values with NonNull`() {
println(jackson.writeValueAsString(NonNullDto()))
println(jackson.writeValueAsString(CustomDto()))
}
Result for NonNullDto:
{"wrapped":null}
Result for CustomDto:
{}
Expected behavior
JsonInclude.Include.NON_NULL should exclude null values for kotlin value classes.
Additional context
Kotlin version: 1.8.10