Skip to content

Commit 36300cf

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 52f3f55 commit 36300cf

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;
@@ -677,7 +676,10 @@ public void doWithPersistentProperty(PersistentProperty<?> property) {
677676
} else if (property.isCollectionLike()) {
678677
result = mergeCollections(property, sourceValue, targetValue, mapper);
679678
} else if (property.isEntity()) {
680-
result = Optionals.mapIfAllPresent(sourceValue, targetValue, (l, r) -> mergeForPut(l, r, mapper));
679+
680+
result = targetValue.isEmpty()
681+
? sourceValue
682+
: targetValue.flatMap(t -> sourceValue.map(s -> mergeForPut(s, t, mapper)));
681683
} else {
682684
result = sourceValue;
683685
}

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
@@ -664,6 +664,26 @@ void deserializesCustomCollectionOfPrimitives() throws Exception {
664664
assertThat(result.longs).isEqualTo(List.of(1L, 2L));
665665
}
666666

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

0 commit comments

Comments
 (0)