Fix cacheability leakage in hook_node_update(), to allow PATCH requests in REST and JSON:API to work

Created on 8 January 2020, over 4 years ago
Updated 22 March 2024, 3 months ago

So this isn't a direct bug bug, but a bug having to do with another module.

If you use this module along with the drupal core jsonapi module and you POST to the jsonapi, then one line of code causes issue.

To reproduce:
Enable this module
Enable the jsonapi module
Go to: /admin/config/services/jsonapi and then click on the radio button "Accept all JSON:API create, read, update, and delete operations." and save it.

From a tool like Postman now POST to drupal to create a new page.
EX:
POST http://drupallocal.com/jsonapi/node/article
(Body)

{
  "data": {
    "type": "node--article",
    "attributes": {
      "title": "TEST 1 PAGE"
    }
  }
}  

And Drupal will throw the following error:
Uncaught PHP Exception LogicException: "The controller result claims to be providing relevant cache metadata, but leaked metadata was detected. Please ensure you are not rendering content too early. Returned object class: Drupal\\jsonapi\\ResourceResponse."

The stack trace goes back to line
views_url_alias_node.module file line 108

$alias = $entity->toUrl()->toString();

This is because the drupal ->toUrl() call triggers early rendering. (For more info on that read this article https://www.lullabot.com/articles/early-rendering-a-lesson-in-debugging-...)

The fix is to get the Url this way instead:

$alias = \Drupal::service('path.alias_manager')->getAliasByPath('/node/'.$entity->id());

And it returns the exact same thing as your line of code, only it won't trigger the early rendering (which the JSONAPI makes a fuss about).
(heres more info on issue https://www.drupal.org/project/drupal/issues/3093471 β†’ )

πŸ› Bug report
Status

Needs review

Version

3.0

Component

Code

Created by

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024