Deserialization should throw error if wrapper tag name is wrong
I have 3 data classes with jaxb annotations:
@XmlRootElement(name = "root")
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {
public Parent parent;
}
public class Parent {
@XmlElementWrapper(name = "children")
@XmlElement(name = "child")
public List<Child> children;
}
public class Child {
public String name;
public Integer age;
}
I want deserialize xml string to objects for this classes. My code for serialize:
XmlMapper jacksonMapper = new XmlMapper();
jacksonMapper.registerModule(new JaxbAnnotationModule());
Root root = jacksonMapper.readValue(xml, Root.class);
If I send that xml, then it's work perfect. I got one child in list.
<root>
<parent>
<children>
<child>
<name>John</name>
<age>6</age>
</child>
</children>
</parent>
</root>
But if I send that xml, then it's worked without errors, in spite of tag name is wrong.
<root>
<parent>
<children>
<zzzz>
<name>John</name>
<age>6</age>
</zzzz>
</children>
</parent>
</root>
What am I doing wrong? Or is this correct conduct? If I used javax.xml.bind.Unmarshaller then it ignore wrong tags.
As I understand, Jackson is ignoring wrapping tag at all while deserialize array element. Maybe this came from JSON because array items in them don't contains any named wrappers and that names don't need to be checked.
And if you set EventHandler into javax.xml.bind.Unmarshaller then you will see that JaxB check names of wrapper tag and allow to decide to fail or continue parsing process.
Is there any way around this problem or it's impossible?
Hi,
Same problem here. I expect the root element name (either using the class name or localName attribute of @JacksonXmlRootElement annotation) is considered while deserialization and in case of non-matching names the deserialization should fail.
e.g. I have a class like this:
@Data
@JacksonXmlRootElement(localName="root")
public static class Root {
private String sub;
}
and I expect the deserialization of
<root>
<sub>Hello</sub>
</root>
succeeds, while the deserialization of
<someDifferentRootElementNameHere>
<sub>Hello</sub>
</someDifferentRootElementNameHere>
fails. But both will be deserialized and I cannot find any deserialization feature to avoid this.
Thanks.
So: correct, as things are, matching of "item" entries is not verified. I do not remember exact reasons for this, but I don't think it is fundamentally impossible, but at the same time not trivial to do (if it was, it would already be checked).
It would be great to fix this, but I haven't had time to look into the issue. If anyone has time to dig deeper, and add notes -- extra information on handling usually helps figure out solution -- that would be helpful.
Quick note: hoping to address this for 2.12, probably adding a Feature to enable/disable verification.