DATAREST-1383 - PATCH requests now skip application of values for backend read-only properties.

MappedProperties now immediately drops non-writable properties when created for deserialization. Previously those properties would have to be annotated with @JsonProperty(access = Access.READ_ONLY) explicitly to avoid them being considered.
This commit is contained in:
Oliver Drotbohm
2019-05-28 17:21:24 +02:00
parent 339c3d26d0
commit 28787116a6
4 changed files with 47 additions and 7 deletions

View File

@@ -569,6 +569,22 @@ public class DomainObjectReaderUnitTests {
assertThat(result.strings).containsExactly("value");
}
@Test // DATAREST-1383
public void doesNotWipeReadOnlyPropertyForPatch() throws Exception {
SampleUser user = new SampleUser("name", "password");
user.lastLogin = new Date();
user.email = "foo@bar.com";
ObjectMapper mapper = new ObjectMapper();
ObjectNode source = (ObjectNode) mapper.readTree("{ \"lastLogin\" : null, \"email\" : \"bar@foo.com\"}");
SampleUser result = reader.merge(source, user, mapper);
assertThat(result.lastLogin).isNotNull();
assertThat(result.email).isEqualTo("foo@bar.com");
}
@SuppressWarnings("unchecked")
private static <T> T as(Object source, Class<T> type) {
@@ -586,6 +602,9 @@ public class DomainObjectReaderUnitTests {
@JsonProperty(access = READ_ONLY) //
private Date lastLogin;
@ReadOnlyProperty //
private String email;
public SampleUser(String name, String password) {
this.name = name;

View File

@@ -18,6 +18,7 @@ package org.springframework.data.rest.webmvc.json;
import static org.assertj.core.api.Assertions.*;
import org.junit.Test;
import org.springframework.data.annotation.ReadOnlyProperty;
import org.springframework.data.annotation.Transient;
import org.springframework.data.keyvalue.core.mapping.context.KeyValueMappingContext;
import org.springframework.data.mapping.PersistentEntity;
@@ -84,6 +85,20 @@ public class MappedPropertiesUnitTests {
assertThat(properties.getPersistentProperty("readOnlyProperty")).isNotNull();
}
@Test // DATAREST-1383
public void doesNotRegardReadOnlyPropertyForDeserialization() {
MappedProperties properties = MappedProperties.forDeserialization(entity, mapper);
assertThat(properties.hasPersistentPropertyForField("anotherReadOnlyProperty")).isFalse();
assertThat(properties.getPersistentProperty("readOnlyProperty")).isNull();
properties = MappedProperties.forSerialization(entity, mapper);
assertThat(properties.hasPersistentPropertyForField("anotherReadOnlyProperty")).isTrue();
assertThat(properties.getPersistentProperty("readOnlyProperty")).isNotNull();
}
static class Sample {
public @Transient String notExposedBySpringData;
@@ -91,5 +106,6 @@ public class MappedPropertiesUnitTests {
public String exposedProperty;
public @JsonProperty("email") String emailAddress;
public @JsonProperty(access = Access.READ_ONLY) String readOnlyProperty;
public @ReadOnlyProperty String anotherReadOnlyProperty;
}
}