Skip to content

Commit 8b63502

Browse files
committed
Fix regression in PUT handling for empty nested documents.
The fix for #2174 introduced a bug for our PUT handling of nested documents in case the target object's field value is null as it would only apply the nested value if all Optionals were present. This is, of course not the case. Fixes #2264.
1 parent 601a793 commit 8b63502

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import org.springframework.data.rest.webmvc.mapping.Associations;
4141
import org.springframework.data.rest.webmvc.util.InputStreamHttpInputMessage;
4242
import org.springframework.data.util.ClassTypeInformation;
43-
import org.springframework.data.util.Optionals;
4443
import org.springframework.data.util.TypeInformation;
4544
import org.springframework.http.converter.HttpMessageNotReadableException;
4645
import org.springframework.lang.Nullable;
@@ -685,7 +684,10 @@ public void doWithPersistentProperty(PersistentProperty<?> property) {
685684
} else if (property.isCollectionLike()) {
686685
result = mergeCollections(property, sourceValue, targetValue, mapper);
687686
} else if (property.isEntity()) {
688-
result = Optionals.mapIfAllPresent(sourceValue, targetValue, (l, r) -> mergeForPut(l, r, mapper));
687+
688+
result = targetValue.isEmpty()
689+
? sourceValue
690+
: targetValue.flatMap(t -> sourceValue.map(s -> mergeForPut(s, t, mapper)));
689691
} else {
690692
result = sourceValue;
691693
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,26 @@ void deserializesCustomCollectionOfPrimitives() throws Exception {
662662
assertThat(result.longs).isEqualTo(Arrays.asList(1L, 2L));
663663
}
664664

665+
@Test // GH-2264
666+
void nestedEntitiesAreCreatedWhenMissingForPut() throws Exception {
667+
668+
var outer = new Outer();
669+
outer.name = "outer name";
670+
outer.prop = "something";
671+
672+
var node = new ObjectMapper().readTree(
673+
"{ \"inner\" : { \"name\" : \"new inner name\", \"readOnly\" : \"readonly value\", \"hidden\" : \"hidden value\" } }");
674+
675+
var result = reader.readPut((ObjectNode) node, outer, new ObjectMapper());
676+
677+
assertThat(result).isSameAs(outer);
678+
assertThat(result.inner).isNotNull();
679+
assertThat(result.inner.prop).isNull();
680+
assertThat(result.inner.name).isEqualTo("new inner name");
681+
assertThat(result.inner.readOnly).isNull();
682+
assertThat(result.inner.hidden).isNull();
683+
}
684+
665685
@SuppressWarnings("unchecked")
666686
private static <T> T as(Object source, Class<T> type) {
667687

0 commit comments

Comments
 (0)