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

NullIsSameAsDefault is not respected for non-string types #585

Closed
mroche89 opened this issue Oct 10, 2022 · 0 comments
Closed

NullIsSameAsDefault is not respected for non-string types #585

mroche89 opened this issue Oct 10, 2022 · 0 comments

Comments

@mroche89
Copy link

Describe the bug

When instantiating an objectmapper with the KotlinModule and setting the nullIsSameAsDefault to true, the setting is not respected for non-string types. When parsing json with explicit {"field":null}, we expect the field to have the default, and not be null (see below)

This reopens ticket 425

To Reproduce

The following test should pass (but does not):

@Test
fun `null inputs should use default value`() {
    val objectMapper = ObjectMapper().apply {
        registerModules(
            KotlinModule.Builder()
                .configure(KotlinFeature.NullIsSameAsDefault, true)
                .build()
        )
    }

    val json = """
        {
            "myBool": null,
            "myInt": null,
            "myStr": null,
            "myDouble": null,
            "myNullable": null,
            "myObj": null
        }
    """.trimIndent()
    println(objectMapper.readValue<MyObj>(json))
}

data class MyObj(
    val myBool: Boolean = true,
    val myInt: Int = 10,
    val myStr: String = "RedLeather",
    val myDouble: Double = 20.0,
    val myNullable: String? = null,
    val myObj: Inner = Inner()
) {
    data class Inner(val myVal: String = "YellowLeather")
}

This prints

MyObj(myBool=false, myInt=0, myStr=RedLeather, myDouble=0.0, myNullable=null, myObj=Inner(myVal=YellowLeather))

Only myStr, myNullable and myObj used correct defaults

Versions

Kotlin: 1.6.21
Jackson-module-kotlin: 2.13.4

szymonprz added a commit to szymonprz/jackson-module-kotlin that referenced this issue Nov 5, 2022
…ight now

Cleaned up tests for null to default feature as they can now clearly show what is supported and what is not.
There is no way to provide default value for primitive fields which are not set as nullable, because the primitives are initialized to their defaults (0, 0.0, false etc.) and there is no possibility to distinguish if the value was initialized as a default primitive value or it was provided by user in content (in the first case we can use default parameter value but in the latter no)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant