Skip to content

DeserializationContext.readTreeAsValue() handles null nodes differently from ObjectMapper.treeToValue() #4934

Closed
@FWest98

Description

@FWest98

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

The ObjectMapper functions handle the case where the root node is the null node by specifically calling getNullValue of the root deserializer. The equivalent DeserializationContext functions do not. I would expect the two functions to behave similarly, or the differences should be documented.

See below for a reproducer and use case.

Version Information

2.18.1

Reproduction

My use case is that I have a custom deserializer that reads the defaultValue metadata field that is populated by the JsonProperty annotation. I implement a custom getAbsentValue function that will handle the default values. However, I also want to allow "null" as a default value.

override fun getAbsentValue(ctxt: DeserializationContext): Property<*> {
    val defaultString = property?.metadata?.defaultValue // This is the string "null"

    if(defaultString != null) {
        val tree = ctxt.parser.codec.factory.createParser(defaultString).readValueAsTree<JsonNode>()
        val thisWorks = (ctxt.parser.codec as ObjectMapper).treeToValue<Any>(tree, valueType)
        val doesNotWork = ctxt.readTreeAsValue<Any>(tree, valueType)

        // Further handling
}

The workaround is to cast the codec to an ObjectMapper, but that does not seem very nice and I would prefer not to do it, since there are no guarantees of the cast being allowed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    2.19Issues planned at 2.19 or later

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions