Commit 05422f2a authored by catch's avatar catch
Browse files

Issue #2931765 by timmillwood, Wim Leers: Regression:...

Issue #2931765 by timmillwood, Wim Leers: Regression: \Drupal\hal\LinkManager\LinkManagerBase implicitly depends on REST module
parent 2249c07e
Loading
Loading
Loading
Loading
+6 −6
Changes for core/modules/hal/src/LinkManager/LinkManagerBase.php: 6 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -2,7 +2,7 @@

namespace Drupal\hal\LinkManager;

use Drupal\rest\EventSubscriber\ResourceResponseSubscriber;
use Drupal\serialization\Normalizer\CacheableNormalizerInterface;

/**
 * Defines an abstract base-class for HAL link manager objects.
@@ -49,21 +49,21 @@ public function setLinkDomain($domain) {
   *
   * @see \Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize()
   * @see \Symfony\Component\Serializer\SerializerInterface::serialize()
   * @see \Drupal\rest\EventSubscriber\ResourceResponseSubscriber::SERIALIZATION_CONTEXT_CACHEABILITY
   * @see \Drupal\serialization\Normalizer\CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY
   */
  protected function getLinkDomain(array $context = []) {
    if (empty($this->linkDomain)) {
      if ($domain = $this->configFactory->get('hal.settings')->get('link_domain')) {
        // Bubble the appropriate cacheability metadata whenever possible.
        if (isset($context[ResourceResponseSubscriber::SERIALIZATION_CONTEXT_CACHEABILITY])) {
          $context[ResourceResponseSubscriber::SERIALIZATION_CONTEXT_CACHEABILITY]->addCacheableDependency($this->configFactory->get('hal.settings'));
        if (isset($context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY])) {
          $context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]->addCacheableDependency($this->configFactory->get('hal.settings'));
        }
        return rtrim($domain, '/');
      }
      else {
        // Bubble the relevant cacheability metadata whenever possible.
        if (isset($context[ResourceResponseSubscriber::SERIALIZATION_CONTEXT_CACHEABILITY])) {
          $context[ResourceResponseSubscriber::SERIALIZATION_CONTEXT_CACHEABILITY]->addCacheContexts(['url.site']);
        if (isset($context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY])) {
          $context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]->addCacheContexts(['url.site']);
        }
        $request = $this->requestStack->getCurrentRequest();
        return $request->getSchemeAndHttpHost() . $request->getBasePath();
+10 −10
Changes for core/modules/hal/tests/src/Kernel/HalLinkManagerTest.php: 10 added lines, 10 removed lines.
Original line number Diff line number Diff line
@@ -8,7 +8,7 @@
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\NodeType;
use Drupal\rest\EventSubscriber\ResourceResponseSubscriber;
use Drupal\serialization\Normalizer\CacheableNormalizerInterface;

/**
 * @coversDefaultClass \Drupal\hal\LinkManager\LinkManager
@@ -70,10 +70,10 @@ public function testGetTypeUri($link_domain, $entity_type, $bundle, array $conte

  public function providerTestGetTypeUri() {
    $serialization_context_collecting_cacheability = [
      ResourceResponseSubscriber::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata()
      CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata()
    ];
    $expected_serialization_context_cacheability_url_site = [
      ResourceResponseSubscriber::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheContexts(['url.site'])
      CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheContexts(['url.site'])
    ];

    $base_test_case = [
@@ -133,7 +133,7 @@ public function providerTestGetTypeUri() {
        'context' => $serialization_context_collecting_cacheability,
        'expected return' => 'http://llamas-rock.com/for-real/rest/type/node/page',
        'expected context' => [
          ResourceResponseSubscriber::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheTags(['config:hal.settings']),
          CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheTags(['config:hal.settings']),
        ],
      ],
    ];
@@ -163,10 +163,10 @@ public function testGetRelationUri($link_domain, $entity_type, $bundle, $field_n

  public function providerTestGetRelationUri() {
    $serialization_context_collecting_cacheability = [
      ResourceResponseSubscriber::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata()
      CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata()
    ];
    $expected_serialization_context_cacheability_url_site = [
      ResourceResponseSubscriber::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheContexts(['url.site'])
      CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheContexts(['url.site'])
    ];

    $field_name = $this->randomMachineName();
@@ -230,7 +230,7 @@ public function providerTestGetRelationUri() {
        'context' => $serialization_context_collecting_cacheability,
        'expected return' => 'http://llamas-rock.com/for-real/rest/relation/node/page/' . $field_name,
        'expected context' => [
          ResourceResponseSubscriber::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheTags(['config:hal.settings']),
          CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheTags(['config:hal.settings']),
        ],
      ],
    ];
@@ -258,7 +258,7 @@ public function testGetRelationInternalIds() {
   */
  public function testHalLinkManagersSetLinkDomain() {
    $serialization_context = [
      ResourceResponseSubscriber::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata()
      CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata()
    ];

    /* @var \Drupal\rest\LinkManager\LinkManager $link_manager */
@@ -266,10 +266,10 @@ public function testHalLinkManagersSetLinkDomain() {
    $link_manager->setLinkDomain('http://example.com/');
    $link = $link_manager->getTypeUri('node', 'page', $serialization_context);
    $this->assertEqual($link, 'http://example.com/rest/type/node/page');
    $this->assertEqual(new CacheableMetadata(), $serialization_context[ResourceResponseSubscriber::SERIALIZATION_CONTEXT_CACHEABILITY]);
    $this->assertEqual(new CacheableMetadata(), $serialization_context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]);
    $link = $link_manager->getRelationUri('node', 'page', 'field_ref', $serialization_context);
    $this->assertEqual($link, 'http://example.com/rest/relation/node/page/field_ref');
    $this->assertEqual(new CacheableMetadata(), $serialization_context[ResourceResponseSubscriber::SERIALIZATION_CONTEXT_CACHEABILITY]);
    $this->assertEqual(new CacheableMetadata(), $serialization_context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]);
  }

}
+3 −11
Changes for core/modules/rest/src/EventSubscriber/ResourceResponseSubscriber.php: 3 added lines, 11 removed lines.
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\rest\ResourceResponseInterface;
use Drupal\serialization\Normalizer\CacheableNormalizerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
@@ -21,15 +22,6 @@
 */
