DATAREST-705 - DomainObjectReader now doesn't wipe identifier and version properties.

Identifier and version properties are handled in a special way by Spring Data REST (ids are derived from and exposed via the URI, versions make it into the ETag header) and are not contained in the response representation. That requires handling PUT request having to explicitly exclude them from being wiped to null so that the server side values are still preserved.
This commit is contained in:
Oliver Gierke
2015-11-20 16:53:18 +01:00
parent 6e3835d956
commit 6068a1788d
2 changed files with 38 additions and 0 deletions

View File

@@ -121,6 +121,10 @@ public class DomainObjectReader {
@Override
public void doWithPersistentProperty(PersistentProperty<?> property) {
if (property.isIdProperty() || property.isVersionProperty()) {
return;
}
String mappedName = properties.getMappedName(property);
boolean isMappedProperty = mappedName != null;

View File

@@ -27,6 +27,8 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Version;
import org.springframework.data.mapping.context.PersistentEntities;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.rest.core.mapping.ResourceMappings;
@@ -58,6 +60,7 @@ public class DomainObjectReaderUnitTests {
mappingContext.getPersistentEntity(SampleUser.class);
mappingContext.getPersistentEntity(Person.class);
mappingContext.getPersistentEntity(TypeWithGenericMap.class);
mappingContext.getPersistentEntity(VersionedType.class);
mappingContext.afterPropertiesSet();
PersistentEntities entities = new PersistentEntities(Collections.singleton(mappingContext));
@@ -144,6 +147,28 @@ public class DomainObjectReaderUnitTests {
reader.readPut(node, "", mapper);
}
/**
* @see DATAREST-705
*/
@Test
public void doesNotWipeIdAndVersionPropertyForPut() throws Exception {
VersionedType type = new VersionedType();
type.id = 1L;
type.version = 1L;
type.firstname = "Dave";
ObjectMapper mapper = new ObjectMapper();
ObjectNode node = (ObjectNode) mapper.readTree("{ \"lastname\" : \"Matthews\" }");
VersionedType result = reader.readPut(node, type, mapper);
assertThat(result.lastname, is("Matthews"));
assertThat(result.firstname, is(nullValue()));
assertThat(result.id, is(1L));
assertThat(result.version, is(1L));
}
@JsonAutoDetect(fieldVisibility = Visibility.ANY)
static class SampleUser {
@@ -176,4 +201,13 @@ public class DomainObjectReaderUnitTests {
Map<String, Object> map;
}
@JsonAutoDetect(fieldVisibility = Visibility.ANY)
static class VersionedType {
@Id Long id;
@Version Long version;
String firstname, lastname;
}
}