Skip to content

Deserialization of enums with name defined with different cases leads to InvalidDefinitionException: Multiple fields representing property #4409

@sbailliez

Description

@sbailliez

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

I'm using a thirdparty client that is deserializing json where an enum is made of variations like RGBA and RGBa. When upgrading our dependencies from Jackson 2.14.1 to 2.16.1 this API started failing with

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Multiple fields representing property "rgba" ...

Version Information

2.16.1

Looks like the bug was introduced in 2.16.0 as it works with 2.15.4

Reproduction

    public static class Bug {
        public enum ColorMode {
            RGB, RGBa, RGBA
        }
        public ColorMode colorMode;
    }

    @Test
    public void enumFails() throws Exception {
        String json = "{ \"color_mode\": \"RGBa\"}";
        ObjectMapper mapper = new ObjectMapper();
        mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        Bug bug = mapper.readValue(json, Bug.class);
    }

Expected behavior

This should succeed.

Additional context

For this particular, I worked around this issue right now by patching the objectmapper created within the client and adding a custom deserializer for that enum:

    public static class EnumDeserializer extends StdDeserializer<File.ColorMode> {
        public EnumDeserializer() {
            this(null);
        }

        public EnumDeserializer(Class<?> vc) {
            super(vc);
        }

        @Override
        public File.ColorMode deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
            JsonNode node = p.getCodec().readTree(p);
            String value = node.asText();
            return value != null ? File.ColorMode.valueOf(value) : null;
        }
    }
    
    // .... reflective access to find the objectmapper in the thirdparty client...
    mapper.registerModule(new SimpleModule("patch-enum")
         .addDeserializer(File.ColorMode.class, new EnumDeserializer()));

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