spring-projects / spring-projects/spring-data-rest
Behavior of @RestResource is inconsistent/confusing [DATAREST-1517]
@odrotbohm is already working on this.
Since Dec 31, 2020.
- Dominant language
- Java
- Stars
- 958
- Forks
- 568
- PR merge metrics
- No merged PRs in 30d
Description
jvanheesch opened DATAREST-1517 and commented
This issue was discussed on Gitter.
The annotation @RestResource(exported=false) has the following effect:
- Placed on association (e.g.
book.author):
the association resource is not exported: requests to the corresponding uri (/books/\{id}/author) result in404. - Placed on query method (e.g.
bookRepository.findAllByAuthorId()):
the query method resource is not exported: requests to the corresponding uri (/books/search/findAllByAuthorId) result in404. - Placed on "special crud method" (e.g.
bookRepository.findById(), seeDefaultCrudMethods):
the resource is still exported (no404), instead the corresponding http method is disabled (405).
The behavior of 3 significantly differs from that of 1 and 2: @RestResource cannot be used to not-export the item resource, it can only be used to modify the exposure of http methods.
This leads to confusion, as demonstrated by this question on Stack Overflow, where findById() is not even declared yet an item resource is exported.
In addition, when annotating bookRepository.findById() with @RestResource(exported=false):
PUT/PATCH/DELETEto item resource are still supported if their corresponding methods are not explicitly annotated (and default exposure is used)- The item's association resources (e.g.
/books/\{id}/publisher) are still exported (GET/books/\{id}/publisher) still returns the book's publisher).
These 3 cases are demonstrated by unit tests (see reference url).
I don't have a detailed proposal for how things should work, but I do think the current functionality is confusing and should be reconsidered.
Side note: there is some other 404/405 behavior that seems strange to me (not demonstrated in reproducer):
PUT/PATCH/DELETEto collection resource results in404(whereas I'd expect405, as the uri exists)POSTto item resource results in404(whereas I'd expect405, as the uri exists)- As demonstrated by the earlier Stack Overflow question,
PUT/PATCHto item resource results in500iffindById()is not declared, yetDELETEresults in405.
This is due to&& crudMethods.hasFindOneMethod()inDefaultExposureAwareCrudMethods.exposesDelete.
We can't do something similar forDefaultExposureAwareCrudMethods.exposesSave, asexposesSaveis also used forPOSTto collection resource (create).
This does not seem like a big deal to me, but it does show that the&& crudMethods.hasFindOneMethod()logic inDefaultExposureAwareCrudMethods.exposesDeletemay be kind of a smell
Reference URL: https://github.com/jvanheesch/spring-data-rest-404-405
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.