Skip to content

Using Kotlin Default Parameter Values when JSON value is null and Kotlin parameter type is Non-NullableΒ #130

@grundleborg

Description

@grundleborg

I've got the following simplified JSON example, which I'm trying to decode into the simplified Kotlin Data Class below.

{ 
   "boolField": null,
    "stringField": null
}
data class TestObject(
        val boolField: Boolean = true,
        val stringField: String = "default"
)

The key thing here is that the Kotlin properties are not nullable, but there is a known default value for them. However, the JSON sometimes contains null for those fields.

I am trying to get the example JSON to decode using the default values in place of the nulls since the type is non-nullable. However, this doesn't work out of the box, instead throwing a MissingKotlinParameterException.

I had a look at modifying the code with a feature flag to behave the way I wanted. This was easy enough to do with some minor alterations to createFromObjectWith() in KotlinValueInstantiator for the String case. However, for the Boolean case it does not work, as in Java, that non-optional Boolean becomes a boolean primitive type, which cannot take null and thus Jackson Data Binding sets it with the default value of false.

So, assuming I haven't missed the point completely with this, I'm wondering if there's a way in the KotlinValueInstantiator to know that the primitive types were set with their default values by Jackson Data Binding in order to make this work for primitive types too?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions