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 d1432db4f..c10bfcc9e 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 @@ -60,6 +60,7 @@ * @author Craig Andrews * @author Mathias Düsterhöft * @author Thomas Mrozinski + * @author Bas Schoenmaeckers * @since 2.2 */ public class DomainObjectReader { @@ -235,8 +236,12 @@ T doMerge(ObjectNode root, T target, ObjectMapper mapper) throws Exception { String fieldName = entry.getKey(); if (!mappedProperties.isWritableProperty(fieldName)) { + PropertyAccessor targetPropertyAccessor = PropertyAccessorFactory.forBeanPropertyAccess(target); + + if (!targetPropertyAccessor.isWritableProperty(fieldName)) { + i.remove(); + } - 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 1d59b25fa..94cfc4efb 100755 --- 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 @@ -74,6 +74,7 @@ * @author Mathias Düsterhöft * @author Ken Dombeck * @author Thomas Mrozinski + * @author Bas Schoenmaeckers */ @RunWith(MockitoJUnitRunner.class) public class DomainObjectReaderUnitTests { @@ -576,6 +577,22 @@ public void doesNotWipeReadOnlyPropertyForPatch() throws Exception { assertThat(result.email).isEqualTo("foo@bar.com"); } + @Test // DATAREST-1524 + public void useTransientSettersWithNonPersistentPropertiesForPatch() throws Exception { + SampleUser user = new SampleUser("name", "password"); + user.lastLogin = new Date(); + user.email = "foo@bar.com"; + user.nonPersistentField = false; + + ObjectMapper mapper = new ObjectMapper(); + ObjectNode source = (ObjectNode) mapper.readTree("{ \"online\" : true}"); + + @SuppressWarnings("deprecation") + SampleUser result = reader.merge(source, user, mapper); + + assertThat(result.isOnline()).isTrue(); + } + @Test // DATAREST-1068 public void arraysCanBeResizedDuringMerge() throws Exception { ObjectMapper mapper = new ObjectMapper(); @@ -606,6 +623,22 @@ static class SampleUser { @ReadOnlyProperty // private String email; + @Transient + @JsonIgnore + boolean nonPersistentField; + + @Transient + @JsonProperty + public boolean isOnline() { + return nonPersistentField; + } + + @Transient + @JsonProperty + public void setOnline(Boolean online) { + this.nonPersistentField = online; + } + public SampleUser(String name, String password) { this.name = name;