From 4207be424350d151fa5a30e8c29a0ea20dd093d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathias=20Du=CC=88sterho=CC=88ft?= Date: Wed, 4 Oct 2017 10:55:38 +0200 Subject: [PATCH] DATAREST-1006 - MappedProperties now skips all ignored properties. Original pull request: #258. --- .../rest/webmvc/json/MappedProperties.java | 5 +++++ .../json/DomainObjectReaderUnitTests.java | 21 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/MappedProperties.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/MappedProperties.java index a323e5b0b..596bfe05e 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/MappedProperties.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/MappedProperties.java @@ -42,6 +42,7 @@ import com.fasterxml.jackson.databind.introspect.ClassIntrospector; * * @author Oliver Gierke * @author Mark Paluch + * @author Mathias Düsterhöft */ @RequiredArgsConstructor(access = AccessLevel.PRIVATE) class MappedProperties { @@ -69,6 +70,10 @@ class MappedProperties { for (BeanPropertyDefinition property : description.findProperties()) { + if (description.getIgnoredPropertyNames().contains(property.getName())) { + continue; + } + Optional> persistentProperty = // Optional.ofNullable(entity.getPersistentProperty(property.getInternalName())); 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 5ffc7ed72..623ed4c94 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 @@ -15,6 +15,7 @@ */ package org.springframework.data.rest.webmvc.json; +import static com.fasterxml.jackson.annotation.JsonProperty.Access.READ_ONLY; import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.Matchers.*; import static org.junit.Assert.assertThat; @@ -64,6 +65,7 @@ import org.springframework.data.rest.webmvc.mapping.Associations; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationContext; @@ -206,6 +208,22 @@ public class DomainObjectReaderUnitTests { assertThat(result.version).isEqualTo(1L); } + @Test //DATAREST-1006 + public void doesNotWipeReadOnlyJsonPropertyForPut() throws Exception { + + SampleUser sampleUser = new SampleUser("name", "password"); + sampleUser.lastLogin = new Date(); + + ObjectMapper mapper = new ObjectMapper(); + ObjectNode node = (ObjectNode) mapper.readTree("{ \"name\" : \"another\" }"); + + SampleUser result = reader.readPut(node, sampleUser, mapper); + + assertThat(result.name, is("another")); + assertThat(result.password, notNullValue()); + assertThat(result.lastLogin, notNullValue()); + } + @Test // DATAREST-873 public void doesNotApplyInputToReadOnlyFields() throws Exception { @@ -551,6 +569,9 @@ public class DomainObjectReaderUnitTests { @JsonIgnore String password; Map relatedUsers; + @JsonProperty(access = READ_ONLY) + private Date lastLogin; + public SampleUser(String name, String password) { this.name = name;