Skip to content

Argument type mismatch for enum with @JsonCreator that takes String, gets JSON Number #3006

@GrozaAnton

Description

@GrozaAnton

Describe the bug
Array of number does not parse.
problem: argument type mismatch (through reference chain: java.lang.Object[][0])

Version information
2.12.0

To Reproduce
Sample in Java

public class Foo {

    public static void main(String[] args) throws JsonProcessingException {
        Type type = Operation[].class;
        List<String> sources = List.of("[1,3]", "[\"1\",\"3\"]");

        ObjectMapper mapper = new CustomObjectMapper();

            sources.forEach(s -> {
                try {
                    mapper.readValue(s, mapper.constructType(type));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });

    }

    public static class CustomJsonFactory extends JsonFactory {
        public CustomJsonFactory() {
            super();
            enable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN);
            enable(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS);
        }
    }

    public static class CustomObjectMapper extends ObjectMapper {
        public CustomObjectMapper() {
            super(new CustomJsonFactory());

            setSerializationInclusion(JsonInclude.Include.NON_NULL);

            // Disabling failure on received unknown properties
            configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            configure(JsonParser.Feature.INCLUDE_SOURCE_IN_LOCATION, false);
            configure(DeserializationFeature.ACCEPT_FLOAT_AS_INT, false);

            // Disabling getter and setter during bean mapping
            setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
            setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE);
            setVisibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.NONE);
            setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE);
        }
    }

    @RequiredArgsConstructor
    enum Operation {
        ONE(1L),
        TWO(2L),
        THREE(3L);

        private static final Map<Long, Operation> mapping;

        static {
            HashMap<Long, Operation> operations = new HashMap<>();
            for (Operation operation : Operation.values()) {
                operations.put(operation.getId(), operation);
            }

            mapping = Collections.unmodifiableMap(operations);
        }

        @Getter
        private final long id;

        @JsonCreator
        public static Operation forValue(@NonNull final String idStr) {
            Operation candidate = mapping.get(Long.parseLong(idStr));
            if (candidate == null) {
                throw new IllegalArgumentException("Unable " + idStr);
            }
            return candidate;
        }

        @JsonValue
        public long toValue() {
            return id;
        }
    }
}

Expected behavior
On version 2.10.5.1 I have two works cases for my sample "[1,3]" and "["1","3"]"
Since 2.11.0 case with JSON "[1,3]" does not work;
I did not find in the change logs changes of this behavior.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions