From 0b1bbbab3102195ea59555eb311fbbe9bd622ed9 Mon Sep 17 00:00:00 2001 From: Milos Cubrilo Date: Mon, 12 Dec 2016 18:46:28 +0100 Subject: [PATCH] DATAREST-959 - Fix adding elements to empty array in DomainObjectReader. Original pull request: #246. --- .../rest/webmvc/json/DomainObjectReader.java | 2 +- .../json/DomainObjectReaderUnitTests.java | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) 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 ce86de0d0..7858327e3 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 @@ -348,7 +348,7 @@ public class DomainObjectReader { private static Collection asCollection(Object source) { if (source == null) { - return Collections.emptyList(); + return new ArrayList(); } if (source instanceof Collection) { 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 1aab3ca3c..f1099e226 100644 --- 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 @@ -390,6 +390,24 @@ public class DomainObjectReaderUnitTests { assertThat(result.inner.items.get(0).some, is("value")); } + /** + * @see DATAREST-959 + */ + @Test + public void writesArrayOverUndefinedValueForPut() throws Exception { + + Parent source = new Parent(); + source.inner = new Child(); + source.inner.items = null; + + JsonNode node = new ObjectMapper().readTree("{ \"inner\" : { \"items\" : [ { \"some\" : \"value\" } ] } }"); + + Parent result = reader.readPut((ObjectNode) node, source, new ObjectMapper()); + + assertThat(result.inner.items.size(), is(1)); + assertThat(result.inner.items.get(0).some, is("value")); + } + @Test public void testname() throws Exception {