Skip to content

Commit cebd491

Browse files
committed
Properly append all superfluous elements to an array during merge patch.
Fixes GH-2357.
1 parent d9e5fa5 commit cebd491

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
@@ -770,6 +770,40 @@ void replacesNestedArrays() throws Exception {
770770
assertThat(result.map.get("array")).isEqualTo(new String[] { "second", "update" });
771771
}
772772

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

0 commit comments

Comments
 (0)