-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Closed
Milestone
Description
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
Labels
No labels