Skip to content

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

Closed
@chapmbk

Description

@chapmbk

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));
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions