Skip to content

Discussion on fixing the ObjectMapper.readValue extension function problem(#399). #934

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

Closed
k163377 opened this issue Mar 22, 2025 · 2 comments

Comments

@k163377
Copy link
Contributor

k163377 commented Mar 22, 2025

Currently, the ObjectMapper.readValue extension function may return null even if the type argument T is specified as non-null (#399).

Image

This problem can be resolved by correcting the following

public inline fun <reified T> ObjectMapper.readValue(content: String): T =
  readValue(content, jacksonTypeRef<T>()) as T

// or

public inline fun <reified T> ObjectMapper.readValue(content: String): T =
  readValue(content, jacksonTypeRef<T>()).apply {
    if (this !is T) throw TODO("Some exception.")
  }

Here we want to determine what is the appropriate exception to be thrown(This discussion is a continuation from jackson-dev).

Specifications

In a strictly situational case, it is possible that is T could be false due to ObjectMapper problems.
For example, the deserializer may accidentally return null, or an inherited ObjectMapper may accidentally return an incorrect value.
In other words, the problem is not necessarily caused by invalid input.

Thus, a simple implementation of as T may result in both a NullPointerException and a ClassCastException.

Therefore, I personally think it is better to use the general exception defined in Jackson(JsonMappingException?) or the newly defined custom exception in kotlin-module.

Additional context

This fix does not solve the problem of StrictNullChecks not being applied to top-level collections.

@k163377 k163377 changed the title 【WIP】Discussion on fixing problems with ObjectMapper.readValue extension functions 【WIP】Discussion on fixing the ObjectMapper.readValue extension function problem(#399). Mar 22, 2025
@k163377 k163377 changed the title 【WIP】Discussion on fixing the ObjectMapper.readValue extension function problem(#399). Discussion on fixing the ObjectMapper.readValue extension function problem(#399). Mar 22, 2025
@cowtowncoder
Copy link
Member

My first thought based on discussion on mailing list is to consider InvalidNullException -- IllegalArgumentException is not quite it (I think I do understand why it seems initially like the thing).
And not NPE.

@k163377
Copy link
Contributor Author

k163377 commented Apr 6, 2025

Let the discussion continue at #937.
This Issue is closed.

@k163377 k163377 closed this as completed Apr 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants