question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`@JacksonInject` will try to fetch deserializer for injected creator property if there is visible field

See original GitHub issue

Everything works as expected if you remove

.withVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)

or

mapper.setDefaultSetterInfo(JsonSetter.Value.construct(Nulls.AS_EMPTY, Nulls.AS_EMPTY));

Otherwise error thrown: Cannot create empty instance of [simple type, class dk.xakeps.jacksontest.Test$Internal], no default Creator

jackson-databind 2.9.9.3 Test class:

import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.annotation.Nulls;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.InjectableValues;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

public class Test {
    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();

        mapper.setDefaultSetterInfo(JsonSetter.Value.construct(Nulls.AS_EMPTY, Nulls.AS_EMPTY));
        mapper.setVisibility(mapper.getVisibilityChecker().withVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY));

        TestCase o = mapper.reader(new InjectableValues.Std().addValue(Internal.class, new Internal("test")))
                .forType(TestCase.class)
                .readValue("{\"id\":3}");
        System.out.println(o.str);
    }

    public static final class TestCase {
        private final Internal str;
        private final int id;

        @JsonCreator
        public TestCase(@JacksonInject Internal str, @JsonProperty("id") int id) {
            this.str = str;
            this.id = id;
        }
    }

    public static final class Internal {
        private final String val;

        public Internal(String val) {
            this.val = val;
        }
    }
}

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
solonovamaxcommented, Apr 19, 2021

I found this issue on my own (see the now-closed #3124), and here’s a smaller test, if you want that:

import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.InjectableValues;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;

public class CursedInjectionDeserializerTest {
    
    @Test
    void testInjectionBlackMagic() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setInjectableValues(new InjectableValues.Std().addValue(Injected.class, new Injected()));
        
        TestCase test = mapper.readValue("{\"id\": 3}", TestCase.class);
        System.out.println(test.id);
    }
    
    
    public static final class TestCase {
        private final Injected injected;
        private final int      id;
        
        public TestCase(@JacksonInject Injected injected,
                        @JsonProperty("id") int id) {
            this.injected = injected;
            this.id = id;
        }
    }
    
    public static final class Injected {
        private void setAbcd(Void a) {}
        
        private void setAbcd(Boolean a) {}
    }
}

I used some parts of what I found in #3124, and the existing test case you had.

0reactions
cowtowncodercommented, Sep 6, 2020

One sidenote: Jackson 3.0 does not seem to fail for the test, for whatever reason.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Injecting ObjectMapper into a deserialized object using ...
I've been trying to get an ObjectMapper injected into a Jackson-deserialized object using @JacksonInject and @JsonCreator, but to no avail.
Read more >
Jackson Annotation Examples - Baeldung
In this tutorial, we'll do a deep dive into Jackson Annotations. ... It's very useful when we need to deserialize some JSON that...
Read more >
Using @JacksonInject to inject values during deserialization
@JacksonInject annotation is used to indicate that value of annotated property will be injected during deserialization. This is useful if we ...
Read more >
Jackson Annotations for JSON - Spring Framework Guru
You can also use @JsonProperty annotation during deserialization when the property names of the JSON and the field names of the Java object...
Read more >
Index (Jackson-annotations 2.13.0 API) - FasterXML
Method called to check whether this generator instance can be used for ... any of the properties results in `true`, and names of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found