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.

Treat nullable fields as optional

See original GitHub issue

Currently if you have a data class that has nullable fields the serializer still considers the fields as required so for example

@Serializable
data class ResponseData(
    val id: Long
    val email: String?,
    val phone: String?,
    val text: String
}

I would have expected the nullable fields to be null if they didnt exist in the json response but they throw the MissingFieldException

To fix the error you just set the default value to null

@Serializable
data class ResponseData(
    val id: Long
    val email: String? = null,
    val phone: String? = null,
    val text: String
}

To prevent from having to go through every nullable field can we just have it so that if its nullable and no default value set then its also considered optional?

What is the point of even having nullable fields if you have to set a default value anyway (aside from being able to have a null value for the field) for it to be null if the fields does not exist in the json response?

It seems to me something like this would make more sense and be more kotlin like

@Serializable
data class ResponseData(
    val id: Long //Required because its not nullable
    val email: String?, //Optional
    val phone: String? = "123456789", //Optional thats nullable
    val text: String //Required because its not nullable
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
psteigercommented, Nov 12, 2020

I don’t agree. In Kotlin, if you try to instantiate your first data class example in code, you will have to provide a value for the nullable fields, for they don’t have a default value. So the notion of nullable == optional is not really a Kotlin idiom. Same goes for serialization.

We need a way to differentiate

{value: null}

and

{}

Sometimes, they don’t mean the same thing. Both deserialize to:

data class(val value: Int? = null)

but only the former deserializes to:

data class(val value: Int?)

In other words, “undefined” is not always meant to mean the same as “defined as null”.

2reactions
sandwwraithcommented, Jun 25, 2021

@kernitus That’s what the new flag explicitNulls (#1535 ) does

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Optional and Nullable Properties in API Requests
Learn how optional and nullable properties can be used flexibly in combinations in each parameter of your API requests made from a SDK....
Read more >
10. Handling nulls with Optional - Java 8 tutorial!
You can create an optional object from a nullable value using the static factoy method Optional.ofNullable . The advantage over using this method...
Read more >
How to make a field optional? - java - Stack Overflow
In Java, all Strings are nullable and therefore you can treat all Strings as optional. Now your question should be: how to make...
Read more >
Java - Null Handling With Optionals | by Ömer Kurular - Medium
Optional class provides a way to handle nullable objects in a elegant way ... Then, we checked if the variable is null or...
Read more >
Guide To Java 8 Optional | Baeldung
Optional makes us deal with nullable values explicitly as a way of enforcing good programming practices. Let's now look at how the above...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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 Hackernoon Post

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