core/core.services.yml | 4 +-- core/lib/Drupal/Core/Routing/BcRoute.php | 36 +++++++++++++++++++++- .../RestResourceGetRouteProcessorBC.php | 25 ++------------- .../EntityResource/EntityResourceTestBase.php | 9 ++---- 4 files changed, 42 insertions(+), 32 deletions(-) diff --git a/core/core.services.yml b/core/core.services.yml index 9b64819..d848df3 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -938,8 +938,8 @@ services: # The HTTP method route filter must run very early: it removes any routes # whose requirements do not allow the HTTP method of the current request. # Throws a 405 if no routes match the current request's HTTP method. - # (If it runs before content_type_header_matcher, it can ensure that that - # only receives routes which can have a Content-Type request header.) + # (If it runs before content_type_header_matcher, it can ensure that only + # receives routes which can have a Content-Type request header.) - { name: route_filter, priority: 10 } content_type_header_matcher: class: Drupal\Core\Routing\ContentTypeHeaderMatcher diff --git a/core/lib/Drupal/Core/Routing/BcRoute.php b/core/lib/Drupal/Core/Routing/BcRoute.php index e886931..a0af044 100644 --- a/core/lib/Drupal/Core/Routing/BcRoute.php +++ b/core/lib/Drupal/Core/Routing/BcRoute.php @@ -14,7 +14,7 @@ * - have an accompanying outbound route processor, that overwrites this empty * route definition with the redirected route's definition. * - * F.e. see \Drupal\rest\RouteProcessor\RestResourceGetRouteProcessorBC. + * @see \Drupal\rest\RouteProcessor\RestResourceGetRouteProcessorBC */ class BcRoute extends Route { @@ -26,4 +26,38 @@ public function __construct() { $this->setOption('bc_route', TRUE); } + /** + * {@inheritdoc} + */ + public function serialize() { + // Ensure this route definition is as empty as possible when it gets stored. + return serialize([ + 'path' => '', + 'host' => '', + 'defaults' => [], + 'requirements' => [], + 'options' => [], + 'schemes' => [], + 'methods' => [], + ]); + } + + /** + * Inherit definition of target route, for use in outbound route processors. + * + * @param \Symfony\Component\Routing\Route $target_route + * The route that is the redirect target. + * + * @return $this + */ + public function inheritDefinitionFromTargetRoute(Route $target_route) { + $this->setPath($target_route->getPath()); + $this->setDefaults($target_route->getDefaults()); + $this->setRequirements($target_route->getRequirements()); + $this->setOptions($target_route->getOptions()); + $this->setHost($target_route->getHost()); + $this->setSchemes($target_route->getSchemes()); + $this->setMethods($target_route->getMethods()); + } + } diff --git a/core/modules/rest/src/RouteProcessor/RestResourceGetRouteProcessorBC.php b/core/modules/rest/src/RouteProcessor/RestResourceGetRouteProcessorBC.php index cf55326..41052e2 100644 --- a/core/modules/rest/src/RouteProcessor/RestResourceGetRouteProcessorBC.php +++ b/core/modules/rest/src/RouteProcessor/RestResourceGetRouteProcessorBC.php @@ -4,6 +4,7 @@ use Drupal\Core\Render\BubbleableMetadata; use Drupal\Core\RouteProcessor\OutboundRouteProcessorInterface; +use Drupal\Core\Routing\BcRoute; use Drupal\Core\Routing\RouteProviderInterface; use Symfony\Component\Routing\Route; @@ -49,32 +50,12 @@ public function processOutbound($route_name, Route $route, array &$parameters, B // route definitions that are as empty as possible, plus an outbound route // processor. // @see \Drupal\rest\Plugin\ResourceBase::routes() - if ($route_name_parts[0] === 'rest' && $route_name_parts[count($route_name_parts) - 2] === 'GET' && in_array($route_name_parts[count($route_name_parts) - 1], $this->serializerFormats, TRUE)) { + if ($route instanceof BcRoute && $route_name_parts[0] === 'rest' && $route_name_parts[count($route_name_parts) - 2] === 'GET' && in_array($route_name_parts[count($route_name_parts) - 1], $this->serializerFormats, TRUE)) { array_pop($route_name_parts); $redirected_route_name = implode('.', $route_name_parts); @trigger_error(sprintf("The '%s' route is deprecated since version 8.5.x and will be removed in 9.0.0. Use the '%s' route instead.", $route_name, $redirected_route_name), E_USER_DEPRECATED); - static::overwriteRoute($route, $this->routeProvider->getRouteByName($redirected_route_name)); + $route->inheritDefinitionFromTargetRoute($this->routeProvider->getRouteByName($redirected_route_name)); } } - /** - * Overwrites one route's metadata with the other's. - * - * @param \Symfony\Component\Routing\Route $target_route - * The route whose metadata to overwrite. - * @param \Symfony\Component\Routing\Route $source_route - * The route whose metadata to read from. - * - * @see \Symfony\Component\Routing\Route - */ - protected static function overwriteRoute(Route $target_route, Route $source_route) { - $target_route->setPath($source_route->getPath()); - $target_route->setDefaults($source_route->getDefaults()); - $target_route->setRequirements($source_route->getRequirements()); - $target_route->setOptions($source_route->getOptions()); - $target_route->setHost($source_route->getHost()); - $target_route->setSchemes($source_route->getSchemes()); - $target_route->setMethods($source_route->getMethods()); - } - } diff --git a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php index 48bfc12..a9164b6 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php @@ -659,13 +659,8 @@ public function testGet() { $other_format = static::$format === 'json' ? 'xml' : 'json'; $bc_route_other_format = Url::fromRoute('rest.entity.' . static::$entityTypeId . '.GET.' . $other_format, $url->getRouteParameters(), $url->getOptions()); $bc_route_other_format->setUrlGenerator($this->container->get('url_generator')); - try { - $bc_route_other_format->toString(TRUE); - $this->assertTrue(FALSE); - } - catch (RouteNotFoundException $e) { - $this->assertTrue(TRUE); - } + $this->setExpectedException(RouteNotFoundException::class); + $bc_route_other_format->toString(TRUE); } /**