Skip to content

@JacksonInject fails when trying to inject and instance of ObjectMapper with a PROPERTIES based JsonCreator contructor #1119

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
chapmbk opened this issue Feb 5, 2016 · 3 comments

Comments

@chapmbk
Copy link

chapmbk commented Feb 5, 2016

This following example works as expected

public class JacksonForUpdate {

    public static class Bean {
        private String name;
        private Map<String, Object> properties;

        public void setName(String name) {
            this.name = name;
        }

        @JsonCreator
        public Bean(JsonNode node, @JacksonInject ObjectMapper mapper) throws IOException {
            this.properties = mapper.convertValue(node, Map.class);
            mapper.readerForUpdating(this).readValue(node);
        }

        @Override
        public String toString() {
            return "Bean{" +
                "name='" + name + '\'' +
                ", properties=" + properties +
                '}';
        }
    }

    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        // make the mapper to be available for the bean
        mapper.setInjectableValues(new InjectableValues.Std().addValue(ObjectMapper.class, mapper));
        final ObjectNode objectNode = mapper.createObjectNode();
        objectNode.put("name", "value");
        System.out.println(
            mapper.treeToValue(
                objectNode, Bean.class));
    }
}

The next example fails with an exception: Conflicting setter definitions for property "config":

com.fasterxml.jackson.databind.ObjectMapper#setConfig(1 params) vs com.fasterxml.jackson.databind.ObjectMapper#setConfig(1 params)

public class JacksonForUpdate {

    public static class Bean {
        private String name;

        @JsonCreator
        public Bean(@JsonProperty("name") String name, @JacksonInject ObjectMapper mapper) throws IOException {
            this.name = name;
        }

        @Override
        public String toString() {
            return "Bean{" +
                "name='" + name + '\'' +
                '}';
        }
    }

    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        // make the mapper to be available for the bean
        mapper.setInjectableValues(new InjectableValues.Std().addValue(ObjectMapper.class, mapper));
        final ObjectNode objectNode = mapper.createObjectNode();
        objectNode.put("name", "value");
        System.out.println(
            mapper.treeToValue(
                objectNode, Bean.class));
    }
}
@cowtowncoder
Copy link
Member

Hmmh. Interesting. Thank you for reporting this.

@cowtowncoder
Copy link
Member

I suspect this is same as/related to #962: that is, injectable value being introspected.
So sort of secondary fail.

@cowtowncoder
Copy link
Member

Ok, yes; I think this is duplicate of #962: I will close this one just based on order of reporting, problem itself is valid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants