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

CommentFileSizeAuthor
#9 3517128-9.patch6.06 KBjsacksick
#7 cacheable-dependency.diff3.42 KBdobe
Command icon 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

yannickbaettig created an issue. See original summary.

jsacksick’s picture

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

yannickbaettig’s picture

Hi jsacksick

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

  • jsacksick committed c0713895 on 8.x-1.x
    Issue #3517128 by yannickbaettig, jsacksick: Error: Call to undefined...
jsacksick’s picture

Status: Active » Fixed

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

dobe’s picture

Status: Fixed » Needs review
StatusFileSize
new3.42 KB

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.

jsobiecki’s picture

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
  • - 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?
jsacksick’s picture

StatusFileSize
new6.06 KB

Actually, we need to do this everywhere...

  • jsacksick committed 55afb5af on 8.x-1.x
    [#3517128] fix: Error: Call to undefined method Drupal\jsonapi\...
jsacksick’s picture

Status: Needs review » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.