|
| 1 | +package com.fasterxml.jackson.dataformat.xml.lists; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 7 | +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
| 8 | +import com.fasterxml.jackson.dataformat.xml.XmlMapper; |
| 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.JacksonXmlRootElement; |
| 12 | + |
| 13 | +public class IterableCollectionBuilder646Test extends XmlTestBase { |
| 14 | + |
| 15 | + @JsonDeserialize(builder = Parent.Builder.class) |
| 16 | + @JacksonXmlRootElement(localName = "parent") |
| 17 | + static class Parent { |
| 18 | + private final List<Child> children; |
| 19 | + |
| 20 | + private Parent(List<Child> children) { |
| 21 | + this.children = children; |
| 22 | + } |
| 23 | + |
| 24 | + @JsonProperty("child") |
| 25 | + @JacksonXmlElementWrapper(useWrapping = false) |
| 26 | + public List<Child> getChildren() { |
| 27 | + return children; |
| 28 | + } |
| 29 | + |
| 30 | + static class Builder { |
| 31 | + private final List<Child> children = new ArrayList<>(); |
| 32 | + |
| 33 | + @JsonProperty("child") |
| 34 | + @JacksonXmlElementWrapper(useWrapping = false) |
| 35 | + public Builder children(Iterable<Child> children) { |
| 36 | + for (Child c : children) { |
| 37 | + this.children.add(c); |
| 38 | + } |
| 39 | + return this; |
| 40 | + } |
| 41 | + |
| 42 | + public Parent build() { |
| 43 | + return new Parent(children); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @JsonDeserialize(builder = Child.Builder.class) |
| 49 | + @JacksonXmlRootElement(localName = "child") |
| 50 | + static class Child { |
| 51 | + private final String id; |
| 52 | + |
| 53 | + public Child(String id) { |
| 54 | + this.id = id; |
| 55 | + } |
| 56 | + |
| 57 | + @JsonProperty("id") |
| 58 | + public String getId() { |
| 59 | + return id; |
| 60 | + } |
| 61 | + |
| 62 | + static class Builder { |
| 63 | + private String id; |
| 64 | + |
| 65 | + @JsonProperty("id") |
| 66 | + public Builder id(String id) { |
| 67 | + this.id = id; |
| 68 | + return this; |
| 69 | + } |
| 70 | + |
| 71 | + public Child build() { |
| 72 | + return new Child(id); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // -- Test Methods --// |
| 78 | + private final XmlMapper MAPPER = newMapper(); |
| 79 | + |
| 80 | + public void testIssue646() throws Exception { |
| 81 | + final String XML = "<parent><child><id>1</id></child></parent>"; |
| 82 | + Parent parent = MAPPER.readValue(XML, Parent.class); |
| 83 | + assertNotNull(parent); |
| 84 | + assertNotNull(parent.getChildren()); |
| 85 | + assertEquals(1, parent.getChildren().size()); |
| 86 | + |
| 87 | + } |
| 88 | +} |
0 commit comments