Problem/Motivation
Unable to pass $data/unserialized parameter containing request body to method 'put' of custom rest resource plugin. This type of request is not considered in \Drupal\rest\RequestHandler::createArgumentResolver when preparing list of available arguments for argument resolver. I suppose it was not considered due to https://groups.drupal.org/node/284948.
Steps to reproduce
- Implement a custom rest resource
- Implement method 'put' in the custom rest resource with parameter $data or $unserialized
- Make a PUT request to the custom rest resource
Proposed resolution
Change condition from
if (in_array($request->getMethod(), ['PATCH', 'POST'], TRUE)) {
to
if (in_array($request->getMethod(), ['PATCH', 'POST', 'PUT'], TRUE)) {
As I see from https://groups.drupal.org/node/284948, PUT was put off because it adds problems for dealing with rest resources for entities. But there are cases when developers create rest resources not dealing with any entity. In this case the developers are fully responsible for logic of such resources including method 'put'. They probably don't face problems described in https://groups.drupal.org/node/284948 . So, I think we should allow such developers to implement proper REST API with support of PUT. Also, PUT is allowed by the rest resource base class. So, I think we should keep support of PUT on the basic level of rest resources since it was already done.
Remaining tasks
No
User interface changes
No
API changes
No
Data model changes
No
Release notes snippet
Fixed argument resolver for methods 'put' and 'delete' of REST resource plugins so, that now it provides request body to parameters with name $data or $unserialized.
Comment | File | Size | Author |
---|
Issue fork drupal-3226944
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
sergei_brill CreditAttribution: sergei_brill commentedComment #4
daffie CreditAttribution: daffie commented@sergei_brill: Thank you for your MR. I am changing the issue to a feature request as core has made the decision to not support put here and you would like to add it. Also the MR needs some automatic test to make sure that your change works and keeps working in the future.
Comment #5
drasgardian CreditAttribution: drasgardian at Eighty Options commentedThe same issues also apply to DELETE requests.
Plugins implementing delete don't have access to the request body.
Comment #6
sergei_brill CreditAttribution: sergei_brill commentedComment #7
sergei_brill CreditAttribution: sergei_brill commentedComment #10
sergei_brill CreditAttribution: sergei_brill as a volunteer commentedChanged version to 9.3.x since now it is considered a feature request. Tests added, resolving of arguments $data/$unserialized in delete method added.
Comment #11
sergei_brill CreditAttribution: sergei_brill as a volunteer commentedComment #13
NiklanAgreed that
DELETE
should receive$data
/$unserialized
values.According to RFC 7231, DELETE method can have body. MDN also says:
The current solution is to accept
$request
argument and get body from it, the problem is, that if resource supports for multiple content types, it must implement something like\Drupal\rest\RequestHandler::deserialize()
to handle multiple types.Workaround:
Comment #15
ahana.kundu CreditAttribution: ahana.kundu commentedComment #16
ranjith_kumar_k_u CreditAttribution: ranjith_kumar_k_u at Zyxware Technologies commentedComment #18
Janvi Dasani CreditAttribution: Janvi Dasani commentedAdded patch against #16 in 10.1.x
Comment #20
smustgrave CreditAttribution: smustgrave at Mobomo commentedCan the MR be updated for 10.1 please
Hiding files as the issue appears to be worked in the MR.