|
| 1 | +package com.fasterxml.jackson.dataformat.xml.failing; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.annotation.JsonInclude; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 8 | + |
| 9 | +import com.fasterxml.jackson.dataformat.xml.XmlTestBase; |
| 10 | +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; |
| 11 | +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; |
| 12 | +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement; |
| 13 | + |
| 14 | +public class Issue393DeserTest extends XmlTestBase |
| 15 | +{ |
| 16 | + @JacksonXmlRootElement(localName = "result") |
| 17 | + @JsonInclude(JsonInclude.Include.NON_NULL) |
| 18 | + static class Value393 { |
| 19 | + private Prices prices = new Prices(); |
| 20 | + |
| 21 | + public void setPrices(Prices prices) { |
| 22 | + this.prices = prices; |
| 23 | + } |
| 24 | + |
| 25 | + @JacksonXmlProperty(localName = "prices") |
| 26 | + public Prices getPrices() { |
| 27 | + return this.prices; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | +// @JsonIgnoreProperties(ignoreUnknown = true) |
| 32 | + @JacksonXmlRootElement(localName = "prices") |
| 33 | + static class Prices { |
| 34 | + private List<Price> price = new ArrayList<Price>(); |
| 35 | + |
| 36 | + public void setPrice(List<Price> price) { |
| 37 | + this.price = price; |
| 38 | + } |
| 39 | + |
| 40 | + @JacksonXmlElementWrapper(useWrapping = false) |
| 41 | + @JacksonXmlProperty(localName = "price") |
| 42 | + public List<Price> getPrice() { |
| 43 | + return this.price; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | +// @JacksonXmlRootElement(localName = "price") |
| 48 | +// @JsonIgnoreProperties(ignoreUnknown = true) |
| 49 | + static class Price { |
| 50 | + private String price; |
| 51 | + private String num; |
| 52 | + |
| 53 | + protected Price() { } |
| 54 | + public Price(String p, String n) { |
| 55 | + price = p; |
| 56 | + num = n; |
| 57 | + } |
| 58 | + |
| 59 | + public void setPrice(String price) { |
| 60 | + this.price = price; |
| 61 | + } |
| 62 | + |
| 63 | + @JacksonXmlProperty(localName = "price") |
| 64 | + public String getPrice() { |
| 65 | + return this.price; |
| 66 | + } |
| 67 | + |
| 68 | + public void setNum(String num) { |
| 69 | + this.num = num; |
| 70 | + } |
| 71 | + |
| 72 | + @JacksonXmlProperty(localName = "num") |
| 73 | + public String getNum() { |
| 74 | + return this.num; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /* |
| 79 | + /******************************************************** |
| 80 | + /* Test methods |
| 81 | + /******************************************************** |
| 82 | + */ |
| 83 | + |
| 84 | + private final ObjectMapper MAPPER = newMapper(); |
| 85 | + |
| 86 | + // [dataform#393] |
| 87 | + public void testDeser393() throws Exception |
| 88 | + { |
| 89 | + // for debugging: |
| 90 | +/* |
| 91 | + Value393 input = new Value393(); |
| 92 | + Prices prices = new Prices(); |
| 93 | + prices.setPrice(Arrays.asList( |
| 94 | + new Price("100", "7.0"), |
| 95 | + new Price("100", "4.0") |
| 96 | + )); |
| 97 | + input.setPrices(prices); |
| 98 | +
|
| 99 | + String xml = MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(input); |
| 100 | + System.out.println("XML:\n"+xml); |
| 101 | +*/ |
| 102 | +/* |
| 103 | + String content = //"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "\n" + |
| 104 | + "<result>\n" |
| 105 | + + " <prices>\n" |
| 106 | + + " <price>\n" |
| 107 | + + " <num>100</num>\n" |
| 108 | + + " <price>7.0</price>\n" |
| 109 | + + " </price>\n" |
| 110 | + + " <price>\n" |
| 111 | + + " <num>100</num>\n" |
| 112 | + + " <price>4.0</price>\n" |
| 113 | + + " </price>" |
| 114 | + + " </prices>\n" |
| 115 | + + "</result>"; |
| 116 | + Value393 result = MAPPER.readValue(content, Value393.class); |
| 117 | + assertNotNull(result); |
| 118 | +*/ |
| 119 | + |
| 120 | + String content = |
| 121 | + "<prices>\n" |
| 122 | + + " <price>\n" |
| 123 | + + " <num>100</num>\n" |
| 124 | + + " <price>7.0</price>\n" |
| 125 | + + " </price>\n" |
| 126 | + + " <price>\n" |
| 127 | + + " <num>100</num>\n" |
| 128 | + + " <price>4.0</price>\n" |
| 129 | + + " </price>" |
| 130 | + + "</prices>\n"; |
| 131 | + Prices result = MAPPER.readValue(content, Prices.class); |
| 132 | + assertNotNull(result); |
| 133 | + } |
| 134 | +} |
0 commit comments