Skip to content

Commit 5c76446

Browse files
committed
Properly append all superfluous elements to an array during merge patch.
Fixes GH-2357.
1 parent 41de5ad commit 5c76446

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/DomainObjectReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ private boolean handleArrayNode(ArrayNode array, Collection<Object> collection,
390390
? rawValues.apply(current)
391391
: mapper.treeToValue(jsonNode, componentType.getType()));
392392

393-
break;
393+
continue;
394394
}
395395

396396
Object next = value.next();

spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/DomainObjectReaderUnitTests.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,40 @@ void replacesNestedArrays() throws Exception {
771771
assertThat(result.map.get("array")).isEqualTo(new String[] { "second", "update" });
772772
}
773773

774+
@Test // GH-2357
775+
void addsElementToPreviouslyEmptyCollectionForPatch() throws Exception {
776+
777+
Child child = new Child();
778+
child.items = new ArrayList<>();
779+
780+
JsonNode node = new ObjectMapper()
781+
.readTree("{ \"items\" : [ { \"some\" : \"value\" }, { \"some\" : \"otherValue\" } ] }");
782+
783+
Child result = reader.doMerge((ObjectNode) node, child, new ObjectMapper());
784+
785+
assertThat(result.items).hasSize(2);
786+
assertThat(result.items.get(0).some).isEqualTo("value");
787+
assertThat(result.items.get(1).some).isEqualTo("otherValue");
788+
}
789+
790+
@Test // GH-2357
791+
void augmentsCollectionForPatch() throws Exception {
792+
793+
Child child = new Child();
794+
child.items = new ArrayList<>(Arrays.asList(new Item("old")));
795+
796+
JsonNode node = new ObjectMapper()
797+
.readTree(
798+
"{ \"items\" : [ { \"some\" : \"value\" }, { \"some\" : \"otherValue\" }, { \"some\" : \"yetAnotherValue\" } ] }");
799+
800+
Child result = reader.doMerge((ObjectNode) node, child, new ObjectMapper());
801+
802+
assertThat(result.items).hasSize(3);
803+
assertThat(result.items.get(0).some).isEqualTo("value");
804+
assertThat(result.items.get(1).some).isEqualTo("otherValue");
805+
assertThat(result.items.get(2).some).isEqualTo("yetAnotherValue");
806+
}
807+
774808
@SuppressWarnings("unchecked")
775809
private static <T> T as(Object source, Class<T> type) {
776810

0 commit comments

Comments
 (0)