class ResourceResponseSubscriber implements EventSubscriberInterface {

  /**
   * Name of key for bubbling cacheability metadata via serialization context.
   *
   * @see \Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize()
   * @see \Symfony\Component\Serializer\SerializerInterface::serialize()
   * @see \Drupal\rest\EventSubscriber\ResourceResponseSubscriber::renderResponseBody()
   */
  const SERIALIZATION_CONTEXT_CACHEABILITY = 'cacheability';

  /**
   * The serializer.
   *
@@ -172,7 +164,7 @@ protected function renderResponseBody(Request $request, ResourceResponseInterfac
    if ($data !== NULL) {
      $serialization_context = [
        'request' => $request,
        static::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata(),
        CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata(),
      ];

      // @deprecated In Drupal 8.5.0, will be removed before Drupal 9.0.0. Use
@@ -188,7 +180,7 @@ protected function renderResponseBody(Request $request, ResourceResponseInterfac
          @trigger_error('Implicit cacheability metadata bubbling (onto the global render context) in normalizers is deprecated since Drupal 8.5.0 and will be removed in Drupal 9.0.0. Use the "cacheability" serialization context instead, for explicit cacheability metadata bubbling. See https://www.drupal.org/node/2918937', E_USER_DEPRECATED);
          $response->addCacheableDependency($context->pop());
        }
        $response->addCacheableDependency($serialization_context[static::SERIALIZATION_CONTEXT_CACHEABILITY]);
        $response->addCacheableDependency($serialization_context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]);
      }

      $response->setContent($output);
+23 −0
Changes for core/modules/serialization/src/Normalizer/CacheableNormalizerInterface.php: 23 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\serialization\Normalizer;

use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/**
 * Defines the interface for normalizers producing cacheable normalizations.
 *
 * @see cache
 */
interface CacheableNormalizerInterface extends NormalizerInterface {

  /**
   * Name of key for bubbling cacheability metadata via serialization context.
   *
   * @see \Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize()
   * @see \Symfony\Component\Serializer\SerializerInterface::serialize()
   * @see \Drupal\rest\EventSubscriber\ResourceResponseSubscriber::renderResponseBody()
   */
  const SERIALIZATION_CONTEXT_CACHEABILITY = 'cacheability';

}
+3 −5
Changes for core/modules/serialization/src/Normalizer/NormalizerBase.php: 3 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -3,14 +3,12 @@
namespace Drupal\serialization\Normalizer;

use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\rest\EventSubscriber\ResourceResponseSubscriber;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\SerializerAwareNormalizer;

/**
 * Base class for Normalizers.
 */
abstract class NormalizerBase extends SerializerAwareNormalizer implements NormalizerInterface {
abstract class NormalizerBase extends SerializerAwareNormalizer implements CacheableNormalizerInterface {

  /**
   * The interface or class that this Normalizer supports.
@@ -92,8 +90,8 @@ protected function checkFormat($format = NULL) {
   *   The data that might have cacheability information.
   */
  protected function addCacheableDependency(array $context, $data) {
    if ($data instanceof CacheableDependencyInterface && isset($context[ResourceResponseSubscriber::SERIALIZATION_CONTEXT_CACHEABILITY])) {
      $context[ResourceResponseSubscriber::SERIALIZATION_CONTEXT_CACHEABILITY]->addCacheableDependency($data);
    if ($data instanceof CacheableDependencyInterface && isset($context[static::SERIALIZATION_CONTEXT_CACHEABILITY])) {
      $context[static::SERIALIZATION_CONTEXT_CACHEABILITY]->addCacheableDependency($data);
    }
  }