Seems to be a lack in the Drupal rest EntityResource code.
when using entity Drupal RESTful Web Services the X-Drupal-Cache-Tags header does not include the related entities.
Examples:
www.example.com/sv/start returns
"X-Drupal-Cache-Tags: block_content:1 block_content_view block_view config:block.block.preview config:block.block.seven_breadcrumbs config:block.block.seven_content config:block.block.seven_help config:block.block.seven_local_actions config:block.block.seven_login config:block.block.seven_messages config:block.block.seven_page_title config:block.block.seven_primary_local_tasks config:block.block.seven_secondary_local_tasks config:block_list config:filter.format.full_html config:system.site config:user.role.anonymous node:6816 node:6841 node:6865 node:6867 node:6869 node:6927 node:6928 node:7428 node:9370 node:9449 node:9452 node:9631 node:9660 node_view rendered user:0 user:1"
www.example.com/sv/start?_format=json returns
"X-Drupal-Cache-Tags: config:rest.settings config:user.role.anonymous node:9370”
If I add the following to "core/modules/rest/src/Plugin/rest/resource/EntityResource.php", method get, then all related entities are in the X-Drupal-Cache-Tags:
$entcb = function($refEnts, $resp) use (&$entcb) {
foreach($refEnts as $refEnt) {
$entcb($refEnt->referencedEntities(), $resp);
$resp->addCacheableDependency($refEnt);
}
};
$entcb($entity->referencedEntities(), $response);
www.example.com/sv/start?_format=json returns
X-Drupal-Cache-Tags: config:node.type.billboard config:node.type.static_frontpage config:node.type.teaser config:node.type.teaser_html config:rest.settings config:taxonomy.vocabulary.main config:taxonomy.vocabulary.selfservice config:taxonomy.vocabulary.teasers config:user.role.administrator config:user.role.anonymous config:user.role.editor_club_ config:user.role.editor_global_ file:264 file:3139 file:3411 file:3614 file:4019 file:4219 file:4245 file:4370 file:4793 file:5413 node:6816 node:6841 node:6865 node:6867 node:6869 node:6927 node:6928 node:7428 node:9370 node:9449 node:9452 node:9631 node:9660 taxonomy_term:280 taxonomy_term:281 taxonomy_term:282 taxonomy_term:283 taxonomy_term:284 user:0 user:1 user:19 user:28 user:3 user:6 user:7 user:8
I've added a patch file of my change if you'll like to add it.
Comment | File | Size | Author |
---|---|---|---|
EntityResource-20160614.patch | 824 bytes | flemming.fridthjof | |
Comments
Comment #3
Wim LeersThanks for reporting this! Totally agreed of course.
The fix is wrong though, this needs to be fixed in
\Drupal\serialization\Normalizer\EntityReferenceFieldItemNormalizer::normalize()
, from where it must be bubbled.But bubbling does not exist yet there, it's being added in #2825812: ImageItem should have an "derivatives" computed property, to expose all image style URLs. Once that's added, it'll be easier to update this too.
Comment #5
Wim LeersWell, actually, it's not that bad.
The
uid
field is an entity reference to aUser
. Itsjson
serialization is this:That doesn't require a cache tag. It's purely reference information: we don't show any of the content of the
User
entity.Similar for the
field_tags
field, which is an entity reference to aTerm
:But then there's
field_image
, which is a reference to aFile
, and it includes data (from a field) from theFile
entity:So, only in that last case is there a problem: we should have added the
file:2
cache tag because we include the final file URL, which would change whenever the underlying file is renamed. (The width, height etc are also not stored in theFile
entity, so those are not a problem.)The originally reported problem is therefore much narrower in scope. Fortunately :)
Comment #7
Wim Leers#2910211: Allow computed exposed properties in ComplexData to support cacheability. added the infrastructure to let normalizers bubble cacheability metadata.
Per #5, the problem isn't as bad though. And the problem described in #5 is being fixed in #2825487: Fix normalization of File entities: file entities should expose the file URL as a computed property on the 'uri' base field.
So I think it's safe to mark this as a duplicate.