DATAREST-986 - DomainObjectReader now merges complex nested maps correctly.

We now use a Map property's generic type information to make sure we convert both the key and the value into the declared types. Previously we just used Object if the source value to map was null. Object is still used as fallback for raw maps though.
This commit is contained in:
Oliver Gierke
2017-01-23 09:24:14 +01:00
parent 2b9c7847b4
commit 96ff0cb465
2 changed files with 65 additions and 11 deletions

View File

@@ -20,6 +20,7 @@ import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import java.io.ByteArrayInputStream;
@@ -33,6 +34,7 @@ import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.junit.Before;
@@ -88,6 +90,7 @@ public class DomainObjectReaderUnitTests {
mappingContext.getPersistentEntity(Inner.class);
mappingContext.getPersistentEntity(Outer.class);
mappingContext.getPersistentEntity(Parent.class);
mappingContext.getPersistentEntity(Product.class);
mappingContext.afterPropertiesSet();
PersistentEntities entities = new PersistentEntities(Collections.singleton(mappingContext));
@@ -396,6 +399,19 @@ public class DomainObjectReaderUnitTests {
assertThat(mapper.treeToValue(node, Object.class), is((Object) "asd"));
}
@Test // DATAREST-986
public void readsComplexMap() throws Exception {
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(
"{ \"map\" : { \"en\" : { \"value\" : \"eventual\" }, \"de\" : { \"value\" : \"schlussendlich\" } } }");
Product result = reader.readPut((ObjectNode) node, new Product(), mapper);
assertThat(result.map.get(Locale.ENGLISH), is(new LocalizedValue("eventual")));
assertThat(result.map.get(Locale.GERMAN), is(new LocalizedValue("schlussendlich")));
}
@SuppressWarnings("unchecked")
private static <T> T as(Object source, Class<T> type) {
@@ -501,4 +517,17 @@ public class DomainObjectReaderUnitTests {
static class Item {
String some;
}
@JsonAutoDetect(fieldVisibility = Visibility.ANY)
static class Product {
Map<Locale, LocalizedValue> map = new HashMap<Locale, LocalizedValue>();
}
@JsonAutoDetect(fieldVisibility = Visibility.ANY)
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
static class LocalizedValue {
String value;
}
}