Skip to content

Commit b61cb9a

Browse files
committed
DATAREST-1249 - DomainObjectMerger now properly binds to uninitialized target collections.
1 parent a145405 commit b61cb9a

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/DomainObjectReader.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,9 @@ private Optional<Collection<Object>> mergeCollections(PersistentProperty<?> prop
480480
@SuppressWarnings("unchecked")
481481
private static Collection<Object> asCollection(Object source) {
482482

483-
if (source instanceof Collection) {
483+
if (source == null) {
484+
return null;
485+
} else if (source instanceof Collection) {
484486
return (Collection<Object>) source;
485487
} else if (source.getClass().isArray()) {
486488
return Arrays.asList(ObjectUtils.toObjectArray(source));

spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/DomainObjectReaderUnitTests.java

+19
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ public void setUp() {
112112
mappingContext.getPersistentEntity(CollectionOfEnumWithMethods.class);
113113
mappingContext.getPersistentEntity(SampleWithReference.class);
114114
mappingContext.getPersistentEntity(Note.class);
115+
mappingContext.getPersistentEntity(WithNullCollection.class);
115116
mappingContext.afterPropertiesSet();
116117

117118
this.entities = new PersistentEntities(Collections.singleton(mappingContext));
@@ -555,6 +556,17 @@ public void patchWithReferenceToRelatedEntityIsResolvedCorrectly() throws Except
555556
assertThat(result.tags).contains(second);
556557
}
557558

559+
@Test // DATAREST-1249
560+
public void mergesIntoUninitializedCollection() throws Exception {
561+
562+
ObjectMapper mapper = new ObjectMapper();
563+
ObjectNode source = (ObjectNode) mapper.readTree("{ \"strings\" : [ \"value\" ] }");
564+
565+
WithNullCollection result = reader.readPut(source, new WithNullCollection(), mapper);
566+
567+
assertThat(result.strings).containsExactly("value");
568+
}
569+
558570
@SuppressWarnings("unchecked")
559571
private static <T> T as(Object source, Class<T> type) {
560572

@@ -766,4 +778,11 @@ public T deserialize(JsonParser p, DeserializationContext ctxt) throws IOExcepti
766778
.findFirst().orElse(null);
767779
}
768780
}
781+
782+
// DATAREST-1249
783+
784+
@JsonAutoDetect(fieldVisibility = Visibility.ANY)
785+
static class WithNullCollection {
786+
List<String> strings;
787+
}
769788
}

0 commit comments

Comments
 (0)