Skip to content

Commit

Permalink
DATAREST-705 - DomainObjectReader now doesn't wipe identifier and ver…
Browse files Browse the repository at this point in the history
…sion 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.
  • Loading branch information
odrotbohm committed Nov 20, 2015
1 parent 515bee7 commit 2d1fe6f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
Expand Up @@ -121,6 +121,10 @@ public <T> T readPut(final ObjectNode source, T target, final ObjectMapper mappe
@Override
public void doWithPersistentProperty(PersistentProperty<?> property) {

if (property.isIdProperty() || property.isVersionProperty()) {
return;
}

String mappedName = properties.getMappedName(property);

boolean isMappedProperty = mappedName != null;
Expand Down
Expand Up @@ -27,6 +27,8 @@
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;
Expand Down Expand Up @@ -58,6 +60,7 @@ public void setUp() {
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));
Expand Down Expand Up @@ -144,6 +147,28 @@ public void rejectsMergingUnknownDomainObject() throws Exception {
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 {

Expand Down Expand Up @@ -176,4 +201,13 @@ static class TypeWithGenericMap {

Map<String, Object> map;
}

@JsonAutoDetect(fieldVisibility = Visibility.ANY)
static class VersionedType {

@Id Long id;
@Version Long version;

String firstname, lastname;
}
}

0 comments on commit 2d1fe6f

Please sign in to comment.