diff --git a/CHANGELOG.md b/CHANGELOG.md index ef9fddde27b..03fa16838b3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ * Deprecate `allow_plain_identifiers` option (#4167) * Exception: Add the ability to customize multiple status codes based on the validation exception (#4017) * GraphQL: Fix graphql fetching with Elasticsearch (#4217) +* ApiLoader: Support `_format` resolving (#4292) ## 2.6.5 diff --git a/src/Bridge/Symfony/Routing/ApiLoader.php b/src/Bridge/Symfony/Routing/ApiLoader.php index d7390fd7bd8..9e03264ce9d 100644 --- a/src/Bridge/Symfony/Routing/ApiLoader.php +++ b/src/Bridge/Symfony/Routing/ApiLoader.php @@ -128,7 +128,7 @@ public function load($data, $type = null): RouteCollection $operation['path'], [ '_controller' => $controller, - '_format' => null, + '_format' => $operation['defaults']['_format'] ?? null, '_stateless' => $operation['stateless'] ?? $resourceMetadata->getAttribute('stateless'), '_api_resource_class' => $operation['resource_class'], '_api_identifiers' => $operation['identifiers'], @@ -241,7 +241,7 @@ private function addRoute(RouteCollection $routeCollection, string $resourceClas $path, [ '_controller' => $controller, - '_format' => null, + '_format' => $operation['defaults']['_format'] ?? null, '_stateless' => $operation['stateless'], '_api_resource_class' => $resourceClass, '_api_identifiers' => $operation['identifiers'], diff --git a/tests/Bridge/Symfony/Routing/ApiLoaderTest.php b/tests/Bridge/Symfony/Routing/ApiLoaderTest.php index aae3a3b66eb..686763b32e6 100644 --- a/tests/Bridge/Symfony/Routing/ApiLoaderTest.php +++ b/tests/Bridge/Symfony/Routing/ApiLoaderTest.php @@ -60,7 +60,7 @@ public function testApiLoader() ]); //custom operations $resourceMetadata = $resourceMetadata->withCollectionOperations([ - 'my_op' => ['method' => 'GET', 'controller' => 'some.service.name', 'requirements' => ['_format' => 'a valid format'], 'defaults' => ['my_default' => 'default_value'], 'condition' => "request.headers.get('User-Agent') matches '/firefox/i'", 'stateless' => null], //with controller + 'my_op' => ['method' => 'GET', 'controller' => 'some.service.name', 'requirements' => ['_format' => 'a valid format'], 'defaults' => ['my_default' => 'default_value', '_format' => 'a valid format'], 'condition' => "request.headers.get('User-Agent') matches '/firefox/i'", 'stateless' => null], //with controller 'my_second_op' => ['method' => 'POST', 'options' => ['option' => 'option_value'], 'host' => '{subdomain}.api-platform.com', 'schemes' => ['https'], 'stateless' => null], //without controller, takes the default one 'my_path_op' => ['method' => 'GET', 'path' => 'some/custom/path', 'stateless' => null], //custom path 'my_stateless_op' => ['method' => 'GET', 'stateless' => true], @@ -87,7 +87,7 @@ public function testApiLoader() ); $this->assertEquals( - $this->getRoute('/dummies.{_format}', 'some.service.name', DummyEntity::class, 'my_op', ['GET'], true, ['_format' => 'a valid format'], ['my_default' => 'default_value', '_stateless' => null], [], '', [], "request.headers.get('User-Agent') matches '/firefox/i'"), + $this->getRoute('/dummies.{_format}', 'some.service.name', DummyEntity::class, 'my_op', ['GET'], true, ['_format' => 'a valid format'], ['my_default' => 'default_value', '_format' => 'a valid format', '_stateless' => null], [], '', [], "request.headers.get('User-Agent') matches '/firefox/i'"), $routeCollection->get('api_dummies_my_op_collection') ); @@ -318,7 +318,7 @@ private function getRoute(string $path, string $controller, string $resourceClas $path, [ '_controller' => $controller, - '_format' => null, + '_format' => $extraDefaults['_format'] ?? null, '_api_resource_class' => $resourceClass, '_api_identifiers' => ['id'], '_api_has_composite_identifier' => false, @@ -339,7 +339,7 @@ private function getSubresourceRoute(string $path, string $controller, string $r $path, [ '_controller' => $controller, - '_format' => null, + '_format' => $extraDefaults['_format'] ?? null, '_api_resource_class' => $resourceClass, '_api_subresource_operation_name' => $operationName, '_api_subresource_context' => $context,