From f80bebf04bb82cb8a7eae7403355ad13546802a7 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 | 18 +++++++++++++ .../data/rest/webmvc/mongodb/Profile.java | 26 +++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) 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 a449d1719..33aff239b 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 { PersistentPropertyAccessor accessor = entity.getPropertyAccessor(target); Object nested = accessor.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 f96e5101b..32c164fa9 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 @@ -22,6 +22,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. import java.math.BigDecimal; import java.util.Arrays; +import java.util.Collections; import org.junit.After; import org.junit.Before; @@ -256,4 +257,21 @@ public class MongoWebTests extends CommonWebTests { andExpect(status().isOk()).// andExpect(jsonPath("$").exists()); } + + /** + * @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, mapper.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 02e2d0eaa..f82ac1e90 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,6 +1,23 @@ +/* + * 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.Date; +import java.util.HashMap; +import java.util.Map; import org.springframework.data.annotation.Id; import org.springframework.data.annotation.LastModifiedDate; @@ -20,6 +37,7 @@ public class Profile { private @JsonProperty(required = true) String type; private @LastModifiedDate Date lastModifiedDate; private @JsonProperty("renamed") String aliased; + private Map metadata = new HashMap(); public String getId() { return id; @@ -56,4 +74,12 @@ public class Profile { public String getAliased() { return aliased; } + + public Map getMetadata() { + return metadata; + } + + public void setMetadata(Map metadata) { + this.metadata = metadata; + } }