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

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

Open
lukaszsuski opened this issue Feb 13, 2023 · 7 comments

Comments

@lukaszsuski
Copy link

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

@lukaszsuski lukaszsuski added the to-evaluate Issue that has been received but not yet evaluated label Feb 13, 2023
@cowtowncoder
Copy link
Member

Wrong issue tracker, will transfer.

@cowtowncoder cowtowncoder transferred this issue from FasterXML/jackson-databind Feb 14, 2023
tmdgusya added a commit to tmdgusya/jackson-module-kotlin that referenced this issue Mar 12, 2023
@k163377
Copy link
Contributor

k163377 commented Mar 14, 2023

@tmdgusya
Could you please check if jackson-module-kogera fixes the problem?
https://github.com/ProjectMapK/jackson-module-kogera

If it seems to be fixed, I will introduce the changes to jackosn-module-kotlin.

@tmdgusya
Copy link

@k163377 oh thank you

@cowtowncoder
Copy link
Member

@tmdgusya Does this mean that Kogera does fix the issue?

@tmdgusya
Copy link

@cowtowncoder I haven't checked yet.

@k163377
Copy link
Contributor

k163377 commented Mar 18, 2023

I checked with kogera and it seems to work to some extent.
However, there was a problem when the value wrapped in value class was null.

import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper

class JsonIncludeNonNullTest {
    @JvmInline
    value class Primitive(val v: Int)

    @JvmInline
    value class NonNullObject(val v: String)

    @JvmInline
    value class NullableObject(val v: String?)

    @JsonInclude(value = JsonInclude.Include.NON_NULL, content = JsonInclude.Include.NON_NULL)
    data class Dto(
        val pN: Primitive? = null,
        val nnoN: NonNullObject? = null,
        val noNn: NullableObject = NullableObject(null),
        val noN1: NullableObject? = null,
        val noN2: NullableObject? = NullableObject(null),
        val map: Map<Any, Any?> = mapOf("no" to NullableObject(null))
    )

    @Test
    fun test() {
        val mapper = jacksonObjectMapper()
        val dto = Dto()
        println(mapper.writeValueAsString(dto)) // -> {"noNn":null,"noN2":null,"map":{"no":null}}
    }
}

This problem is caused by JsonInclude being ignored when getting and writing findNullValueSerializer with JsonSerializer::serialize.
Unfortunately, I have not found a solution to this problem.

Alternatively, there may be room to interpret this as "the value on Kotlin is serialized because it is nonnull".

@k163377 k163377 changed the title Kotlin value class is not excluded from serialization when it has null value If the value wrapped in value class is null, it is not excluded from serialization May 3, 2023
@k163377 k163377 added enhancement and removed to-evaluate Issue that has been received but not yet evaluated labels May 3, 2023
@k163377
Copy link
Contributor

k163377 commented May 3, 2023

The title has been edited to match the remaining current issue.

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

No branches or pull requests

4 participants