Skip to content

Add missing Symfony Routing options to operations configuration #1472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/Bridge/Symfony/Routing/ApiLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,13 @@ private function addRoute(RouteCollection $routeCollection, string $resourceClas
'_format' => null,
'_api_resource_class' => $resourceClass,
sprintf('_api_%s_operation_name', $operationType) => $operationName,
],
] + ($operation['defaults'] ?? []),
$operation['requirements'] ?? [],
[],
'',
[],
[$operation['method']]
$operation['options'] ?? [],
$operation['host'] ?? '',
$operation['schemes'] ?? [],
[$operation['method']],
$operation['condition'] ?? ''
);

$routeCollection->add(RouteNameGenerator::generate($operationName, $resourceShortName, $operationType), $route);
Expand Down
25 changes: 13 additions & 12 deletions tests/Bridge/Symfony/Routing/ApiLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,21 @@ public function testApiLoader()
$resourceMetadata = $resourceMetadata->withShortName('dummy');
//default operation based on OperationResourceMetadataFactory
$resourceMetadata = $resourceMetadata->withItemOperations([
'get' => ['method' => 'GET', 'requirements' => ['id' => '\d+']],
'get' => ['method' => 'GET', 'requirements' => ['id' => '\d+'], 'defaults' => ['my_default' => 'default_value', '_controller' => 'should_not_be_overriden']],
'put' => ['method' => 'PUT'],
'delete' => ['method' => 'DELETE'],
]);
//custom operations
$resourceMetadata = $resourceMetadata->withCollectionOperations([
'my_op' => ['method' => 'GET', 'controller' => 'some.service.name', 'requirements' => ['_format' => 'a valid format']], //with controller
'my_second_op' => ['method' => 'POST'], //without controller, takes the default one
'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'"], //with controller
'my_second_op' => ['method' => 'POST', 'options' => ['option' => 'option_value'], 'host' => '{subdomain}.api-platform.com', 'schemes' => ['https']], //without controller, takes the default one
'my_path_op' => ['method' => 'GET', 'path' => 'some/custom/path'], //custom path
]);

$routeCollection = $this->getApiLoaderWithResourceMetadata($resourceMetadata)->load(null);

$this->assertEquals(
$this->getRoute('/dummies/{id}.{_format}', 'api_platform.action.get_item', DummyEntity::class, 'get', ['GET'], false, ['id' => '\d+']),
$this->getRoute('/dummies/{id}.{_format}', 'api_platform.action.get_item', DummyEntity::class, 'get', ['GET'], false, ['id' => '\d+'], ['my_default' => 'default_value']),
$routeCollection->get('api_dummies_get_item')
);

Expand All @@ -77,12 +77,12 @@ public function testApiLoader()
);

$this->assertEquals(
$this->getRoute('/dummies.{_format}', 'some.service.name', DummyEntity::class, 'my_op', ['GET'], true, ['_format' => 'a valid format']),
$this->getRoute('/dummies.{_format}', 'some.service.name', DummyEntity::class, 'my_op', ['GET'], true, ['_format' => 'a valid format'], ['my_default' => 'default_value'], [], '', [], "request.headers.get('User-Agent') matches '/firefox/i'"),
$routeCollection->get('api_dummies_my_op_collection')
);

$this->assertEquals(
$this->getRoute('/dummies.{_format}', 'api_platform.action.post_collection', DummyEntity::class, 'my_second_op', ['POST'], true),
$this->getRoute('/dummies.{_format}', 'api_platform.action.post_collection', DummyEntity::class, 'my_second_op', ['POST'], true, [], [], ['option' => 'option_value'], '{subdomain}.api-platform.com', ['https']),
$routeCollection->get('api_dummies_my_second_op_collection')
);

Expand Down Expand Up @@ -262,7 +262,7 @@ private function getApiLoaderWithResourceMetadata(ResourceMetadata $resourceMeta
return $apiLoader;
}

private function getRoute(string $path, string $controller, string $resourceClass, string $operationName, array $methods, bool $collection = false, array $requirements = []): Route
private function getRoute(string $path, string $controller, string $resourceClass, string $operationName, array $methods, bool $collection = false, array $requirements = [], array $extraDefaults = [], array $options = [], string $host = '', array $schemes = [], string $condition = ''): Route
{
return new Route(
$path,
Expand All @@ -271,12 +271,13 @@ private function getRoute(string $path, string $controller, string $resourceClas
'_format' => null,
'_api_resource_class' => $resourceClass,
sprintf('_api_%s_operation_name', $collection ? 'collection' : 'item') => $operationName,
],
] + $extraDefaults,
$requirements,
[],
'',
[],
$methods
$options,
$host,
$schemes,
$methods,
$condition
);
}

Expand Down