diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/DomainObjectReader.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/DomainObjectReader.java index 44b1b1bde..1d57ea6f2 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/DomainObjectReader.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/DomainObjectReader.java @@ -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 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> i = root.fields(); i.hasNext();) { diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/DomainObjectReaderUnitTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/DomainObjectReaderUnitTests.java index a66c129a7..8977c3fe9 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/DomainObjectReaderUnitTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/DomainObjectReaderUnitTests.java @@ -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(); + target.map.put("b", new HashMap()); + + 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 map; + } }