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
- Add tests related to #4409 — committed to FasterXML/jackson-databind by cowtowncoder 3 years ago
- Add tests related to #4409 (#4411) — committed to FasterXML/jackson-databind by cowtowncoder 3 years ago
- Refactoring after #4409 solved — committed to FasterXML/jackson-databind by cowtowncoder 3 years ago
- Refactoring JsonFormat detection, post #4409 (#4418) — committed to FasterXML/jackson-databind by cowtowncoder 3 years ago
Ok. The trick is to check what
BasicBeanDescription.findExpectedFormat()does, but fromPOJOPropertiesCollector– and if we do that, I think ideally it’d be collected byPOJOPropertiesCollectorand then handed toBasicBeanDescription(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:
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
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
PropertyNamingStrategywas 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
EnumFeatureto 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.