|
| 1 | +package tools.jackson.dataformat.xml.lists; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collection; |
| 5 | + |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.annotation.JsonRootName; |
| 9 | + |
| 10 | +import tools.jackson.dataformat.xml.XmlMapper; |
| 11 | +import tools.jackson.dataformat.xml.XmlTestUtil; |
| 12 | +import tools.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; |
| 13 | +import tools.jackson.dataformat.xml.annotation.JacksonXmlProperty; |
| 14 | + |
| 15 | +import static org.junit.jupiter.api.Assertions.*; |
| 16 | + |
| 17 | +/** |
| 18 | + * Test for [dataformat-xml#871]: an `xsi:nil="true"` collection element on an unwrapped |
| 19 | + * collection must read as a single null *element* within the collection, not as a null |
| 20 | + * collection (regression from the original #627 attempt). |
| 21 | + */ |
| 22 | +public class Issue871NullElementTest extends XmlTestUtil |
| 23 | +{ |
| 24 | + @JsonRootName(value = "Collection") |
| 25 | + static class CollectionWrapper { |
| 26 | + @JacksonXmlElementWrapper(useWrapping = false) |
| 27 | + @JacksonXmlProperty(localName = "e") |
| 28 | + public Collection<String> value; |
| 29 | + |
| 30 | + public CollectionWrapper() { this.value = new ArrayList<>(); } |
| 31 | + } |
| 32 | + |
| 33 | + private final XmlMapper MAPPER = new XmlMapper(); |
| 34 | + |
| 35 | + private static final String NS = "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""; |
| 36 | + |
| 37 | + @Test |
| 38 | + public void testSingleNullElement() throws Exception |
| 39 | + { |
| 40 | + String xml = "<Collection><e " + NS + " xsi:nil=\"true\"/></Collection>"; |
| 41 | + CollectionWrapper cw = MAPPER.readValue(xml, CollectionWrapper.class); |
| 42 | + |
| 43 | + assertNotNull(cw.value); |
| 44 | + assertEquals(1, cw.value.size()); |
| 45 | + assertNull(cw.value.iterator().next()); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testLeadingNullElementNotDropped() throws Exception |
| 50 | + { |
| 51 | + String xml = "<Collection><e " + NS + " xsi:nil=\"true\"/><e>x</e></Collection>"; |
| 52 | + CollectionWrapper cw = MAPPER.readValue(xml, CollectionWrapper.class); |
| 53 | + |
| 54 | + assertNotNull(cw.value); |
| 55 | + assertEquals(2, cw.value.size()); |
| 56 | + assertEquals(java.util.Arrays.asList(null, "x"), new ArrayList<>(cw.value)); |
| 57 | + } |
| 58 | +} |
0 commit comments