I have the following issue where, when using JSON API to create/update product variations with auto sku enabled, I receive the following error:

{
    "errors": [
        {
            "title": "Internal Server Error",
            "status": 500,
            "detail": "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.",
            "links": {
                "via": "http://xxx.ddev.local/jsonapi/commerce_product_variation/disc",
                "info": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.1"
            }
        }
    ],
    "jsonapi": {
        "version": "1.0",
        "meta": {
            "links": {
                "self": "http://jsonapi.org/format/1.0/"
            }
        }
    }
}

I tracked the issue down to the Token.php plugin... specifically the getSku method that is replacing the SKU tokens. When I add an empty $bubbleable_metadata = new BubbleableMetadata(); object to the replace method call, the above error goes away.

Attached is a patch that fixes this for me. However, my BubbleableMetadata knowledge is a little sparse, so I would love some feedback on this fix.

CommentFileSizeAuthor
autosku-cache-jsonapi-initial.patch1.16 KBpfrilling
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

pfrilling created an issue. See original summary.

bojanz’s picture

Pathauto has an example of this:

    // Pass empty BubbleableMetadata object to explicitly ignore cacheablity,
    // as the result is never rendered.
    $alias = $this->token->replace($pattern->getPattern(), $data, [
      'clear' => TRUE,
      'callback' => [$this->aliasCleaner, 'cleanTokenValues'],
      'langcode' => $langcode,
      'pathauto' => TRUE,
    ], new BubbleableMetadata());

We'll probably need the same fix in Commerce and Commerce Invoice.

  • bojanz committed b7439ed on 8.x-2.x authored by pfrilling
    Issue #2998603 by pfrilling, bojanz: Auto SKU with Json API results in a...
bojanz’s picture

Status: Needs review » Fixed

Opened #3107121: Token replacement leaks cache metadata in non-HTML contexts (e.g., JSON:API) on the Commerce side, and committed the patch with a few tweaks. Thanks!

Status: Fixed » Closed (fixed)

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