Skip to content

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

@k163377

Description

@k163377

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions