Skip to content

Commit 5c0cfd3

Browse files
committed
Fix #2732
1 parent 56af702 commit 5c0cfd3

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

release-notes/VERSION-2.x

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Project: jackson-databind
2626
#2719: `FAIL_ON_IGNORED_PROPERTIES` does not throw on `READONLY` properties with
2727
an explicit name
2828
(reported, fix contributed by David B)
29+
#2732: Allow `JsonNode` auto-convert into `ArrayNode` if duplicates found (for XML)
2930
- Add `BeanDeserializerBase.isCaseInsensitive()`
3031
- Some refactoring of `CollectionDeserializer` to solve CSV array handling issues
3132

src/main/java/com/fasterxml/jackson/databind/deser/std/JsonNodeDeserializer.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,20 @@ protected void _handleDuplicateField(JsonParser p, DeserializationContext ctxt,
230230
"Duplicate field '%s' for `ObjectNode`: not allowed when `DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY` enabled",
231231
fieldName);
232232
}
233+
// [databind#2732]: Special case for XML; automatically coerce into `ArrayNode`
234+
if (ctxt.isEnabled(StreamReadCapability.DUPLICATE_PROPERTIES)) {
235+
// Note that ideally we wouldn't have to shuffle things but... Map.putIfAbsent()
236+
// only added in JDK 8, to efficiently check for add. So...
237+
if (oldValue.isArray()) { // already was array, to append
238+
((ArrayNode) oldValue).add(newValue);
239+
objectNode.replace(fieldName, oldValue);
240+
} else { // was not array, convert
241+
ArrayNode arr = nodeFactory.arrayNode();
242+
arr.add(oldValue);
243+
arr.add(newValue);
244+
objectNode.replace(fieldName, arr);
245+
}
246+
}
233247
}
234248

235249
/*

0 commit comments

Comments
 (0)