-
Notifications
You must be signed in to change notification settings - Fork 628
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
Treat nullable fields as optional #1196
Comments
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". |
It is possible for formats to support this independently (as in the XML format). But doing so does require the format itself to track the properties set and then generated synthetic property values for the values not set. Doing this is additional work and the way optional values works is better, but hidden from the format. On the other hand handling optional values is done by each serializer and thus depends on the Kotlin version used to compile the serializer (the serialization plugin), not the runtime library. As @psteiger points out, for some formats (like JSon) there is a difference between an unset property and one with null value. In XML there is no notion of null value, so absence will have to do. The format therefore would need to make the decision. Having the format interact/reconfigure the serializer would put quite some requirements upon the serializer and therefore overkill. |
Fair point, I am in the process of converting my serialization over from Moshi and it was able to do this so when I started getting this error in all my data classes I couldn't understand why missing properties were not just null since they were nullable |
Actually nullable properties can have non-null default values. |
As it was correctly mentioned, missing property and null value are different things, so this likely wouldn't be implemented. |
Hi! I read this thread, and it looks strange for me - you say, that with default value in constructor field is optional, otherwise - required. Also we have this section of readme. It says, that exists @required annotation, which makes optional fields required... @psteiger, your argument is that language constructor behaviour defines optionality of the fields - but I can argue with that, because we discuss fields, not constructors, and in case of fields optionality is nullability. (in many cases kotlin nullability is proposed as replacement of java optional) So, that what I think about it. We have @required now, so maybe we can remove |
@tujhex you tagged |
Well, maybe we could have an option to enable this behaviour, like with |
Ah nice! |
Currently if you have a data class that has nullable fields the serializer still considers the fields as required so for example
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
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
The text was updated successfully, but these errors were encountered: