Skip to content

Commit ad486b3

Browse files
authored
Added fix for iterable collection builder methods not treating iterable as a collection (#648)
1 parent ae1bc1a commit ad486b3

File tree

4 files changed

+102
-2
lines changed

4 files changed

+102
-2
lines changed

release-notes/CREDITS-2.x

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,12 @@ Arthur Chan (@arthurscchan)
252252

253253
Alex H (@ahcodedthat)
254254

255-
* Contribtued #643: XML serialization of floating-point infinity is incompatible
255+
* Contributed #643: XML serialization of floating-point infinity is incompatible
256256
with JAXB and XML Schema
257257
(2.17.0)
258+
259+
Bas Passon (@bpasson)
260+
261+
* Reported, contributed fix for #646: Deserializing fails when using builder classes
262+
with `Iterable` Collection setters
263+
(2.17.1)

release-notes/VERSION-2.x

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ Project: jackson-dataformat-xml
88

99
No changes since 2.17
1010

11+
2.17.1 (not yet released)
12+
13+
#646: Deserializing fails when using builder classes with `Iterable` Collection setters
14+
(fix contributed by Bas P)
15+
1116
2.17.0 (12-Mar-2024)
1217

1318
#324: Support use of `xsi:type` for polymorphic serialization

src/main/java/com/fasterxml/jackson/dataformat/xml/util/TypeUtil.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ public class TypeUtil
1111
public static boolean isIndexedType(JavaType type)
1212
{
1313
Class<?> cls = type.getRawClass();
14-
if (type.isContainerType() || type.isIterationType()) {
14+
// 25-Mar-2024, tatu [dataformat-xml#646]: Need to support Iterable too
15+
if (type.isContainerType() || type.isIterationType() || cls == Iterable.class) {
1516
// One special case; byte[] will be serialized as base64-encoded String, not real array, so:
1617
// (actually, ditto for char[]; thought to be a String)
1718
if (cls == byte[].class || cls == char[].class) {
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
// [dataformat-xml#646]
14+
public class IterableCollectionBuilder646Test extends XmlTestBase
15+
{
16+
@JsonDeserialize(builder = Parent.Builder.class)
17+
@JacksonXmlRootElement(localName = "parent")
18+
static class Parent {
19+
private final List<Child> children;
20+
21+
Parent(List<Child> children) {
22+
this.children = children;
23+
}
24+
25+
@JsonProperty("child")
26+
@JacksonXmlElementWrapper(useWrapping = false)
27+
public List<Child> getChildren() {
28+
return children;
29+
}
30+
31+
static class Builder {
32+
private final List<Child> children = new ArrayList<>();
33+
34+
@JsonProperty("child")
35+
@JacksonXmlElementWrapper(useWrapping = false)
36+
public Builder children(Iterable<Child> children) {
37+
for (Child c : children) {
38+
this.children.add(c);
39+
}
40+
return this;
41+
}
42+
43+
public Parent build() {
44+
return new Parent(children);
45+
}
46+
}
47+
}
48+
49+
@JsonDeserialize(builder = Child.Builder.class)
50+
@JacksonXmlRootElement(localName = "child")
51+
static class Child {
52+
private final String id;
53+
54+
public Child(String id) {
55+
this.id = id;
56+
}
57+
58+
@JsonProperty("id")
59+
public String getId() {
60+
return id;
61+
}
62+
63+
static class Builder {
64+
private String id;
65+
66+
@JsonProperty("id")
67+
public Builder id(String id) {
68+
this.id = id;
69+
return this;
70+
}
71+
72+
public Child build() {
73+
return new Child(id);
74+
}
75+
}
76+
}
77+
78+
// -- Test Methods --//
79+
private final XmlMapper MAPPER = newMapper();
80+
81+
public void testIssue646() throws Exception {
82+
final String XML = "<parent><child><id>1</id></child></parent>";
83+
Parent parent = MAPPER.readValue(XML, Parent.class);
84+
assertNotNull(parent);
85+
assertNotNull(parent.getChildren());
86+
assertEquals(1, parent.getChildren().size());
87+
}
88+
}

0 commit comments

Comments
 (0)