Problem/Motivation

Found working on #2291055: REST resources for anonymous users: register.

When registering a new user we send an email to the user with some data. Building this email we need to replace tokens with the data (i.e. url to activate the account). And... here is the 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\rest\ResourceResponse. in Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext() (line 160 of core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php).
 

The problem is here:
EarlyRenderingControllerWrapperSubscriber::wrapControllerExecutionInRenderContext()

    // If early rendering happened, i.e. if code in the controller called
    // drupal_render() outside of a render context, then the bubbleable metadata
    // for that is stored in the current render context.
    if (!$context->isEmpty()) {

I'm not sure about the best way to fix this problem.

Test attached to reproduce the problem.

Proposed resolution

Remaining tasks

CommentFileSizeAuthor
earlyrender.patch2.45 KBmarthinal

Comments

marthinal created an issue. See original summary.

Status: Needs review » Needs work

The last submitted patch, earlyrender.patch, failed testing.

klausi’s picture

Hm, looks like you forgot to add the ->addCacheableDependency() call on the $response object? See EntityResource for example which does that for every response.

The last submitted patch, earlyrender.patch, failed testing.

marthinal’s picture

Status: Needs work » Closed (works as designed)

@klausi Yep, Many thanks for your help!