From b859b8b91fd170f9970d0d40741aeb1e07d786fa Mon Sep 17 00:00:00 2001 From: Craig Andrews Date: Fri, 11 Nov 2016 16:36:14 -0500 Subject: [PATCH] DATAREST-938 - Update nested entities instead of replacing them with new instances. Original pull request: #241. --- .../rest/webmvc/json/DomainObjectReader.java | 1 + .../json/DomainObjectReaderUnitTests.java | 42 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/DomainObjectReader.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/DomainObjectReader.java index 3ca3b07ff..14fd78aae 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/DomainObjectReader.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/DomainObjectReader.java @@ -209,6 +209,7 @@ public class DomainObjectReader { } if (rawValue != null && property.isEntity()) { + i.remove(); doMerge(objectNode, rawValue, mapper); } } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/DomainObjectReaderUnitTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/DomainObjectReaderUnitTests.java index 62403727d..8304d3b58 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/DomainObjectReaderUnitTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/DomainObjectReaderUnitTests.java @@ -79,6 +79,8 @@ public class DomainObjectReaderUnitTests { mappingContext.getPersistentEntity(SampleWithCreatedDate.class); mappingContext.getPersistentEntity(SampleWithTransient.class); mappingContext.getPersistentEntity(User.class); + mappingContext.getPersistentEntity(Inner.class); + mappingContext.getPersistentEntity(Outer.class); mappingContext.afterPropertiesSet(); PersistentEntities entities = new PersistentEntities(Collections.singleton(mappingContext)); @@ -272,6 +274,31 @@ public class DomainObjectReaderUnitTests { assertThat(sub4.get("c2"), is("new")); } + /** + * @see DATAREST-938 + */ + @Test + public void nestedEntitiesAreUpdated() throws Exception { + + Inner inner = new Inner(); + inner.name = "inner name"; + inner.prop = "something"; + Outer outer = new Outer(); + outer.prop = "else"; + outer.name = "outer name"; + outer.inner = inner; + + JsonNode node = new ObjectMapper().readTree("{ \"inner\" : { \"name\" : \"new inner name\" } }"); + + Outer result = reader.merge((ObjectNode) node, outer, new ObjectMapper()); + + assertThat(result, is(sameInstance(outer))); + assertThat(result.prop, is("else")); + assertThat(result.inner.prop, is("something")); + assertThat(result.inner.name, is("new inner name")); + assertThat(result.inner, is(sameInstance(inner))); + } + @SuppressWarnings("unchecked") private static T as(Object source, Class type) { @@ -364,4 +391,19 @@ public class DomainObjectReaderUnitTests { String name; @Transient String temporary; } + + @JsonAutoDetect(fieldVisibility = Visibility.ANY) + static class Outer { + + String name; + String prop; + Inner inner; + } + + @JsonAutoDetect(fieldVisibility = Visibility.ANY) + static class Inner { + + String name; + String prop; + } }