Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserialization fails of lists containing elements with xml:space attribute #257

Closed
ghost opened this issue Aug 24, 2017 · 2 comments
Closed
Milestone

Comments

@ghost
Copy link

ghost commented Aug 24, 2017

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);
    }
}
@paulharkink
Copy link

I can confirm this issue still exists (jackson-dataformat-xml 2.10.3)

@cowtowncoder cowtowncoder added this to the 2.12.0 milestone Jun 7, 2020
@cowtowncoder
Copy link
Member

I can not reproduce the issue any more; fixed by recent changes (possibly fix for #390).
Marking as fixed in 2.12.0, to be released.

@cowtowncoder cowtowncoder changed the title Deserialization fails of lists containing elements with xml:space Deserialization fails of lists containing elements with xml:space attribute Jun 7, 2020
valb3r added a commit to valb3r/flowable-bpmn-intellij-plugin that referenced this issue Jul 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants