From 4150688851323e54967cacfbcd0d6a7e00c41980 Mon Sep 17 00:00:00 2001 From: Craig Andrews Date: Fri, 11 Nov 2016 14:41:52 -0500 Subject: [PATCH] DATAREST-937 - Transient properties in JSON are now included in merge. We now don't prematurely drop fields that don't have a persistent property exposed in DomainObjectReader. Doing so dropped values for transient fields as the latter are not exposed as persistent property in the first place. We still skip any nested merging though. Original pull request: #240. --- .../rest/webmvc/json/DomainObjectReader.java | 1 - .../json/DomainObjectReaderUnitTests.java | 26 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) 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 a2955f9e7..3ca3b07ff 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 @@ -165,7 +165,6 @@ public class DomainObjectReader { String fieldName = entry.getKey(); if (!mappedProperties.hasPersistentPropertyForField(fieldName)) { - i.remove(); continue; } 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 85fcc61cb..e4379dbd6 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 @@ -38,6 +38,7 @@ 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.Transient; import org.springframework.data.annotation.Version; import org.springframework.data.keyvalue.core.mapping.context.KeyValueMappingContext; import org.springframework.data.mapping.context.PersistentEntities; @@ -75,6 +76,7 @@ public class DomainObjectReaderUnitTests { mappingContext.getPersistentEntity(TypeWithGenericMap.class); mappingContext.getPersistentEntity(VersionedType.class); mappingContext.getPersistentEntity(SampleWithCreatedDate.class); + mappingContext.getPersistentEntity(SampleWithTransient.class); mappingContext.getPersistentEntity(User.class); mappingContext.afterPropertiesSet(); @@ -83,6 +85,23 @@ public class DomainObjectReaderUnitTests { this.reader = new DomainObjectReader(entities, new Associations(mappings, mock(RepositoryRestConfiguration.class))); } + /** + * @see DATAREST- + */ + @Test + public void considersTransientProperties() throws Exception { + + SampleWithTransient sample = new SampleWithTransient(); + sample.name="name"; + sample.temporary="temp"; + JsonNode node = new ObjectMapper().readTree("{\"name\": \"new name\", \"temporary\": \"new temp\"}"); + + SampleWithTransient result = reader.readPut((ObjectNode) node, sample, new ObjectMapper()); + + assertThat(result.name, is("new name")); + assertThat(result.temporary, is("new temp")); + } + /** * @see DATAREST-461 */ @@ -336,4 +355,11 @@ public class DomainObjectReaderUnitTests { public Calendar creationDate; public String label; } + + @JsonAutoDetect(fieldVisibility = Visibility.ANY) + static class SampleWithTransient { + + String name; + @Transient String temporary; + } }