|
| 1 | +package tools.jackson.dataformat.xml.lists; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.annotation.*; |
| 8 | + |
| 9 | +import tools.jackson.dataformat.xml.XmlMapper; |
| 10 | +import tools.jackson.dataformat.xml.XmlTestUtil; |
| 11 | +import tools.jackson.dataformat.xml.annotation.*; |
| 12 | +import static org.junit.jupiter.api.Assertions.*; |
| 13 | + |
| 14 | +// [dataformat-xml#514]: Deserialisation to polymorphic class using |
| 15 | +// `JsonTypeInfo.Id.DEDUCTION` and `@JacksonXmlElementWrapper` fails |
| 16 | +public class DeductionWithUnwrappedList514Test extends XmlTestUtil |
| 17 | +{ |
| 18 | + static class NestedClass { |
| 19 | + @JacksonXmlProperty(localName = "Text") |
| 20 | + public String text; |
| 21 | + } |
| 22 | + |
| 23 | + @JsonTypeInfo(use = JsonTypeInfo.Id.DEDUCTION) |
| 24 | + @JsonSubTypes({ |
| 25 | + @JsonSubTypes.Type(Success514.class), |
| 26 | + @JsonSubTypes.Type(Error514.class) |
| 27 | + }) |
| 28 | + static abstract class Response514 { } |
| 29 | + |
| 30 | + static class Success514 extends Response514 { |
| 31 | + @JacksonXmlProperty(localName = "SomeData") |
| 32 | + public String someData; |
| 33 | + |
| 34 | + @JacksonXmlProperty(localName = "Detail") |
| 35 | + @JacksonXmlElementWrapper(useWrapping = false) |
| 36 | + public List<NestedClass> details; |
| 37 | + } |
| 38 | + |
| 39 | + static class Error514 extends Response514 { |
| 40 | + @JacksonXmlProperty(localName = "ErrorCode") |
| 41 | + public String errorCode; |
| 42 | + } |
| 43 | + |
| 44 | + @JsonRootName("RootTag") |
| 45 | + static class WholeResponse { |
| 46 | + @JacksonXmlProperty(localName = "Response") |
| 47 | + public Response514 response; |
| 48 | + } |
| 49 | + |
| 50 | + // Direct deserialization without deduction — should work |
| 51 | + @JsonRootName("RootTag") |
| 52 | + static class WholeResponseDirect { |
| 53 | + @JacksonXmlProperty(localName = "Response") |
| 54 | + public Success514 response; |
| 55 | + } |
| 56 | + |
| 57 | + private final XmlMapper MAPPER = newMapper(); |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testDirectDeserializationWithoutDeduction() throws Exception |
| 61 | + { |
| 62 | + String xml = |
| 63 | + "<RootTag>" |
| 64 | + + "<Response>" |
| 65 | + + "<SomeData>hello</SomeData>" |
| 66 | + + "<Detail><Text>hello world</Text></Detail>" |
| 67 | + + "</Response>" |
| 68 | + + "</RootTag>"; |
| 69 | + WholeResponseDirect result = MAPPER.readValue(xml, WholeResponseDirect.class); |
| 70 | + assertNotNull(result); |
| 71 | + assertNotNull(result.response); |
| 72 | + assertEquals("hello", result.response.someData); |
| 73 | + assertNotNull(result.response.details); |
| 74 | + assertEquals(1, result.response.details.size()); |
| 75 | + assertEquals("hello world", result.response.details.get(0).text); |
| 76 | + } |
| 77 | + |
| 78 | + // [dataformat-xml#514] |
| 79 | + @Test |
| 80 | + public void testDeductionWithUnwrappedList() throws Exception |
| 81 | + { |
| 82 | + String xml = |
| 83 | + "<RootTag>" |
| 84 | + + "<Response>" |
| 85 | + + "<SomeData>hello</SomeData>" |
| 86 | + + "<Detail><Text>hello world</Text></Detail>" |
| 87 | + + "</Response>" |
| 88 | + + "</RootTag>"; |
| 89 | + WholeResponse result = MAPPER.readValue(xml, WholeResponse.class); |
| 90 | + assertNotNull(result); |
| 91 | + assertNotNull(result.response); |
| 92 | + assertInstanceOf(Success514.class, result.response); |
| 93 | + Success514 success = (Success514) result.response; |
| 94 | + assertEquals("hello", success.someData); |
| 95 | + assertNotNull(success.details); |
| 96 | + assertEquals(1, success.details.size()); |
| 97 | + assertEquals("hello world", success.details.get(0).text); |
| 98 | + } |
| 99 | +} |
0 commit comments