Problem/Motivation
We get the following error, when updating the checkout with a PATCH request :
Error: Call to undefined method Drupal\jsonapi\ResourceResponse::addCacheableDependency() in Drupal\commerce_api\Resource\CheckoutResource->process() (line 145 of /var/www/html/web/modules/contrib/commerce_api/src/Resource/CheckoutResource.php).
The issue seems to come from the fact that Drupal\jsonapi_resources\Unstable\ResourceResponseFactory creates a ResourceResponse, which doesnt have the addCacheableDependency() method.
#0 /var/www/html/web/modules/contrib/jsonapi_resources/src/Unstable/Controller/JsonapiResourceController.php(70): Drupal\commerce_api\Resource\CheckoutResource->process(Object(Symfony\Component\HttpFoundation\Request), Array, Object(Drupal\commerce_order\Entity\Order), Object(Drupal\jsonapi\JsonApiResource\JsonApiDocumentTopLevel))
#1 [internal function]: Drupal\jsonapi_resources\Unstable\Controller\JsonapiResourceController->processRequest(Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Routing\RouteMatch))
#2 /var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array(Array, Array)
#3 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(638): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#4 /var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(121): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure))
#5 /var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array)
#6 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(181): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#7 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(76): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
#8 /var/www/html/web/modules/contrib/simple_oauth/src/HttpMiddleware/BasicAuthSwap.php(68): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#9 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/Session.php(53): Drupal\simple_oauth\HttpMiddleware\BasicAuthSwap->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#10 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#11 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/ContentLength.php(28): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#12 /var/www/html/vendor/asm89/stack-cors/src/Cors.php(53): Drupal\Core\StackMiddleware\ContentLength->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#13 /var/www/html/web/modules/contrib/shield/src/ShieldMiddleware.php(266): Asm89\Stack\Cors->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#14 /var/www/html/web/modules/contrib/shield/src/ShieldMiddleware.php(133): Drupal\shield\ShieldMiddleware->bypass(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#15 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\shield\ShieldMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#16 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#17 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/AjaxPageState.php(36): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#18 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/StackedHttpKernel.php(51): Drupal\Core\StackMiddleware\AjaxPageState->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#19 /var/www/html/web/core/lib/Drupal/Core/DrupalKernel.php(741): Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#20 /var/www/html/web/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#21 {main}Steps to reproduce
Do a PATCH or POST request to https://commerce.api/jsonapi/checkout/{order}
Proposed resolution
Check if the response is a CacheableResourceResponse before calling addCacheableDependency()
| Comment | File | Size | Author |
|---|---|---|---|
| #9 | 3517128-9.patch | 6.06 KB | jsacksick |
| #7 | cacheable-dependency.diff | 3.42 KB | dobe |
Issue fork commerce_api-3517128
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
jsacksick commentedhm... you're either running an outdated version of Drupal or possibly even JSONAPI Resources.
Comment #4
yannickbaettig commentedHi jsacksick
hmm I'm running:
Drupal 10.4.6 and JSONAPI Resources 1.2.0
Comment #6
jsacksick commentedI committed a slightly different fix, testing the interface instead.
Comment #7
dobe commentedHeres a patch to update the other resources with the same logic @jsacksick used. As I was experiencing it with the PaymentResource as well. So figured just find them all.
Comment #8
jsobiecki commentedThank you for the provided patch! I can confirm that it resolves the issue on my end.
It appears the root cause of the problem was related to how ResourceResponseFactory behaves. Depending on the method’s cacheability, it returns either a ResourceResponse or a CacheableResourceResponse.
I reviewed the usage of addCacheableDependency across the project. Here are my findings:
Comment #9
jsacksick commentedActually, we need to do this everywhere...
Comment #11
jsacksick commented