|
| 1 | +package com.fasterxml.jackson.dataformat.xml.unwrapped; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import junit.framework.Assert; |
| 7 | + |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +import com.fasterxml.jackson.annotation.JsonInclude.Include; |
| 11 | +import com.fasterxml.jackson.dataformat.xml.XmlMapper; |
| 12 | +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; |
| 13 | +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; |
| 14 | +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement; |
| 15 | + |
| 16 | +public class TestIssue86 { |
| 17 | + |
| 18 | + @Test |
| 19 | + public void deserializeUnwrappedListWhenLocalNameForRootElementAndXmlPropertyMatch() throws Exception { |
| 20 | + final String source = |
| 21 | + "<test id=\"0\">" + |
| 22 | + "<test id=\"0.1\">" + |
| 23 | + "<test id=\"0.1.1\"/>" + |
| 24 | + "</test>" + |
| 25 | + "<test id=\"0.2\"/>" + |
| 26 | + "<test id=\"0.3\">" + |
| 27 | + "<test id=\"0.3.1\"/>" + |
| 28 | + "</test>" + |
| 29 | + "</test>"; |
| 30 | + |
| 31 | + final Issue86 before = new Issue86( |
| 32 | + "0", |
| 33 | + Arrays.asList( |
| 34 | + new Issue86( |
| 35 | + "0.1", |
| 36 | + Arrays.asList( |
| 37 | + new Issue86( |
| 38 | + "0.1.1", |
| 39 | + null))), |
| 40 | + new Issue86( |
| 41 | + "0.2", |
| 42 | + null), |
| 43 | + new Issue86( |
| 44 | + "0.3", |
| 45 | + Arrays.asList( |
| 46 | + new Issue86( |
| 47 | + "0.3.1", |
| 48 | + null))))); |
| 49 | + |
| 50 | + final XmlMapper mapper = new XmlMapper(); |
| 51 | + mapper.setSerializationInclusion(Include.NON_NULL); |
| 52 | + |
| 53 | + final String xml = mapper.writeValueAsString(before); |
| 54 | + Assert.assertEquals(source, xml); |
| 55 | + |
| 56 | + final Issue86 after = mapper.readValue(xml, Issue86.class); |
| 57 | + Assert.assertEquals(before, after); |
| 58 | + } |
| 59 | + |
| 60 | + @JacksonXmlRootElement(localName = "test") |
| 61 | + public static class Issue86 { |
| 62 | + |
| 63 | + @JacksonXmlProperty(localName = "id", isAttribute = true) |
| 64 | + private String id; |
| 65 | + |
| 66 | + @JacksonXmlElementWrapper(useWrapping = false) |
| 67 | + @JacksonXmlProperty(localName = "test") |
| 68 | + private List<Issue86> children; |
| 69 | + |
| 70 | + public Issue86() {} |
| 71 | + |
| 72 | + public Issue86(final String id, final List<Issue86> children) { |
| 73 | + this.id = id; |
| 74 | + this.children = children; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public boolean equals(final Object other) { |
| 79 | + if (other == null) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + |
| 83 | + if (other == this) { |
| 84 | + return true; |
| 85 | + } |
| 86 | + |
| 87 | + if (!(other instanceof Issue86)) { |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + final Issue86 otherIssue86 = (Issue86) other; |
| 92 | + return otherIssue86.id.equals(id) && otherIssue86.children.equals(children); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments