DATAREST-1249 - DomainObjectMerger now properly binds to uninitialized target collections.

This commit is contained in:
Oliver Gierke
2018-05-30 10:24:41 +02:00
parent a5b3e554a0
commit 778fdf48f0
2 changed files with 22 additions and 1 deletions

View File

@@ -480,7 +480,9 @@ public class DomainObjectReader {
@SuppressWarnings("unchecked")
private static Collection<Object> asCollection(Object source) {
if (source instanceof Collection) {
if (source == null) {
return null;
} else if (source instanceof Collection) {
return (Collection<Object>) source;
} else if (source.getClass().isArray()) {
return Arrays.asList(ObjectUtils.toObjectArray(source));

View File

@@ -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> T as(Object source, Class<T> type) {
@@ -766,4 +778,11 @@ public class DomainObjectReaderUnitTests {
.findFirst().orElse(null);
}
}
// DATAREST-1249
@JsonAutoDetect(fieldVisibility = Visibility.ANY)
static class WithNullCollection {
List<String> strings;
}
}