DATAREST-701 - Fixed potential NullPointerException in DomainObjectReader.

We now skip nested Map handling in case the Map value is not a managed entity. Also added a more strict guard against the PersistentEntity lookup to make sure users see a more descriptive error in case of failures.
This commit is contained in:
Oliver Gierke
2015-11-10 11:58:02 +01:00
parent bb612e0ea0
commit 76f853ec03
2 changed files with 47 additions and 1 deletions

View File

@@ -104,7 +104,12 @@ public class DomainObjectReader {
Assert.notNull(target, "Existing object instance must not be null!");
Assert.notNull(mapper, "ObjectMapper must not be null!");
final PersistentEntity<?, ?> entity = entities.getPersistentEntity(target.getClass());
Class<? extends Object> type = target.getClass();
final PersistentEntity<?, ?> entity = entities.getPersistentEntity(type);
Assert.notNull(entity, "No PersistentEntity found for ".concat(type.getName()).concat("!"));
final MappedProperties properties = getJacksonProperties(entity, mapper);
entity.doWithProperties(new SimplePropertyHandler() {
@@ -155,6 +160,11 @@ public class DomainObjectReader {
Assert.notNull(mapper, "ObjectMapper must not be null!");
PersistentEntity<?, ?> entity = entities.getPersistentEntity(target.getClass());
if (entity == null) {
return mapper.readerForUpdating(target).readValue(root);
}
MappedProperties mappedProperties = getJacksonProperties(entity, mapper);
for (Iterator<Entry<String, JsonNode>> i = root.fields(); i.hasNext();) {

View File

@@ -19,6 +19,7 @@ import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
@@ -56,6 +57,7 @@ public class DomainObjectReaderUnitTests {
MongoMappingContext mappingContext = new MongoMappingContext();
mappingContext.getPersistentEntity(SampleUser.class);
mappingContext.getPersistentEntity(Person.class);
mappingContext.getPersistentEntity(TypeWithGenericMap.class);
mappingContext.afterPropertiesSet();
PersistentEntities entities = new PersistentEntities(Collections.singleton(mappingContext));
@@ -114,6 +116,34 @@ public class DomainObjectReaderUnitTests {
assertThat(result.relatedUsers.get("parent").name, is("Oliver"));
}
/**
* @see DATAREST-701
*/
@Test
public void mergesNestedMapWithoutTypeInformation() throws Exception {
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree("{\"map\" : {\"a\": \"1\", \"b\": {\"c\": \"2\"}}}");
TypeWithGenericMap target = new TypeWithGenericMap();
target.map = new HashMap<String, Object>();
target.map.put("b", new HashMap<String, Object>());
reader.readPut((ObjectNode) node, target, mapper);
}
/**
* @see DATAREST-701
*/
@Test(expected = IllegalArgumentException.class)
public void rejectsMergingUnknownDomainObject() throws Exception {
ObjectMapper mapper = new ObjectMapper();
ObjectNode node = (ObjectNode) mapper.readTree("{}");
reader.readPut(node, "", mapper);
}
@JsonAutoDetect(fieldVisibility = Visibility.ANY)
static class SampleUser {
@@ -140,4 +170,10 @@ public class DomainObjectReaderUnitTests {
this.lastName = lastName;
}
}
@JsonAutoDetect(fieldVisibility = Visibility.ANY)
static class TypeWithGenericMap {
Map<String, Object> map;
}
}