-
Notifications
You must be signed in to change notification settings - Fork 620
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
coerceInputValues=true and explicitNulls=false should decode unknown enum value without default to null #2586
Comments
Hi! I've reproduced your issue, and it seems there is some misunderstanding because the documentation wasn't formulated clearly enough. It says, To solve this problem right now, I'd recommend adding the default |
Technically there is a difference between a value set to null and the value being nullable (without null default), but is this a concern to the library in this case? Can't it always be assumed that when it's nullable, it should default to null? As when you mark something nullable, you are fully aware it can be missing and that nulls could be present. If this could be set by a new property, that would be great. |
IMO, this is a very far-reaching assumption. Json has a clear distinction between null and absence; while these may be the same things for one API, they may be different for another (hence the necessity for all these flags). Moreover, we strive to be format-agnostic in the core, and for other data formats this distinction may be even more important. |
A null value is not the same as a default value. And there are cases where the default would not be null (generally used to set the absence of a value). There are similarly cases where assuming absence of a value equals null is incorrect (this is somewhat format specific, More to the point, it is not clear that it is safe to assume that null is the same as a default, or that it is safe to treat an unsupported value the same as the absence of said value (without it being signalled, it is not possible for the relying code to know this substitution happened). |
It's just not about enums only.
doesn't do what it says right now
Why is this labelled as a feature when it is clearly a bug? |
@boy12hoody The original issue is specifically about an incorrect enum value in the input and is not related to |
@sandwwraith I just hadn't noticed other types also having this issue. Should I reword my issue or do you want a new one (from me or boy12hoody)? |
@carstenhag It's better to create a new one. So far, I'm unable to reproduce what you are saying: @Serializable
data class NullableInt(val i: Int?)
@Test
fun testNullableInt() {
val j = Json { explicitNulls = false; coerceInputValues = true }
println(j.decodeFromString<NullableInt>("{}"))
println(j.decodeFromString<NullableInt>("{\"i\":null}"))
} correctly yields |
@sandwwraith I dont use |
@boy12hoody As I said, please create a separate issue with a code to reproduce |
Regarding to the issue described by @carstenhag We in our project highly relied on the combination of I would love to have this "feature" (or bugfix) very soon. 🙏 Thank you in advance! |
Describe the bug
We updated from 1.6.2 to 1.6.3 and now nullable enums without explicit null default don't get decoded anymore, leading to an exception.
To Reproduce
SerializationException: org.example.Service does not contain element with name 'test' at path $.service
Expected behavior
We were expecting that these configs (taken from the documentation) get "added up" together:
In other words: For a nullable enum without default value, set its value to "null" if the JSON value is an unknown enum member.
Other
Apart from combining these two config properties, I wouldn't know how we can get automatic nulls? We would have to set an explicit null to each nullable enum?
Also see #2585.
Environment
The text was updated successfully, but these errors were encountered: