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

Deserialization should throw error if wrapper tag name is wrong

Open neon-paradise opened this issue 7 years ago • 5 comments

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.

neon-paradise avatar Nov 13 '18 08:11 neon-paradise

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.

valery1707 avatar Nov 13 '18 21:11 valery1707

Is there any way around this problem or it's impossible?

neon-paradise avatar Nov 14 '18 07:11 neon-paradise

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.

selimok avatar Mar 13 '19 11:03 selimok

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.

cowtowncoder avatar Oct 03 '19 17:10 cowtowncoder

Quick note: hoping to address this for 2.12, probably adding a Feature to enable/disable verification.

cowtowncoder avatar Jul 04 '20 02:07 cowtowncoder