question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:26
  • Comments:37 (13 by maintainers)

github_iconTop GitHub Comments

24reactions
or-dvircommented, Jul 16, 2018

There are conflicts with various settings and picking one model.

Kotlin itself does not treat null and as the same. Neither does Jackson by default which would throw an exception for null being set into a primitive depending on the setting of the FAIL_ON_NULL_FOR_PRIMITIVES feature switch.

For a non nullable type should we treat null as when there is a default value available in the method signature? what about a nullable type? how do we know when null is intentional versus meaning ?

expected behavior (at least for me):

  • trying to set null to a non-nullable variable (e.g. String) if variable has default value, use it. otherwise throw exception
  • trying to set null to a nullable variable (e,g, ‘String?’) set to null regardless if there is a default value or not (as the developer has explicitly declared that null is acceptable).

Your workaround for now is to remove the null values from your JSON completely to mean “absent” instead of using null

not a good solution as many developers are using 3rd party APIs which they have no control over

22reactions
apatridacommented, Oct 26, 2019

Branch 2.10 now allows null to be treated as “use the default” value, but requires that you set a flag when creating the Kotlin module, otherwise the behavior stays as it was as the default behavior.

ObjectMapper().registerModule(KotlinModule(nullisSameAsDefault = true)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Kotlin Default Parameter Values when JSON value is ...
Using Kotlin Default Parameter Values when JSON value is null and Kotlin parameter type is Non-Nullable.
Read more >
Deserializing non-null type by passing a default value in Kotlin
With version 2.10.0 of jackson-databind you can have: data class MyDataClass ( @JsonSetter(nulls = Nulls.
Read more >
Most elegant way of using Gson + Kotlin with default values ...
First of, there is the Awesome-Kotlin list about JSON libraries. ... non-nullable types for null-safety and default arguments for the data ...
Read more >
Nullable arguments for non-nullable parameters with default ...
I recently encountered a scenario where I wanted to be able to provide a nullable type argument to a function where the corresponding ......
Read more >
Kotlin NullPointerException: Parameter specified as non-null ...
In this quick tutorial, we're going to see what happens when we store a null value into a non-null data type in Kotlin....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found