2
votes

I am using Spring Data REST. I am trying to unbind a collection association from an entity (item). i.e. - a property of the item is of List type. I want to remove all items from that List.

To do this, I am using the DELETE method:

curl -X DELETE …/categories/54ea0bcf27a2fb1b4641083a/fixedParentCategories

This gives me a 405 Method not allowed error code. But, it works for a single valued association (when it is not of List type). The documentation clearly lists DELETE as a supported method for associations. I'd like to know if there is a way around this. Also, I tried using PUT (Content-Type: text/uri-list) with an empty body, and it gives an error about missing request body. Other operations on this association are all working fine - I am able to add items to this collection, etc.

My entity looks like this:

@Document
public class Category { 

    @DBRef(lazy = true)
    private List<Category> fixedParentCategories;
    …
}
1
The documentation also states that 405 is returned for associations that are non-optional. What does your domain model look like? - Oliver Drotbohm
@Document @Description("Category") public class Category { @Description("Fixed parent categories") @DBRef(lazy = true) private List<Category> fixedParentCategories; } - Lijo Jacob
Added the domain object to the original post - Lijo Jacob
As I was testing this, I also observed this:- a PUT (Content-Type: application/json) on the item (the entity) clears all the associations. I ended up using PATCH so that they retain their value. Is this expected? Of course I read the documentation which says I need to use PATCH when I want only those properties provided in the request body JSON to be updated, and to leave the remaining untouched. But does that hold for the associations too? - Lijo Jacob

1 Answers

6
votes

I just checked the code and you're right, we're actively rejecting DELETE requests for Maps and collections. The rationale is as follows:

An association that is of Map or collection must never be null in the domain model. Translating this into HTTP resources means that the resource will always be available and in the worst case return an empty representation (empty JSON array, or empty JSON object). Accepting a DELETE request would logically null the relationship in the domain model and thus lead to a state that contradicts the very first assumption.

We generally recommend to simply PUT an empty body with media type text/uri-list to the association resource to empty out the associations as that's semantically more correct. Consider it like the difference between truncating and dropping a database table.

If you think that should change and have good reasons that you can back your request with, feel free to open a ticket in our JIRA.