-
-
Notifications
You must be signed in to change notification settings - Fork 231

Description
Jackson version: 2.9.0
Occurs with and without woodstox.
I am trying to deserialize some external XML containing the attribute xml:space="preserve"
. This fails with MismatchedInputException: Cannot construct instance of TestDeserialization$Inner
(although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('Content 2')
XML Example:
<Outer>
<inner>
<alpha xml:space="preserve">Content 1</alpha>
<beta>Content 2</beta>
</inner>
<inner>
<alpha xml:space="preserve">Content 3</alpha>
<beta>Content 4</beta>
</inner>
</Outer>
This works fine without xml:space or with xml:space on the <beta>
tags.
public class TestDeserialization {
static class Outer {
@JacksonXmlElementWrapper(useWrapping = false) public List<Inner> inner;
}
static class Inner {
public String alpha;
public String beta;
}
public static void main(String[] args) throws IOException {
//Same xml as above
String xml = "<Outer><inner><alpha xml:space=\"preserve\">Content 1</alpha><beta>Content 2</beta></inner><inner><alpha xml:space=\"preserve\">Content 3</alpha><beta>Content 4</beta></inner></Outer>";
ObjectMapper mapper = new XmlMapper();
String noSpace = xml.replace(" xml:space=\"preserve\"", "");
System.out.println("Works fine without xml:space:" + noSpace);
System.out.println(mapper.readValue(noSpace, Outer.class).inner.get(0).beta);
String spaceBeta = noSpace.replace("<beta", "<beta xml:space=\"preserve\"");
System.out.println("Works fine with xml:space on <beta" + spaceBeta);
System.out.println(mapper.readValue(spaceBeta, Outer.class).inner.get(0).beta);
//This throws MismatchedInputException: Cannot construct instance of `TestDeserialization$Inner`
System.out.println(mapper.readValue(xml, Outer.class).inner.get(0).beta);
}
}
Below is some more code for serialization and deserialization round-trips. It works fine if the List in Outer has useWrapping = true, it works fine if you replace List with Inner. It also works if you swap the beta and alpha fields in the inner class - as shown above xml:space is fine on the last element in the XML. The order of the class fields does only matter because its the order of the fields in the XML.
public class Test {
static class Outer {
//It works fine if useWrapping = true
//It also works if inner is a simple field instead of a collection
@JacksonXmlElementWrapper(useWrapping = false) public List<Inner> inner;
}
static class Inner {
public String alpha;
//It works if beta is before alpha or the beta is missing from this class
public String beta;
}
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new XmlMapper();
Inner inner = new Inner();
inner.alpha = "Content 1";
inner.beta = "Content 2";
Outer outer = new Outer();
outer.inner = Arrays.asList(inner, inner);
String xml = mapper.writeValueAsString(outer);
//This works
System.out.println(mapper.readValue(xml, Outer.class).inner.get(0).beta);
String xmlWithSpace = xml.replace("<alpha>", "<alpha xml:space=\"preserve\">");
System.out.println(xmlWithSpace);
//This throws MismatchedInputException: Cannot construct instance of `Test2$Inner`
System.out.println(mapper.readValue(xmlWithSpace, Outer.class).inner.get(0).beta);
}
}