Skip to content

If the value wrapped in value class is null, it is not excluded from serialization #625

Open
@lukaszsuski

Description

@lukaszsuski

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions