Problem/Motivation
If the patch request body doesn't contain ID, then in logs appears this warning:
Warning: Undefined array key "id" in Drupal\jsonapi\Controller\EntityResource->patchIndividual() (line 322 of /var/www/html/web/core/modules/jsonapi/src/Controller/EntityResource.php) #0 /var/www/html/web/core/includes/bootstrap.inc(347): _drupal_error_handler_real() #1 /var/www/html/web/core/modules/jsonapi/src/Controller/EntityResource.php(322): _drupal_error_handler() #2 [internal function]: Drupal\jsonapi\Controller\EntityResource->patchIndividual() #3 /var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array() #4 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(580): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\@closure() #5 /var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext() #6 /var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext() #7 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(169): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\@closure() #8 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(81): Symfony\Component\HttpKernel\HttpKernel->handleRaw() #9 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/Session.php(58): Symfony\Component\HttpKernel\HttpKernel->handle() #10 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle() #11 /var/www/html/web/modules/custom/upstart_commerce_blog_management/src/UpStartCommerceBlogManagementBlogMiddleware.php(85): Drupal\Core\StackMiddleware\KernelPreHandle->handle() #12 /var/www/html/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\upstart_commerce_blog_management\UpStartCommerceBlogManagementBlogMiddleware->handle() #13 /var/www/html/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass() #14 /var/www/html/vendor/asm89/stack-cors/src/Asm89/Stack/Cors.php(60): Drupal\page_cache\StackMiddleware\PageCache->handle() #15 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Asm89\Stack\Cors->handle() #16 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle() #17 /var/www/html/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle() #18 /var/www/html/web/core/lib/Drupal/Core/DrupalKernel.php(718): Stack\StackedHttpKernel->handle() #19 /var/www/html/web/index.php(19): Drupal\Core\DrupalKernel->handle() #20 @main.
Steps to reproduce
Send a PATCH request to any resource and skip in the body the id attribute.
Proposed resolution
Add isset() check into line 318 of class Drupal\jsonapi\Controller\EntityResource
| Comment | File | Size | Author |
|---|---|---|---|
| #15 | 3377269-only-test.patch | 1.93 KB | reinfate |
Issue fork drupal-3377269
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:
- 11.x
changes, plain diff MR !4531
- 3377269-warning-undefined-array
changes, plain diff MR !4484
Comments
Comment #3
_tarik_ commentedComment #4
smustgrave commentedMR should be updated for 11.x
Also will need to research why that value is not set. Could be masking a larger issue.
Comment #5
bbralaThanks for the contribution, i'd prefer we use 'isset' instead of empty since empty has a few issues.
Comment #6
_tarik_ commentedHi smustgrave (#4)
This problem can be reproduced only if the id attribute wasn't added to the request body. So we can be calm about the module functionality.
Comment #7
_tarik_ commentedHi bbrala (#5)
Sure I can do this, but could you a bit clarify what issues we can get with using empty() in this part of the code?
I used it because, in this way, we will skip all cases when data['id'] contains an empty string or any other value that can't be for the uuid
Thanks!
Comment #8
project update bot commented[removed]
Comment #9
bbralaIf you use isset then you know the key exists. Although array_key_exists is technically even cleaner, isset is fine. After that the code would check if the value is an uuid. Does that make sense?
Comment #11
_tarik_ commentedbbrala
Thanks for your response!
I got your point and updated the pull request now it uses !isset() instead empty().
Also was created a new branch for the 11.x version of Drupal core.
Comment #12
_tarik_ commentedComment #13
smustgrave commentedStill needs a test
How is this reproducible from core? This may be a bug in core that putting isset() is covering up.
Comment #15
reinfate commentedAdded test for the mentioned issue.
Also, there was the same issue just under a few lines that I fixed as well.
I am uploading the test-only patch as well.
@smustgrave There is nothing above,
Drupal\jsonapi\Controller\EntityResource->patchIndividual()it is Controller. Also, JSON:API correctly responds to the request without the "id" key in the body. The only issue is PHP warnings.P.S. I realized CI is dead, should I make a separate branch for the test only now?
P.S.S. Or not dead, no idea :)
Comment #16
poker10 commented@ReINFaTe No need to upload a separate test only patch or created a new branch if tests are included in the bugfix patch. We can run a test-only Gitlab pipeline now, which will automatically run the tests from the MR, see: https://www.drupal.org/about/core/blog/drupal-cores-gitlab-ci-testing-is... (Test-only runs are now automated in GitLab CI).
Comment #17
smustgrave commentedThe tests-only patch does show the issue, so this may be good to go. Lets see.
Comment #18
quietone commentedI read the IS and comments. I did not find any unanswered questions.
Comment #19
longwaveAdded a question to the MR.
Comment #20
smustgrave commentedAppears the question was answered, doesn't appear a change is required.
Comment #23
longwaveThanks for answering my question.
This is eligible for backport to 10.2.x as a bug fix.
Committed and pushed b930619b05 to 11.x and bb6903e8e6 to 10.2.x. Thanks!