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
Comments
Comment #3
klausiHm, looks like you forgot to add the ->addCacheableDependency() call on the $response object? See EntityResource for example which does that for every response.
Comment #5
marthinal commented@klausi Yep, Many thanks for your help!