jackson-dataformat-xml
jackson-dataformat-xml copied to clipboard
XmlMapper does not support JsonParser.Feature.STRICT_DUPLICATE_DETECTION
I configured an XmlMapper with the parser feature for rejecting duplicates, as such:
XmlMapper mapper = new XmlMapper();
mapper.configure(JsonParser.Feature.STRICT_DUPLICATE_DETECTION, true);
and the XmlMapper does not seem to support the feature as duplicates were allowed through.
Would actually need bit more information on usage here (yes, very long time for feedback :) ) -- this could be for JsonNode usage, or maybe "untyped" (java.lang.Object) or Maps?
I was trying something similar to this expecting that the fourth step would reject the duplicate field as the third does:
public static class MyObject {
public String my_field;
}
public static void main(String[] args) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
XmlMapper xmlMapper = new XmlMapper();
// 1. default allows duplicate fields
objectMapper.readValue("{\"my_field\": \"value_a\", \"my_field\": \"value_b\"}", MyObject.class);
xmlMapper.readValue("<MyObject><my_field>value_a</my_field><my_field>value_b</my_field></MyObject>", MyObject.class);
// 2. configure rejection of duplicate fields
objectMapper.configure(JsonParser.Feature.STRICT_DUPLICATE_DETECTION, true);
xmlMapper.configure(JsonParser.Feature.STRICT_DUPLICATE_DETECTION, true);
// 3. verify json rejects duplicate fields
try {
objectMapper.readValue("{\"my_field\": \"value_a\", \"my_field\": \"value_b\"}", MyObject.class);
Assert.fail("json allowed duplicate");
} catch (JsonParseException e) {
Assert.assertTrue(e.getMessage().contains("Duplicate field"));
}
// 4. verify xml rejects duplicate fields
try {
xmlMapper.readValue("<MyObject><my_field>value_a</my_field><my_field>value_b</my_field></MyObject>", MyObject.class);
Assert.fail("xml allowed duplicate");
} catch (JsonParseException e) {
Assert.assertTrue(e.getMessage().contains("Duplicate field"));
}
}`
Thank you @cpopp that helps.