Error: Call to undefined method Drupal\jsonapi\ResourceResponse::addCacheableDependency() with PATCH request

Created on 3 April 2025, 6 months ago

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()

๐Ÿ› Bug report
Status

Active

Component

Code

Created by

๐Ÿ‡จ๐Ÿ‡ญSwitzerland yannickbaettig

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @yannickbaettig
  • @yannickbaettig opened merge request.
  • ๐Ÿ‡ฎ๐Ÿ‡ฑIsrael jsacksick

    hm... you're either running an outdated version of Drupal or possibly even JSONAPI Resources.

  • ๐Ÿ‡จ๐Ÿ‡ญSwitzerland yannickbaettig

    Hi jsacksick

    hmm I'm running:
    Drupal 10.4.6 and JSONAPI Resources 1.2.0

  • ๐Ÿ‡ฎ๐Ÿ‡ฑIsrael jsacksick

    I committed a slightly different fix, testing the interface instead.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States dobe

    Heres 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.

  • Status changed to Needs review 3 months ago
  • ๐Ÿ‡ต๐Ÿ‡ฑPoland jsobiecki Wroclaw

    Thank 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:

    • - src/Resource/CartCollectionResource.php โ€” Do we need to add a check here?
    • - src/Resource/CheckoutResource.php โ€” Already fixed in the dev branch.
    • - src/Resource/Checkout/PaymentResource.php โ€” Fixed by this patch: https://www.drupal.org/project/commerce_api/issues/3517128#comment-16112759 ๐Ÿ› Error: Call to undefined method Drupal\jsonapi\ResourceResponse::addCacheableDependency() with PATCH request Active
    • - src/Resource/ShippingMethodsResource.php โ€” Fixed via the same patch as above.
    • - src/Resource/CartUpdateItemResource.php โ€” Also fixed via the same patch.
    • - src/Resource/CurrentStoreResource.php โ€” Do we need changes here?
    • - src/Plugin/jsonapi_hypermedia/LinkProvider/ShippingMethodsLinkProvider.php โ€” Should we update this as well?
    • - src/Plugin/jsonapi_hypermedia/LinkProvider/PaymentLinkProviderBase.php โ€” Should a change be made here?
Production build 0.71.5 2024