Skip to content

When deserializing using ObjectMapper::treeToValue, NPE occurs when JsonParser::readValueAs is called #5683

Description

@k163377

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

The issue occurs as described in the title.

This error does not occur when using readValue or when the JSON representation is numeric.

It appears there are differences in the processing path for treeToValue compared to readValue, resulting in DeserializationContext._readCapabilities being null.
Furthermore, access to this property only occurs when conversion from a string to a primitive is performed.
Specifically, it is called from the StdDeserializer._checkFromStringCoercion method.

Version Information

3.0.0 ~ 3.1.0-RC1(9720c26)

Reproduction

OK will be printed, but processing NG will cause an exception.

package tools.jackson.databind;

import com.fasterxml.jackson.annotation.JsonValue;
import org.junit.jupiter.api.Test;
import tools.jackson.core.JacksonException;
import tools.jackson.core.JsonParser;
import tools.jackson.databind.annotation.JsonDeserialize;
import tools.jackson.databind.deser.std.StdDeserializer;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class Kotlin1089Test {
    @JsonDeserialize(using = Inner.Deser.class)
    public record Inner(@JsonValue long value) {
        static class Deser extends StdDeserializer<Inner> {
            protected Deser() { super(Inner.class); }

            @Override
            public Inner deserialize(JsonParser p, DeserializationContext ctxt) throws JacksonException {
                return new Inner(p.readValueAs(Long.class));
            }
        }
    }

    public record Outer(Inner inner) { }

    @Test
    public void testSerialization() throws Exception {
        var mapper = new ObjectMapper();
        var json = "{\"inner\":\"123\"}";

        var ok = mapper.readValue(json, Outer.class);
        System.out.println("OK: " + ok);

        var tree = mapper.readTree(json);
        var ng = mapper.treeToValue(tree, Outer.class);
        System.out.println("NG: " + ng);
    }
}

The stack trace is as follows.

tools.jackson.databind.DatabindException: Cannot invoke "tools.jackson.core.util.JacksonFeatureSet.isEnabled(tools.jackson.core.util.JacksonFeature)" because "this._readCapabilities" is null
 at [No location information] (through reference chain: tools.jackson.databind.Kotlin1089Test$Outer["inner"])

	at tools.jackson.databind/tools.jackson.databind.DatabindException.wrapWithPath(DatabindException.java:111)
	at tools.jackson.databind/tools.jackson.databind.deser.bean.BeanDeserializerBase.wrapAndThrow(BeanDeserializerBase.java:1936)
	at tools.jackson.databind/tools.jackson.databind.deser.bean.BeanDeserializer._deserializeWithErrorWrapping(BeanDeserializer.java:860)
	at tools.jackson.databind/tools.jackson.databind.deser.bean.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:721)
	at tools.jackson.databind/tools.jackson.databind.deser.bean.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1503)
	at tools.jackson.databind/tools.jackson.databind.deser.bean.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:598)
	at tools.jackson.databind/tools.jackson.databind.deser.bean.BeanDeserializer.deserialize(BeanDeserializer.java:200)
	at tools.jackson.databind/tools.jackson.databind.deser.DeserializationContextExt.readRootValue(DeserializationContextExt.java:266)
	at tools.jackson.databind/tools.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:2610)
	at tools.jackson.databind/tools.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:978)
	at tools.jackson.databind/tools.jackson.databind.ObjectMapper.treeToValue(ObjectMapper.java:1291)
	at tools.jackson.databind/tools.jackson.databind.Kotlin1089Test.testSerialization(Kotlin1089Test.java:36)
	at java.base/java.lang.reflect.Method.invoke(Method.java:569)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
Caused by: java.lang.NullPointerException: Cannot invoke "tools.jackson.core.util.JacksonFeatureSet.isEnabled(tools.jackson.core.util.JacksonFeature)" because "this._readCapabilities" is null
	at tools.jackson.databind/tools.jackson.databind.DeserializationContext.isEnabled(DeserializationContext.java:456)
	at tools.jackson.databind/tools.jackson.databind.deser.std.StdDeserializer._checkFromStringCoercion(StdDeserializer.java:1499)
	at tools.jackson.databind/tools.jackson.databind.deser.std.StdDeserializer._checkFromStringCoercion(StdDeserializer.java:1474)
	at tools.jackson.databind/tools.jackson.databind.deser.std.StdDeserializer._parseLong(StdDeserializer.java:985)
	at tools.jackson.databind/tools.jackson.databind.deser.jdk.NumberDeserializers$LongDeserializer.deserialize(NumberDeserializers.java:626)
	at tools.jackson.databind/tools.jackson.databind.deser.jdk.NumberDeserializers$LongDeserializer.deserialize(NumberDeserializers.java:591)
	at tools.jackson.databind/tools.jackson.databind.DeserializationContext._readValue(DeserializationContext.java:406)
	at tools.jackson.databind/tools.jackson.databind.DeserializationContext.readValue(DeserializationContext.java:390)
	at tools.jackson.databind/tools.jackson.databind.DeserializationContext.readValue(DeserializationContext.java:366)
	at tools.jackson.core@3.1.0-SNAPSHOT/tools.jackson.core.base.ParserMinimalBase.readValueAs(ParserMinimalBase.java:810)
	at tools.jackson.databind/tools.jackson.databind.Kotlin1089Test$Inner$Deser.deserialize(Kotlin1089Test.java:20)
	at tools.jackson.databind/tools.jackson.databind.Kotlin1089Test$Inner$Deser.deserialize(Kotlin1089Test.java:15)
	at tools.jackson.databind/tools.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:569)
	at tools.jackson.databind/tools.jackson.databind.deser.bean.BeanDeserializer._deserializeWithErrorWrapping(BeanDeserializer.java:858)
	... 12 more

Expected behavior

No response

Additional context

This is due to the following issue reported to kotlin-module.
FasterXML/jackson-module-kotlin#1089

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions