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.

CommentFileSizeAuthor
EntityResource-20160614.patch824 bytesflemming.fridthjof
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

flemming.fridthjof created an issue. See original summary.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Wim Leers’s picture

Title: X-Drupal-Cache-Tags and Drupal RESTful Web Services missing related entities » [PP-1] EntityResource only sets entity cache tags on response; cache tags for entity reference fields missing
Priority: Normal » Major
Status: Active » Postponed
Issue tags: -X-Drupal-Cache-Tags, -REST +D8 cacheability, +Needs tests

Thanks 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.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Wim Leers’s picture

Priority: Major » Normal
Issue tags: +Needs issue summary update

Well, actually, it's not that bad.

The uid field is an entity reference to a User. Its json serialization is this:

"uid": [{
  "target_id": "1",
  "target_type": "user",
  "target_uuid": "32e583bb-5451-4021-bd9b-d04d7596857c",
  "url": "/user/1"
}],

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 a Term:

"field_tags": [
{
"target_id": "1",
"target_type": "taxonomy_term",
"target_uuid": "6f6e1564-c585-4bd1-91bc-cd76357b5109",
"url": "/taxonomy/term/1"
},
{
"target_id": "2",
"target_type": "taxonomy_term",
"target_uuid": "73900fea-abb0-4a61-871b-9535a6bd83ae",
"url": "/taxonomy/term/2"
}
}]

But then there's field_image, which is a reference to a File, and it includes data (from a field) from the File entity:

"field_image": [{
"target_id": "2",
"alt": "asdfasdf",
"title": "",
"width": "834",
"height": "523",
"target_type": "file",
"target_uuid": "5540a23e-9207-4769-a3c9-e558f989f64b",
"url": "http://d8/sites/default/files/2017-02/beta.png"
}
}]

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 the File entity, so those are not a problem.)

The originally reported problem is therefore much narrower in scope. Fortunately :)

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Wim Leers’s picture

Title: [PP-1] EntityResource only sets entity cache tags on response; cache tags for entity reference fields missing » EntityResource only sets entity cache tags on response; cache tags for entity reference fields missing
Status: Postponed » Closed (duplicate)
Related issues: +#2910211: Allow computed exposed properties in ComplexData to support cacheability., +#2825487: Fix normalization of File entities: file entities should expose the file URL as a computed property on the 'uri' base field

#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.