Skip to content

Commit

Permalink
Fix regression in PUT handling for empty nested documents.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
odrotbohm committed Jun 12, 2023
1 parent 601a793 commit 8b63502
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import org.springframework.data.rest.webmvc.mapping.Associations;
import org.springframework.data.rest.webmvc.util.InputStreamHttpInputMessage;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.Optionals;
import org.springframework.data.util.TypeInformation;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.lang.Nullable;
Expand Down Expand Up @@ -685,7 +684,10 @@ public void doWithPersistentProperty(PersistentProperty<?> property) {
} else if (property.isCollectionLike()) {
result = mergeCollections(property, sourceValue, targetValue, mapper);
} else if (property.isEntity()) {
result = Optionals.mapIfAllPresent(sourceValue, targetValue, (l, r) -> mergeForPut(l, r, mapper));

result = targetValue.isEmpty()
? sourceValue
: targetValue.flatMap(t -> sourceValue.map(s -> mergeForPut(s, t, mapper)));
} else {
result = sourceValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,26 @@ void deserializesCustomCollectionOfPrimitives() throws Exception {
assertThat(result.longs).isEqualTo(Arrays.asList(1L, 2L));
}

@Test // GH-2264
void nestedEntitiesAreCreatedWhenMissingForPut() throws Exception {

var outer = new Outer();
outer.name = "outer name";
outer.prop = "something";

var node = new ObjectMapper().readTree(
"{ \"inner\" : { \"name\" : \"new inner name\", \"readOnly\" : \"readonly value\", \"hidden\" : \"hidden value\" } }");

var result = reader.readPut((ObjectNode) node, outer, new ObjectMapper());

assertThat(result).isSameAs(outer);
assertThat(result.inner).isNotNull();
assertThat(result.inner.prop).isNull();
assertThat(result.inner.name).isEqualTo("new inner name");
assertThat(result.inner.readOnly).isNull();
assertThat(result.inner.hidden).isNull();
}

@SuppressWarnings("unchecked")
private static <T> T as(Object source, Class<T> type) {

Expand Down

0 comments on commit 8b63502

Please sign in to comment.