DATAREST-959 - Polishing.

Skip all merge logic if the source value is null. That frees all nested logic from handling with that case and us falling back to plain Jackson reading.

The array handling now also opts out if the source value is not a collection or array in the first place as it means we need to let Jackson override the value with the collection given to be deserialized.

Original pull request: #246.
This commit is contained in:
Oliver Gierke
2016-12-13 09:54:26 +01:00
parent 0b1bbbab31
commit c03653b7e9
2 changed files with 45 additions and 10 deletions

View File

@@ -26,10 +26,12 @@ import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -394,7 +396,7 @@ public class DomainObjectReaderUnitTests {
* @see DATAREST-959
*/
@Test
public void writesArrayOverUndefinedValueForPut() throws Exception {
public void addsElementToPreviouslyEmptyCollection() throws Exception {
Parent source = new Parent();
source.inner = new Child();
@@ -408,6 +410,31 @@ public class DomainObjectReaderUnitTests {
assertThat(result.inner.items.get(0).some, is("value"));
}
/**
* @see DATAREST-959
*/
@Test
@SuppressWarnings("unchecked")
public void turnsObjectIntoCollection() throws Exception {
Parent source = new Parent();
source.inner = new Child();
source.inner.object = new Item("value");
JsonNode node = new ObjectMapper()
.readTree("{ \"inner\" : { \"object\" : [ { \"some\" : \"value\" }, { \"some\" : \"otherValue\" } ] } }");
Parent result = reader.readPut((ObjectNode) node, source, new ObjectMapper());
assertThat(result.inner.object, is(instanceOf(Collection.class)));
Collection<?> collection = (Collection<?>) result.inner.object;
assertThat(collection.size(), is(2));
Iterator<Map<String, Object>> iterator = (Iterator<Map<String, Object>>) collection.iterator();
assertThat(iterator.next().get("some"), is((Object) "value"));
assertThat(iterator.next().get("some"), is((Object) "otherValue"));
}
@Test
public void testname() throws Exception {
@@ -515,6 +542,7 @@ public class DomainObjectReaderUnitTests {
@JsonAutoDetect(fieldVisibility = Visibility.ANY)
static class Child {
List<Item> items;
Object object;
}
@JsonAutoDetect(fieldVisibility = Visibility.ANY)