jackson-databind: Deserialization of enums with name defined with different cases leads to `InvalidDefinitionException`: Multiple fields representing property

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

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 21 (18 by maintainers)

Commits related to this issue

Most upvoted comments

Ok. The trick is to check what BasicBeanDescription.findExpectedFormat() does, but from POJOPropertiesCollector – and if we do that, I think ideally it’d be collected by POJOPropertiesCollector and then handed to BasicBeanDescription (to avoid multiple introspection calls).

But in short term (2.17) duplication would be fine. This lookup is needed in 2.17 just for enum types, so we’d have something like:

    protected void _renameUsing(Map<String, POJOPropertyBuilder> propMap,
            PropertyNamingStrategy naming)
    {
        // _findFormatShape() needs to what `BasicBeanDescription.findExpectedFormat()`
        // (but need not take or use "defValue"; there's no defaulting)
        if (_type.isEnumType() && _findFormatShape() != Shape.OBJECT) {
            return;
        }
        ...
}

I might have time to look into this tomorrow, but if not I hope above helps.

Did some digging and made tests against 2.15 and 2.16

  • 2.15🔗
    • PropertyNamingStrategy NOT applied to both Serialization and Deserialization
  • 2.16 🔗
    • PropertyNamingStrategy applied to ONLY deserialization

Fixed for 2.16.2

Thanks @sbailliez for reproduction and all!

@JooHyukKim do you mean for 2.15 “NOT applied to both Serialization and Deserialization”?

@sbailliez I may have spoken too soon on this change being intentional wrt PropertyNamingStrategy. It may well be unintentional change, due to somewhat aggressive rewriting of Enum handling in 2.16 (and some in 2.15 I think).

I must say I did not remember that PropertyNamingStrategy was to have worked on enum values. But at the same time, behavior that 2.16 has out is, by know, the behavioral baseline.

I think EnumFeature to control this would be useful; that’d need to go in 2.17, and should have default settings that reflect what 2.16 does.

This could mean that strategy should, by default, apply, but may be disabled by the new EnumFeature.