|
| 1 | +package com.fasterxml.jackson.dataformat.xml.lists; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.dataformat.xml.XmlMapper; |
| 8 | +import com.fasterxml.jackson.dataformat.xml.XmlTestBase; |
| 9 | +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; |
| 10 | +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; |
| 11 | +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement; |
| 12 | + |
| 13 | +// [dataformat-xml#256] |
| 14 | +public class ListDeser256Test extends XmlTestBase |
| 15 | +{ |
| 16 | + static class ExampleObject { |
| 17 | + public List<LevelOne> levelOne; |
| 18 | + |
| 19 | + public List<LevelOne> getLevelOne() { return levelOne; } |
| 20 | + |
| 21 | + static class LevelOne { |
| 22 | + public LevelTwo levelTwo; |
| 23 | + |
| 24 | + public LevelTwo getLevelTwo() { return levelTwo; } |
| 25 | + } |
| 26 | + |
| 27 | + static class LevelTwo { |
| 28 | + public String fieldOne; |
| 29 | + public String fieldTwo; |
| 30 | + |
| 31 | + public String getFieldOne() { return fieldOne; } |
| 32 | + public String getFieldTwo() { return fieldTwo; } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + @JacksonXmlRootElement(localName = "Object") |
| 37 | + static abstract class ExampleObjectMixin { |
| 38 | + @JacksonXmlElementWrapper(useWrapping = false) |
| 39 | + @JacksonXmlProperty(localName = "LevelOne") |
| 40 | + abstract List<ExampleObject.LevelOne> getLevelOne(); |
| 41 | + |
| 42 | + @JacksonXmlElementWrapper(useWrapping = false) |
| 43 | + @JsonProperty("LevelOne") // This is a workaround to set the element name, @JacksonXmlProperty seems to get ignored |
| 44 | + abstract void setLevelOne(List<ExampleObject.LevelOne> levelOne); |
| 45 | + } |
| 46 | + |
| 47 | + static abstract class LevelOneMixin { |
| 48 | + @JacksonXmlProperty(localName = "LevelTwo") |
| 49 | + abstract ExampleObject.LevelTwo getLevelTwo(); |
| 50 | + |
| 51 | + @JsonProperty("LevelTwo") |
| 52 | + abstract void setLevelTwo(ExampleObject.LevelTwo levelTwo); |
| 53 | + } |
| 54 | + |
| 55 | + static abstract class LevelTwoMixin { |
| 56 | + @JacksonXmlProperty(localName = "Field1") |
| 57 | + abstract String getFieldOne(); |
| 58 | + |
| 59 | + @JsonProperty("Field1") |
| 60 | + abstract void setFieldOne(String fieldOne); |
| 61 | + |
| 62 | + @JacksonXmlProperty(localName = "Field2") |
| 63 | + abstract String getFieldTwo(); |
| 64 | + |
| 65 | + @JsonProperty("Field2") |
| 66 | + abstract void setFieldTwo(String fieldTwo); |
| 67 | + } |
| 68 | + |
| 69 | + /* |
| 70 | + /******************************************************** |
| 71 | + /* Test methods |
| 72 | + /******************************************************** |
| 73 | + */ |
| 74 | + |
| 75 | + public void testDeser256() throws Exception |
| 76 | + { |
| 77 | + final String XML = |
| 78 | + "<Object>\n" + |
| 79 | + " <LevelOne> <!-- This is an array element -->\n" + |
| 80 | + " <LevelTwo>\n" + |
| 81 | + " <Field1>Value1</Field1>\n" + |
| 82 | + " <Field2>Value2</Field2>\n" + |
| 83 | + " </LevelTwo>\n" + |
| 84 | + " </LevelOne>\n" + |
| 85 | + "</Object>"; |
| 86 | + final XmlMapper mapper = XmlMapper.builder() |
| 87 | + .addMixIn(ExampleObject.class, ExampleObjectMixin.class) |
| 88 | + .addMixIn(ExampleObject.LevelOne.class, LevelOneMixin.class) |
| 89 | + .addMixIn(ExampleObject.LevelTwo.class, LevelTwoMixin.class) |
| 90 | + .build(); |
| 91 | + ExampleObject result = mapper.readValue(XML, ExampleObject.class); |
| 92 | + assertNotNull(result); |
| 93 | + assertNotNull(result.levelOne); |
| 94 | + assertEquals(1, result.levelOne.size()); |
| 95 | + assertNotNull(result.levelOne.get(0)); |
| 96 | + assertNotNull(result.levelOne.get(0).levelTwo); |
| 97 | + assertEquals("Value1", result.levelOne.get(0).levelTwo.fieldOne); |
| 98 | + assertEquals("Value2", result.levelOne.get(0).levelTwo.fieldTwo); |
| 99 | + } |
| 100 | +} |
0 commit comments