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 class DomainObjectReader { @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.Test; 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 class DomainObjectReaderUnitTests { 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 class DomainObjectReaderUnitTests { 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 @@ public class DomainObjectReaderUnitTests { Map map; } + + @JsonAutoDetect(fieldVisibility = Visibility.ANY) + static class VersionedType { + + @Id Long id; + @Version Long version; + + String firstname, lastname; + } }