diff --git a/release-notes/CREDITS-2.x b/release-notes/CREDITS-2.x index 6a3a95861..96e8ad722 100644 --- a/release-notes/CREDITS-2.x +++ b/release-notes/CREDITS-2.x @@ -252,6 +252,12 @@ Arthur Chan (@arthurscchan) Alex H (@ahcodedthat) -* Contribtued #643: XML serialization of floating-point infinity is incompatible +* Contributed #643: XML serialization of floating-point infinity is incompatible with JAXB and XML Schema (2.17.0) + +Bas Passon (@bpasson) + +* Reported, contributed fix for #646: Deserializing fails when using builder classes + with `Iterable` Collection setters + (2.17.1) diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 6cb43bddf..bacd95994 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -8,6 +8,11 @@ Project: jackson-dataformat-xml No changes since 2.17 +2.17.1 (not yet released) + +#646: Deserializing fails when using builder classes with `Iterable` Collection setters + (fix contributed by Bas P) + 2.17.0 (12-Mar-2024) #324: Support use of `xsi:type` for polymorphic serialization diff --git a/src/main/java/com/fasterxml/jackson/dataformat/xml/util/TypeUtil.java b/src/main/java/com/fasterxml/jackson/dataformat/xml/util/TypeUtil.java index fdfc8d242..722c4709a 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/xml/util/TypeUtil.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/xml/util/TypeUtil.java @@ -11,7 +11,8 @@ public class TypeUtil public static boolean isIndexedType(JavaType type) { Class cls = type.getRawClass(); - if (type.isContainerType() || type.isIterationType()) { + // 25-Mar-2024, tatu [dataformat-xml#646]: Need to support Iterable too + if (type.isContainerType() || type.isIterationType() || cls == Iterable.class) { // One special case; byte[] will be serialized as base64-encoded String, not real array, so: // (actually, ditto for char[]; thought to be a String) if (cls == byte[].class || cls == char[].class) { diff --git a/src/test/java/com/fasterxml/jackson/dataformat/xml/lists/IterableCollectionBuilder646Test.java b/src/test/java/com/fasterxml/jackson/dataformat/xml/lists/IterableCollectionBuilder646Test.java new file mode 100644 index 000000000..55ba423a9 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/dataformat/xml/lists/IterableCollectionBuilder646Test.java @@ -0,0 +1,88 @@ +package com.fasterxml.jackson.dataformat.xml.lists; + +import java.util.ArrayList; +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.dataformat.xml.XmlMapper; +import com.fasterxml.jackson.dataformat.xml.XmlTestBase; +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement; + +// [dataformat-xml#646] +public class IterableCollectionBuilder646Test extends XmlTestBase +{ + @JsonDeserialize(builder = Parent.Builder.class) + @JacksonXmlRootElement(localName = "parent") + static class Parent { + private final List children; + + Parent(List children) { + this.children = children; + } + + @JsonProperty("child") + @JacksonXmlElementWrapper(useWrapping = false) + public List getChildren() { + return children; + } + + static class Builder { + private final List children = new ArrayList<>(); + + @JsonProperty("child") + @JacksonXmlElementWrapper(useWrapping = false) + public Builder children(Iterable children) { + for (Child c : children) { + this.children.add(c); + } + return this; + } + + public Parent build() { + return new Parent(children); + } + } + } + + @JsonDeserialize(builder = Child.Builder.class) + @JacksonXmlRootElement(localName = "child") + static class Child { + private final String id; + + public Child(String id) { + this.id = id; + } + + @JsonProperty("id") + public String getId() { + return id; + } + + static class Builder { + private String id; + + @JsonProperty("id") + public Builder id(String id) { + this.id = id; + return this; + } + + public Child build() { + return new Child(id); + } + } + } + + // -- Test Methods --// + private final XmlMapper MAPPER = newMapper(); + + public void testIssue646() throws Exception { + final String XML = "1"; + Parent parent = MAPPER.readValue(XML, Parent.class); + assertNotNull(parent); + assertNotNull(parent.getChildren()); + assertEquals(1, parent.getChildren().size()); + } +}