Should nested relationships be reflected in URLs for JSON API?

Viewed 2032

I'm trying to follow JSON API. I need to expose CRUD access to a nested resource: product reviews.

Prior to using JSON API, I'd expect a REST interface like this:

GET    /products/:product_id/reviews     - list reviews for a product
POST   /products/:product_id/reviews     - add a review for a product
PATCH  /products/:product_id/reviews/:id - update a review for a product
DELETE /products/:product_id/reviews/:id - delete a review for a product

I see some mention of a nested structure like this in the spec:

For example, the URL for a photo’s comments will be:

/photos/1/comments

But I'm not sure whether this structure is intended for all actions.

On the one hand, POST /products/:product_id/reviews for creation seems redundant if I'm going to specify the product in the POST body, under the review data's relationships.

On the other hand, if it's useful to specify a product id when deleting a review (maybe it isn't), DELETE /products/:product_id/reviews/:id seems like the only sane way to do it; people argue about whether a request body is even allowed for DELETE requests.

I could nest for some requests and not others:

GET    /products/:product_id/reviews  - list reviews for a product
POST   /products/:product_id/reviews  - add a review for a product
PATCH  /reviews/:id                   - update a review
DELETE /reviews/:id                   - delete a review

But that seems weirdly inconsistent.

I could never nest:

GET    /reviews     - list reviews for the product specified in params
POST   /reviews     - add a review for the product specified in params
PATCH  /reviews/:id - update a review
DELETE /reviews/:id - delete a review

But that seems awkward, and doesn't seem to match the first quote I made from the docs.

Should nested resource relationships be reflected in the URL when using JSON API?

2 Answers
Related