Problem/Motivation

This issue will be a follow up to #2871591: Allow ComplexData in TypedData to specify computed properties that should be exposed in normalization and other contexts

We were dealing with this in #2871591] but decided it would simplify the patch/reviewing if we separated it out.

Because the new exposed computed properties will be used with #2626924: Include processed text in normalizations: "text" field type's "processed" computed property should be non-internal and carry cacheability metadata then we need to set cache contexts for the processed text.

We should make sure this is possible with BC breaks.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Comments

tedbow created an issue. See original summary.

wim leers’s picture

Title: Allow computed exposed properties in ComplexData to support cacheability. » [PP-1] Allow computed exposed properties in ComplexData to support cacheability.
Issue tags: +API-First Initiative, +blocker
wim leers’s picture

Issue tags: +D8 cacheability
wim leers’s picture

Status: Active » Postponed
wim leers’s picture

tedbow’s picture

Status: Postponed » Needs review
StatusFileSize
new8.25 KB
new42.15 KB

In #2871591: Allow ComplexData in TypedData to specify computed properties that should be exposed in normalization and other contexts which is now at comment #133 it is now clear that we need point out caching will work with the patch so I am starting this issue.

This patch is adding back the cache logic that was taken out in comment #78 of that issue.

Specifically this shows why we need the \Drupal\entity_test\Plugin\DataType\ComputedString as example of data that will need to be cache aware. As will be the case in #2626924: Include processed text in normalizations: "text" field type's "processed" computed property should be non-internal and carry cacheability metadata

tedbow’s picture

StatusFileSize
new10.86 KB
new42.03 KB

Ok. now that #2871591: Allow ComplexData in TypedData to specify computed properties that should be exposed in normalization and other contexts is RTBC here is reroll. Had to make some changes to this b/c of changes on that issue.

Status: Needs review » Needs work

The last submitted patch, 7: 2910211-7-plus-2871591-151.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

tedbow’s picture

Status: Needs work » Needs review
StatusFileSize
new9.29 KB
new6.15 KB
new40.45 KB
  1. Simplified PrimitiveDataNormalizer.php
  2. Removed use of RefinableCacheableDependencyTrait from ComputedString because this causes problems. Just implementing CacheableDependencyInterface is enough.
wim leers’s picture

Status: Needs review » Needs work

👍

  1. +++ b/core/modules/rest/src/EventSubscriber/ResourceResponseSubscriber.php
    @@ -152,14 +160,25 @@ protected function renderResponseBody(Request $request, ResourceResponseInterfac
    +      // @deprecated In Drupal 8.4.0, will be removed before Drupal 9.0.0. Use
    

    8.5.0

  2. +++ b/core/modules/system/tests/modules/entity_test/src/TypedData/ComputedString.php
    @@ -15,9 +17,8 @@ class ComputedString extends TypedData {
    -    $computed_value = "Computed! " . $item->get('value')->getString();
     
    -    return $computed_value;
    +    return 'Computed! ' . $item->get('value')->getString();
    

    Nit: This change is unnecessary.

  3. Most importantly, I'm missing a failing test-only patch, that proves that the changes in ResourceResponseSubscriber are necessary.
tedbow’s picture

Status: Needs work » Needs review
StatusFileSize
new40.49 KB
new8.93 KB
new1.6 KB

@Wim Leers thank for review.
#10
1. Fixed
2. fixed
3. yes. need to happen but seems like the test only patch will make more sense after #2871591: Allow ComplexData in TypedData to specify computed properties that should be exposed in normalization and other contexts

wim leers’s picture

Status: Needs review » Postponed

but seems like the test only patch will make more sense after #2871591: Allow ComplexData in TypedData to specify computed properties that should be exposed in normalization and other contexts

True!

So I think this is now really postponed.

wim leers’s picture

Title: [PP-1] Allow computed exposed properties in ComplexData to support cacheability. » Allow computed exposed properties in ComplexData to support cacheability.
Status: Postponed » Needs review
StatusFileSize
new9.35 KB
wim leers’s picture

StatusFileSize
new3.99 KB
new10.04 KB
index ef33817..99d99de 100644
--- a/core/modules/rest/src/EventSubscriber/ResourceResponseSubscriber.php
+++ b/core/modules/rest/src/EventSubscriber/ResourceResponseSubscriber.php

#2864816: HAL LinkManager doesn't add 'url.site' cache context when needed is making the exact same changes. But that patch was also reviewed by @dawehner, and had some additional remarks. So, bringing over the changes from that issue to this one.

wim leers’s picture

Status: Needs review » Reviewed & tested by the community
  1. In #13, I did a straight rebase of @tedbow's patch.
  2. In #15, I copied over some changes that were requested and RTBC'd by @dawehner from another issue.
  3. The changes to ResourceResponseSuscriber are half of the crucial changes in this issue, and that was also RTBC'd in #2864816
  4. The addition of NormalizerBase::addCacheableDependency() plus TypedDataNormalizer::normalize() using it are the other half of crucial changes. This also comes with explicit functional test coverage. I think those changes are sound too.

Therefore RTBC'ing.

larowlan’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/serialization/src/Normalizer/NormalizerBase.php
@@ -81,4 +82,18 @@ protected function checkFormat($format = NULL) {
+    if ($data instanceof CacheableDependencyInterface && isset($context['cacheability'])) {
+      $context['cacheability']->addCacheableDependency($data);

this should use the constant

Other than that, feels like I've reviewed this elsewhere ;) - oh right - #2864816: HAL LinkManager doesn't add 'url.site' cache context when needed

Do you have a preference for the order they go in?

wim leers’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new1.33 KB
new10.19 KB

Agreed, done. Trivial change, so back to RTBC.

Do you have a preference for the order they go in?

This issue blocks many others (see #14), so this going in first would make sense!

  • larowlan committed 52a598e on 8.5.x
    Issue #2910211 by tedbow, Wim Leers: Allow computed exposed properties...
larowlan’s picture

Status: Reviewed & tested by the community » Fixed

Fixed on commit

diff --git a/core/modules/rest/src/EventSubscriber/ResourceResponseSubscriber.php b/core/modules/rest/src/EventSubscriber/ResourceResponseSubscriber.php
index 9b12412..babb238 100644
--- a/core/modules/rest/src/EventSubscriber/ResourceResponseSubscriber.php
+++ b/core/modules/rest/src/EventSubscriber/ResourceResponseSubscriber.php
@@ -180,7 +180,7 @@ protected function renderResponseBody(Request $request, ResourceResponseInterfac
       // executeInRenderContext() will be removed before Drupal 9.0.0.)
       $context = new RenderContext();
       $output = $this->renderer
-        ->executeInRenderContext($context, function() use ($serializer, $data, $format, $serialization_context) {
+        ->executeInRenderContext($context, function () use ($serializer, $data, $format, $serialization_context) {
           return $serializer->serialize($data, $format, $serialization_context);
         });
       if ($response instanceof CacheableResponseInterface) {

Committed as 52a598e and pushed to 8.5.x.

Published change record

wim leers’s picture

This also unblocked #2864816, that's now back at RTBC: #2864816-46: HAL LinkManager doesn't add 'url.site' cache context when needed.

wim leers’s picture

Status: Fixed » Closed (fixed)

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