From 8414b290b9acd4d562b764eba8fc470ca44ef530 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Fri, 20 Mar 2015 09:36:50 +0100 Subject: [PATCH] DATAREST-491 - Fixed handling of non-entity maps in JSON processing. During our custom JSON handling on PUT requests, we previously piped all JSON objects through code that expected that one to be mapped to a PersistentEntity. This fails for JSON objects that basically represent maps. We now skip that code paths for non-entity properties. --- .../rest/webmvc/json/DomainObjectReader.java | 2 +- .../rest/webmvc/mongodb/MongoWebTests.java | 21 ++++++++++++++- .../data/rest/webmvc/mongodb/Profile.java | 27 +++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) 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 9b6b1031a..1440f0b3e 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 @@ -181,7 +181,7 @@ public class DomainObjectReader { BeanWrapper wrapper = BeanWrapper.create(target, null); Object nested = wrapper.getProperty(property); - if (nested != null) { + if (nested != null && property.isEntity()) { doMerge((ObjectNode) child, nested, mapper); } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/mongodb/MongoWebTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/mongodb/MongoWebTests.java index fba1421e1..bbaee7f48 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/mongodb/MongoWebTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/mongodb/MongoWebTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2014 the original author or authors. + * Copyright 2013-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,7 @@ import static org.junit.Assert.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import java.util.Arrays; +import java.util.Collections; import org.junit.After; import org.junit.Before; @@ -32,6 +33,7 @@ import org.springframework.http.MediaType; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.test.context.ContextConfiguration; +import com.fasterxml.jackson.databind.ObjectMapper; import com.jayway.jsonpath.JsonPath; /** @@ -148,4 +150,21 @@ public class MongoWebTests extends CommonWebTests { assertThat(JsonPath.read(response.getContentAsString(), "$.lastname"), is(nullValue())); assertThat(JsonPath.read(response.getContentAsString(), "$.address.zipCode"), is((Object) "ZIP")); } + + /** + * @see DATAREST-491 + */ + @Test + public void updatesMapPropertyCorrectly() throws Exception { + + Link profilesLink = client.discoverUnique("profiles"); + Link profileLink = assertHasContentLinkWithRel("self", client.request(profilesLink)); + + Profile profile = new Profile(); + profile.setMetadata(Collections.singletonMap("Key", "Value")); + + putAndGet(profileLink, new ObjectMapper().writeValueAsString(profile), MediaType.APPLICATION_JSON); + + client.follow(profileLink).andExpect(jsonPath("$.metadata.Key").value("Value")); + } } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/mongodb/Profile.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/mongodb/Profile.java index 595d9e4ee..d985ac542 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/mongodb/Profile.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/mongodb/Profile.java @@ -1,10 +1,29 @@ +/* + * Copyright 2012-2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.springframework.data.rest.webmvc.mongodb; +import java.util.HashMap; +import java.util.Map; + import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; /** * @author Jon Brisbin + * @author Oliver Gierke */ @Document public class Profile { @@ -12,6 +31,7 @@ public class Profile { @Id private String id; private Long person; private String type; + private Map metadata = new HashMap(); public String getId() { return id; @@ -40,4 +60,11 @@ public class Profile { return this; } + public Map getMetadata() { + return metadata; + } + + public void setMetadata(Map metadata) { + this.metadata = metadata; + } }