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 94e9b585b..aa92569d6 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 @@ -108,7 +108,7 @@ public class DomainObjectReader { @Override public void doWithPersistentProperty(PersistentProperty property) { - if (property.isIdProperty() || property.isVersionProperty()) { + if (property.isIdProperty() || property.isVersionProperty() || !property.isWritable()) { return; } 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 5ec0b1280..5fb4600ab 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 @@ -20,6 +20,7 @@ import static org.junit.Assert.*; import static org.mockito.Mockito.*; import java.util.Collections; +import java.util.Date; import java.util.HashMap; import java.util.Map; @@ -28,7 +29,9 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; +import org.springframework.data.annotation.CreatedDate; import org.springframework.data.annotation.Id; +import org.springframework.data.annotation.ReadOnlyProperty; import org.springframework.data.annotation.Version; import org.springframework.data.keyvalue.core.mapping.context.KeyValueMappingContext; import org.springframework.data.mapping.context.PersistentEntities; @@ -64,6 +67,7 @@ public class DomainObjectReaderUnitTests { mappingContext.getPersistentEntity(Person.class); mappingContext.getPersistentEntity(TypeWithGenericMap.class); mappingContext.getPersistentEntity(VersionedType.class); + mappingContext.getPersistentEntity(SampleWithCreatedDate.class); mappingContext.afterPropertiesSet(); PersistentEntities entities = new PersistentEntities(Collections.singleton(mappingContext)); @@ -172,6 +176,23 @@ public class DomainObjectReaderUnitTests { assertThat(result.version, is(1L)); } + /** + * @see DATAREST-873 + */ + @Test + public void doesNotApplyInputToReadOnlyFields() throws Exception { + + ObjectMapper mapper = new ObjectMapper(); + ObjectNode node = (ObjectNode) mapper.readTree("{}"); + + Date reference = new Date(); + + SampleWithCreatedDate sample = new SampleWithCreatedDate(); + sample.createdDate = reference; + + assertThat(reader.readPut(node, sample, mapper).createdDate, is(reference)); + } + @JsonAutoDetect(fieldVisibility = Visibility.ANY) static class SampleUser { @@ -213,4 +234,12 @@ public class DomainObjectReaderUnitTests { String firstname, lastname; } + + @JsonAutoDetect(fieldVisibility = Visibility.ANY) + static class SampleWithCreatedDate { + + @CreatedDate // + @ReadOnlyProperty // + Date createdDate; + } }