diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/DomainObjectReader.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/DomainObjectReader.java index 5df73fbdd..9511ebf6b 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/DomainObjectReader.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/DomainObjectReader.java @@ -390,7 +390,7 @@ public class DomainObjectReader { ? rawValues.apply(current) : mapper.treeToValue(jsonNode, componentType.getType())); - break; + continue; } Object next = value.next(); diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/DomainObjectReaderUnitTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/DomainObjectReaderUnitTests.java index e3b8e1c26..0d5ecaca4 100755 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/DomainObjectReaderUnitTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/DomainObjectReaderUnitTests.java @@ -776,6 +776,40 @@ class DomainObjectReaderUnitTests { assertThat(result.map.get("array")).isEqualTo(new String[] { "second", "update" }); } + @Test // GH-2357 + void addsElementToPreviouslyEmptyCollectionForPatch() throws Exception { + + Child child = new Child(); + child.items = new ArrayList<>(); + + JsonNode node = new ObjectMapper() + .readTree("{ \"items\" : [ { \"some\" : \"value\" }, { \"some\" : \"otherValue\" } ] }"); + + Child result = reader.doMerge((ObjectNode) node, child, new ObjectMapper()); + + assertThat(result.items).hasSize(2); + assertThat(result.items.get(0).some).isEqualTo("value"); + assertThat(result.items.get(1).some).isEqualTo("otherValue"); + } + + @Test // GH-2357 + void augmentsCollectionForPatch() throws Exception { + + Child child = new Child(); + child.items = new ArrayList<>(Arrays.asList(new Item("old"))); + + JsonNode node = new ObjectMapper() + .readTree( + "{ \"items\" : [ { \"some\" : \"value\" }, { \"some\" : \"otherValue\" }, { \"some\" : \"yetAnotherValue\" } ] }"); + + Child result = reader.doMerge((ObjectNode) node, child, new ObjectMapper()); + + assertThat(result.items).hasSize(3); + assertThat(result.items.get(0).some).isEqualTo("value"); + assertThat(result.items.get(1).some).isEqualTo("otherValue"); + assertThat(result.items.get(2).some).isEqualTo("yetAnotherValue"); + } + @SuppressWarnings("unchecked") private static T as(Object source, Class type) {