-
Notifications
You must be signed in to change notification settings - Fork 174
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
NonNull enum property has null value #373
Comments
This happens with any type- not just enums. IIRC, Objectmapper::readValue always returns a "platform type" which can always be null. This is definitely unfortunate and I don't know what the most correct and least-suprising answer would be, but I suspect the best answer would be a split API where half the methods on ObjectMapper return |
If Kotlin types indicate nullability, this is something that Kotlin module has to provide support for: jackson-databind has no immediate visibility to (or often, concept of) aspects of Kotlin that extend beyond Java. Implementation-wise there are two parts to the problem:
It might be possible to implement both by overriding method
in Same approach could work for other types too, of course, not just |
This is how to know if type is nullalble or not. Null check for non-nullable types can be implemented in |
@rafalbednarczuk This is from source calls, but the challenge is how to introspect this for given type: starting point is likely just type-erased |
@cowtowncoder Does this new feature require changes on |
@rafalbednarczuk It really depends on exactly how it can be implemented, but ideally only in Kotlin module. Ability to fail on nulls (or replace with "default" value) exists already and is usable from Java with https://medium.com/@cowtowncoder/jackson-2-9-features-b2a19029e9ff but the question here is that of trying to automatically apply it for non-nullable Kotlin types, so that users would not have to -- for example -- add annotations all over the place, or have to enumerate all enumerations to use config overrides. |
@cowtowncoder What do you think of modifying readValue functions? Here is working example snippet. I can make pull request for all functions.
|
@rafalbednarczuk Main problem is just non-scalability (and fragility of sub-classing in general) of this approach: |
@cowtowncoder Do you have any other solution? Do you plan to add this feature or should I compile my own fork? |
I have no plans to work on this myself, I am offering information that helps others to possibly implement it (and outline some challenges). |
Let me know where to put the code and I will work on it. |
Typically you would fork whatever you want to contribute fix/change for. |
Opened PR 405 against 2.12 with @rafalbednarczuk's fix. |
Similar issue with Int: val mapper = jacksonObjectMapper()
data class Car(val doors: Int)
val car: Car = mapper.readValue("{\"doors\":null }")
println(car) Ideally this should ideally throw a MissingKotlinParameterException. |
This issue is closed as a duplicate of #399. |
Describe the bug
Deserializing "null" string as non-null enum gives null value.
To Reproduce
this snippet prints
true
Expected behavior
Snippet should throw exception
Versions
Kotlin: 1.3.72
Jackson-module-kotlin: 2.11.2
Jackson-databind: 2.11.0
The text was updated successfully, but these errors were encountered: