Skip to content
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

[BUG] ObjectMapper.readValue<T>(...) breaks null safety #399

Open
revintec opened this issue Dec 1, 2020 · 2 comments
Open

[BUG] ObjectMapper.readValue<T>(...) breaks null safety #399

revintec opened this issue Dec 1, 2020 · 2 comments
Labels

Comments

@revintec
Copy link

revintec commented Dec 1, 2020

image
Jackson-module-kotlin version 2.9.7, newer version should experience the same problem

changing original source code inline fun <reified T> ObjectMapper.readValue(content: String): T = readValue(content, jacksonTypeRef<T>()) adds trailing ... as T would fix the problem, however this way it provides poor exception message
Exception in thread "main" java.lang.NullPointerException: null cannot be cast to non-null type kotlin.Any

@revintec revintec added the bug label Dec 1, 2020
@revintec revintec reopened this Dec 15, 2020
@dinomite dinomite added the 2.12 label Dec 19, 2020
@dinomite
Copy link
Member

Interesting, adding that cast to the extensions functions makes sense to me, but I'm a bit wary as I'm not certain what side effects it might have.

@viartemev do you have any insight?

@revintec would you make a PR with casts on the extension functions? Depending on how confident we are, this could be part of 2.12.1 or might wait until 2.13.

@revintec
Copy link
Author

sorry for the late reply. as I discovered lately, it's more complicated than just add a as T cast

  1. the exception message would not be user-friendly
  2. can't cover generic class
  // should throw, instead we got a map with contaminated values
  println(JSON.readValue<Map<String,Any>>("{\"key\":null}"))

I think the right approach would be to add a isNullable alone side com.fasterxml.jackson.core.type.TypeReference._type and check null inside jackson-core when deserializing. but this requires more modification, thus I didn't come up with a PR for now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants