|
| 1 | +package com.fasterxml.jackson.dataformat.xml.failing; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.annotation.*; |
| 8 | + |
| 9 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 10 | +import com.fasterxml.jackson.dataformat.xml.XmlTestBase; |
| 11 | +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; |
| 12 | +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; |
| 13 | +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement; |
| 14 | + |
| 15 | +public class PolymorphicList576Test extends XmlTestBase |
| 16 | +{ |
| 17 | + @JacksonXmlRootElement(localName = "wrapper") |
| 18 | + static class Wrapper extends Base { |
| 19 | + |
| 20 | + @JacksonXmlProperty(localName = "item") |
| 21 | + @JacksonXmlElementWrapper(useWrapping = false) |
| 22 | + public List<Item> items = new ArrayList<>(); |
| 23 | + |
| 24 | + public Wrapper(List<Item> items) { |
| 25 | + this.items = items; |
| 26 | + } |
| 27 | + |
| 28 | + public Wrapper() { |
| 29 | + } |
| 30 | + |
| 31 | + public List<Item> getItems() { |
| 32 | + return items; |
| 33 | + } |
| 34 | + |
| 35 | + public void setItems(List<Item> items) { |
| 36 | + this.items = items; |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | + @Override |
| 42 | + public String toString() { |
| 43 | + return "Wrapper{" + |
| 44 | + "items=" + items + |
| 45 | + '}'; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @JacksonXmlRootElement(localName = "item") |
| 50 | + @JsonInclude(JsonInclude.Include.NON_NULL) |
| 51 | + static class Item { |
| 52 | + |
| 53 | + private String id; |
| 54 | + |
| 55 | + public Item(String id) { |
| 56 | + this.id = id; |
| 57 | + } |
| 58 | + |
| 59 | + public Item() { |
| 60 | + } |
| 61 | + |
| 62 | + public String getId() { |
| 63 | + return id; |
| 64 | + } |
| 65 | + |
| 66 | + public void setId(String id) { |
| 67 | + this.id = id; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public String toString() { |
| 72 | + return "Item{" + |
| 73 | + "id='" + id + '\'' + |
| 74 | + '}'; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type") |
| 79 | + @JsonSubTypes({ |
| 80 | + @JsonSubTypes.Type(value = Wrapper.class, name = "wrapper") |
| 81 | + }) |
| 82 | + @JsonInclude(JsonInclude.Include.NON_NULL) |
| 83 | + static class Base { |
| 84 | + } |
| 85 | + |
| 86 | + /* |
| 87 | + /******************************************************** |
| 88 | + /* Test methods |
| 89 | + /******************************************************** |
| 90 | + */ |
| 91 | + |
| 92 | + private final ObjectMapper XML_MAPPER = newMapper(); |
| 93 | + |
| 94 | + public void test_3itemsInXml_expect_3itemsInDeserializedObject() throws Exception { |
| 95 | + String xmlString = |
| 96 | + "<?xml version='1.0' encoding='UTF-8'?>\n" |
| 97 | + +"<wrapper type='wrapper'>\n" |
| 98 | + +" <item><id>1</id></item>\n" |
| 99 | + +" <item><id>2</id></item>\n" |
| 100 | + +" <item><id>3</id></item>\n" |
| 101 | + +"</wrapper>\n" |
| 102 | + ; |
| 103 | + Base base = XML_MAPPER.readValue(xmlString, Base.class); |
| 104 | + assertEquals(3, ((Wrapper)base).getItems().size()); |
| 105 | + } |
| 106 | + |
| 107 | + public void test_2itemsInObject_expect_2itemsInObjectAfterRoundTripDeserializationToBaseClass() throws Exception { |
| 108 | + Wrapper wrapper = new Wrapper(); |
| 109 | + Item item1 = new Item("1"); |
| 110 | + Item item2 = new Item("2"); |
| 111 | + wrapper.setItems(Arrays.asList(item1, item2)); |
| 112 | + |
| 113 | + String writeValueAsString = XML_MAPPER.writeValueAsString(wrapper); |
| 114 | + Base base = XML_MAPPER.readValue(writeValueAsString, Base.class); |
| 115 | + |
| 116 | + assertEquals(2, ((Wrapper)base).getItems().size()); |
| 117 | + } |
| 118 | +} |
0 commit comments