From 778fdf48f00dd5b03c2a1cc47c8f41dd1160481e Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Wed, 30 May 2018 10:24:41 +0200 Subject: [PATCH] DATAREST-1249 - DomainObjectMerger now properly binds to uninitialized target collections. --- .../rest/webmvc/json/DomainObjectReader.java | 4 +++- .../json/DomainObjectReaderUnitTests.java | 19 +++++++++++++++++++ 2 files changed, 22 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 1070e8aaa..c2a9e6f0d 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 @@ -480,7 +480,9 @@ public class DomainObjectReader { @SuppressWarnings("unchecked") private static Collection asCollection(Object source) { - if (source instanceof Collection) { + if (source == null) { + return null; + } else if (source instanceof Collection) { return (Collection) source; } else if (source.getClass().isArray()) { return Arrays.asList(ObjectUtils.toObjectArray(source)); 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 623ed4c94..3b6427800 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 @@ -112,6 +112,7 @@ public class DomainObjectReaderUnitTests { mappingContext.getPersistentEntity(CollectionOfEnumWithMethods.class); mappingContext.getPersistentEntity(SampleWithReference.class); mappingContext.getPersistentEntity(Note.class); + mappingContext.getPersistentEntity(WithNullCollection.class); mappingContext.afterPropertiesSet(); this.entities = new PersistentEntities(Collections.singleton(mappingContext)); @@ -555,6 +556,17 @@ public class DomainObjectReaderUnitTests { assertThat(result.tags).contains(second); } + @Test // DATAREST-1249 + public void mergesIntoUninitializedCollection() throws Exception { + + ObjectMapper mapper = new ObjectMapper(); + ObjectNode source = (ObjectNode) mapper.readTree("{ \"strings\" : [ \"value\" ] }"); + + WithNullCollection result = reader.readPut(source, new WithNullCollection(), mapper); + + assertThat(result.strings).containsExactly("value"); + } + @SuppressWarnings("unchecked") private static T as(Object source, Class type) { @@ -766,4 +778,11 @@ public class DomainObjectReaderUnitTests { .findFirst().orElse(null); } } + + // DATAREST-1249 + + @JsonAutoDetect(fieldVisibility = Visibility.ANY) + static class WithNullCollection { + List strings; + } }