DATAREST-931 - DomainObjectMerger now handles arrays with complex objects correctly.

We now explicitly manually merge array nodes that contain complex objects. Previously arrays would've been skipped and the subsequent Jackson update would wipe out all properties not contained in the original document even on PATCH requests.
This commit is contained in:
Oliver Gierke
2016-10-31 13:40:37 +01:00
parent 40bb8e8e6c
commit 50d2678d01
2 changed files with 122 additions and 13 deletions

View File

@@ -19,9 +19,14 @@ import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Before;
@@ -46,6 +51,7 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.base.Charsets;
/**
* Unit tests for {@link DomainObjectReader}.
@@ -68,6 +74,7 @@ public class DomainObjectReaderUnitTests {
mappingContext.getPersistentEntity(TypeWithGenericMap.class);
mappingContext.getPersistentEntity(VersionedType.class);
mappingContext.getPersistentEntity(SampleWithCreatedDate.class);
mappingContext.getPersistentEntity(User.class);
mappingContext.afterPropertiesSet();
PersistentEntities entities = new PersistentEntities(Collections.singleton(mappingContext));
@@ -193,6 +200,23 @@ public class DomainObjectReaderUnitTests {
assertThat(reader.readPut(node, sample, mapper).createdDate, is(reference));
}
@Test
public void readsPatchForEntityNestedInCollection() throws Exception {
Phone phone = new Phone();
phone.creationDate = new GregorianCalendar();
User user = new User();
user.phones.add(phone);
ByteArrayInputStream source = new ByteArrayInputStream(
"{ \"phones\" : [ { \"label\" : \"some label\" } ] }".getBytes(Charsets.UTF_8));
User result = reader.read(source, user, new ObjectMapper());
assertThat(result.phones.get(0).creationDate, is(notNullValue()));
}
@JsonAutoDetect(fieldVisibility = Visibility.ANY)
static class SampleUser {
@@ -242,4 +266,15 @@ public class DomainObjectReaderUnitTests {
@ReadOnlyProperty //
Date createdDate;
}
static class User {
public List<Phone> phones = new ArrayList<Phone>();
}
static class Phone {
public Calendar creationDate;
public String label;
}
}