From 349c3b97771f11105697aef09425dc9891401cc9 Mon Sep 17 00:00:00 2001 From: Jon Brisbin Date: Fri, 17 Aug 2012 15:07:45 -0500 Subject: [PATCH] Missed checking for non-optional relationship on DELETE a singular relationship. Now works as it's supposed to. --- .../data/rest/webmvc/RepositoryRestController.java | 6 ++++++ .../data/rest/webmvc/spec/RelationshipsSpec.groovy | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestController.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestController.java index 80915e1d8..7735de639 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestController.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestController.java @@ -1313,6 +1313,12 @@ public class RepositoryRestController return notFoundResponse(request); } + // Check if this is a @*ToOne relationship and is optional and if not, fail with a 405 Method Not Allowed + if((attrMeta.hasAnnotation(ManyToOne.class) && !attrMeta.annotation(ManyToOne.class).optional()) + || (attrMeta.hasAnnotation(OneToOne.class) && !attrMeta.annotation(OneToOne.class).optional())) { + return negotiateResponse(request, HttpStatus.METHOD_NOT_ALLOWED, new HttpHeaders(), null); + } + Object linked = attrMeta.get(entity); attrMeta.set(null, entity); diff --git a/spring-data-rest-webmvc/src/test/groovy/org/springframework/data/rest/webmvc/spec/RelationshipsSpec.groovy b/spring-data-rest-webmvc/src/test/groovy/org/springframework/data/rest/webmvc/spec/RelationshipsSpec.groovy index 4a49a03ec..920b64798 100644 --- a/spring-data-rest-webmvc/src/test/groovy/org/springframework/data/rest/webmvc/spec/RelationshipsSpec.groovy +++ b/spring-data-rest-webmvc/src/test/groovy/org/springframework/data/rest/webmvc/spec/RelationshipsSpec.groovy @@ -51,8 +51,8 @@ class RelationshipsSpec extends BaseSpec { def "cannot delete a required relationship"() { when: - def request = createRequest("DELETE", "address/$addrId/person/$persId", null) - def response = controller.deleteLink(request, "address", "$addrId", "person", "$persId") + def request = createRequest("DELETE", "address/$addrId/person", null) + def response = controller.clearLinks(request, "address", "$addrId", "person") then: response.statusCode == HttpStatus.METHOD_NOT_ALLOWED