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 70492a637..5df73fbdd 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 @@ -444,15 +444,8 @@ public class DomainObjectReader { TypeInformation typeToMap = getTypeToMap(sourceValue, valueType); if (value instanceof ObjectNode && sourceValue != null) { - doMerge((ObjectNode) value, sourceValue, mapper); - - } else if (value instanceof ArrayNode && sourceValue != null) { - - handleArray(value, sourceValue, mapper, getTypeToMap(sourceValue, typeToMap), null); - } else { - source.put(mappedKey, mapper.treeToValue(value, typeToMap.getType())); } 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 342626468..2d588bf20 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 @@ -109,6 +109,7 @@ class DomainObjectReaderUnitTests { mappingContext.getPersistentEntity(WithCustomMappedPrimitiveCollection.class); mappingContext.getPersistentEntity(BugModel.class); mappingContext.getPersistentEntity(ArrayListHolder.class); + mappingContext.getPersistentEntity(MapWrapper.class); mappingContext.afterPropertiesSet(); this.entities = new PersistentEntities(Collections.singleton(mappingContext)); @@ -736,6 +737,39 @@ class DomainObjectReaderUnitTests { assertThat(updated.array).containsExactly("ancient"); } + @Test // GH-2350 + void replacesArrays() throws Exception { + + ArrayHolder holder = new ArrayHolder(new String[] { "original" }); + + ObjectMapper mapper = new ObjectMapper(); + + JsonNode node = mapper.readTree("{ \"array\" : [ \"first\", \"update\" ] }"); + ArrayHolder result = reader.doMerge((ObjectNode) node, holder, mapper); + + node = mapper.readTree("{ \"array\" : [ \"second\", \"update\" ] }"); + result = reader.doMerge((ObjectNode) node, holder, mapper); + + assertThat(result.getArray()).isEqualTo(new String[] { "second", "update" }); + } + + @Test // GH-2350 + void replacesNestedArrays() throws Exception { + + MapWrapper wrapper = new MapWrapper(); + wrapper.map.put("array", new String[] { "original" }); + + ObjectMapper mapper = new ObjectMapper(); + + JsonNode node = mapper.readTree("{ \"map\" : { \"array\" : [ \"first\", \"update\" ] } }"); + MapWrapper result = reader.doMerge((ObjectNode) node, wrapper, mapper); + + node = mapper.readTree("{ \"map\" : { \"array\" : [ \"second\", \"update\" ] } }"); + result = reader.doMerge((ObjectNode) node, wrapper, mapper); + + assertThat(result.map.get("array")).isEqualTo(new String[] { "second", "update" }); + } + @SuppressWarnings("unchecked") private static T as(Object source, Class type) { @@ -1128,4 +1162,8 @@ class DomainObjectReaderUnitTests { this.values = values; } } + + static class MapWrapper { + public Map map = new HashMap<>(); + } }