jackson-dataformat-xml icon indicating copy to clipboard operation
jackson-dataformat-xml copied to clipboard

XmlMapper does not support JsonParser.Feature.STRICT_DUPLICATE_DETECTION

Open cpopp opened this issue 11 years ago • 3 comments

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.

cpopp avatar Apr 21 '14 20:04 cpopp

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?

cowtowncoder avatar May 20 '20 20:05 cowtowncoder

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"));
	}
}`

cpopp avatar May 20 '20 22:05 cpopp

Thank you @cpopp that helps.

cowtowncoder avatar May 21 '20 22:05 cowtowncoder