Skip to content

DeserializationFeature.READ_ENUMS_USING_TO_STRING not dynamically changeable with 2.7 #1161

@asa-git

Description

@asa-git

Hiya,
There seems to be an issue with the ObjectReader objects not taking into account the configuration set in them instead of the main object mapper instance.

Here is a simple example:

Having this inner class:

    static class Data {
        enum Type {
            A, B, C;

            @Override
            public String toString() {
                return this.name().toLowerCase();
            };
        };

        private final Type type;
        private final String value;

        @JsonCreator
        public Data(//
                @JsonProperty("type") Type type, //
                @JsonProperty("value") String value) {
            this.type = type;
            this.value = value;
        }

        @Override
        public String toString() {
            return "Data [type=" + type + ", value=" + value + "]";
        }
    }

The following will fail with an exception when parsing the enum:

        final String src = "[ { \"type\": \"a\", \"value\": \"1\" }, { \"type\": \"b\", \"value\": \"2\" }]";
        final ObjectMapper mapper = new ObjectMapper();

        MappingIterator<Data> iterator = mapper //
                .readerFor(Data.class) //
                .with(DeserializationFeature.READ_ENUMS_USING_TO_STRING) //
                .readValues(src);
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

to get it to work it seems we need to move the DeserializationFeature configuration to the ObjectMapper itself:

        final String src = "[ { \"type\": \"a\", \"value\": \"1\" }, { \"type\": \"b\", \"value\": \"2\" }]";
        final ObjectMapper mapper = new ObjectMapper() //
                .configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true);

        MappingIterator<Data> iterator = mapper //
                .readerFor(Data.class) //
                .readValues(src);
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

Tested with both version 2.7.1-1 and 2.7.2.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions