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