From c532bb0d51bd25745686f4e4d6cf644f73275b8a Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 3 Mar 2015 09:49:38 +0100 Subject: [PATCH] =?UTF-8?q?DATAREST-485=20-=20Fixed=20handling=20of=20empt?= =?UTF-8?q?y=20requests=20for=20RepositoryPropertyReferenceController.crea?= =?UTF-8?q?tePropertyReference(=E2=80=A6).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The test case introduced for DATAREST-482 failed when running against Spring 4.1 generation as the handling of empty request payload changed slightly in 4.1. We now explicitly use required = false to get null handed into the controller and manually default the Resources in it. Related tickets: DATAREST-482, SPR-12778. --- .../RepositoryPropertyReferenceController.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryPropertyReferenceController.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryPropertyReferenceController.java index ab33ef1df..4751fbd38 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryPropertyReferenceController.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryPropertyReferenceController.java @@ -21,6 +21,7 @@ import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*; import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -295,10 +296,11 @@ class RepositoryPropertyReferenceController extends AbstractRepositoryRestContro consumes = { "application/json", "application/x-spring-data-compact+json", "text/uri-list" }) @ResponseBody public ResponseEntity createPropertyReference( - final RootResourceInformation resourceInformation, final HttpMethod requestMethod, - final @RequestBody Resources incoming, @BackendId Serializable id, @PathVariable String property) + final RootResourceInformation resourceInformation, final HttpMethod requestMethod, final @RequestBody( + required = false) Resources incoming, @BackendId Serializable id, @PathVariable String property) throws Exception { + final Resources source = incoming == null ? new Resources(Collections.emptyList()) : incoming; final RepositoryInvoker invoker = resourceInformation.getInvoker(); Function handler = new Function() { @@ -318,7 +320,7 @@ class RepositoryPropertyReferenceController extends AbstractRepositoryRestContro } // Add to the existing collection - for (Link l : incoming.getLinks()) { + for (Link l : source.getLinks()) { Object propVal = loadPropertyValue(prop.propertyType, l); coll.add(propVal); } @@ -335,7 +337,7 @@ class RepositoryPropertyReferenceController extends AbstractRepositoryRestContro } // Add to the existing collection - for (Link l : incoming.getLinks()) { + for (Link l : source.getLinks()) { Object propVal = loadPropertyValue(prop.propertyType, l); m.put(l.getRel(), propVal); } @@ -349,12 +351,12 @@ class RepositoryPropertyReferenceController extends AbstractRepositoryRestContro "Cannot PATCH a reference to this singular property since the property type is not a List or a Map."); } - if (incoming.getLinks().size() != 1) { + if (source.getLinks().size() != 1) { throw new IllegalArgumentException( "Must send only 1 link to update a property reference that isn't a List or a Map."); } - Object propVal = loadPropertyValue(prop.propertyType, incoming.getLinks().get(0)); + Object propVal = loadPropertyValue(prop.propertyType, source.getLinks().get(0)); prop.accessor.setProperty(prop.property, propVal); }