From 2d1fe6fe878aa4ce4f6fb2862071a24808157caf Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Fri, 20 Nov 2015 16:53:18 +0100 Subject: [PATCH] DATAREST-705 - DomainObjectReader now doesn't wipe identifier and version properties. Identifier and version properties are handled in a special way by Spring Data REST (ids are derived from and exposed via the URI, versions make it into the ETag header) and are not contained in the response representation. That requires handling PUT request having to explicitly exclude them from being wiped to null so that the server side values are still preserved. --- .../rest/webmvc/json/DomainObjectReader.java | 4 +++ .../json/DomainObjectReaderUnitTests.java | 34 +++++++++++++++++++ 2 files changed, 38 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 1d57ea6f2..c423e26a3 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 @@ -121,6 +121,10 @@ public T readPut(final ObjectNode source, T target, final ObjectMapper mappe @Override public void doWithPersistentProperty(PersistentProperty property) { + if (property.isIdProperty() || property.isVersionProperty()) { + return; + } + String mappedName = properties.getMappedName(property); boolean isMappedProperty = mappedName != null; 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 8977c3fe9..08f6a32ce 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 @@ -27,6 +27,8 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; +import org.springframework.data.annotation.Id; +import org.springframework.data.annotation.Version; import org.springframework.data.mapping.context.PersistentEntities; import org.springframework.data.mongodb.core.mapping.MongoMappingContext; import org.springframework.data.rest.core.mapping.ResourceMappings; @@ -58,6 +60,7 @@ public void setUp() { mappingContext.getPersistentEntity(SampleUser.class); mappingContext.getPersistentEntity(Person.class); mappingContext.getPersistentEntity(TypeWithGenericMap.class); + mappingContext.getPersistentEntity(VersionedType.class); mappingContext.afterPropertiesSet(); PersistentEntities entities = new PersistentEntities(Collections.singleton(mappingContext)); @@ -144,6 +147,28 @@ public void rejectsMergingUnknownDomainObject() throws Exception { reader.readPut(node, "", mapper); } + /** + * @see DATAREST-705 + */ + @Test + public void doesNotWipeIdAndVersionPropertyForPut() throws Exception { + + VersionedType type = new VersionedType(); + type.id = 1L; + type.version = 1L; + type.firstname = "Dave"; + + ObjectMapper mapper = new ObjectMapper(); + ObjectNode node = (ObjectNode) mapper.readTree("{ \"lastname\" : \"Matthews\" }"); + + VersionedType result = reader.readPut(node, type, mapper); + + assertThat(result.lastname, is("Matthews")); + assertThat(result.firstname, is(nullValue())); + assertThat(result.id, is(1L)); + assertThat(result.version, is(1L)); + } + @JsonAutoDetect(fieldVisibility = Visibility.ANY) static class SampleUser { @@ -176,4 +201,13 @@ static class TypeWithGenericMap { Map map; } + + @JsonAutoDetect(fieldVisibility = Visibility.ANY) + static class VersionedType { + + @Id Long id; + @Version Long version; + + String firstname, lastname; + } }