Skip to content

Commit 9352351

Browse files
author
Grégoire Paris
committed
Make the requirements configurable
Closes #1401
1 parent e2e3e56 commit 9352351

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

src/Bridge/Symfony/Routing/ApiLoader.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function load($data, $type = null): RouteCollection
114114
'operationId' => $operationId,
115115
],
116116
],
117-
[],
117+
$operation['requirements'] ?? [],
118118
[],
119119
'',
120120
[],
@@ -186,7 +186,7 @@ private function addRoute(RouteCollection $routeCollection, string $resourceClas
186186
'_api_resource_class' => $resourceClass,
187187
sprintf('_api_%s_operation_name', $operationType) => $operationName,
188188
],
189-
[],
189+
$operation['requirements'] ?? [],
190190
[],
191191
'',
192192
[],

tests/Bridge/Symfony/Routing/ApiLoaderTest.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,21 @@ public function testApiLoader()
4747
$resourceMetadata = $resourceMetadata->withShortName('dummy');
4848
//default operation based on OperationResourceMetadataFactory
4949
$resourceMetadata = $resourceMetadata->withItemOperations([
50-
'get' => ['method' => 'GET'],
50+
'get' => ['method' => 'GET', 'requirements' => ['id' => '\d+']],
5151
'put' => ['method' => 'PUT'],
5252
'delete' => ['method' => 'DELETE'],
5353
]);
5454
//custom operations
5555
$resourceMetadata = $resourceMetadata->withCollectionOperations([
56-
'my_op' => ['method' => 'GET', 'controller' => 'some.service.name'], //with controller
56+
'my_op' => ['method' => 'GET', 'controller' => 'some.service.name', 'requirements' => ['_format' => 'a valid format']], //with controller
5757
'my_second_op' => ['method' => 'POST'], //without controller, takes the default one
5858
'my_path_op' => ['method' => 'GET', 'path' => 'some/custom/path'], //custom path
5959
]);
6060

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

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

@@ -76,7 +76,7 @@ public function testApiLoader()
7676
);
7777

7878
$this->assertEquals(
79-
$this->getRoute('/dummies.{_format}', 'some.service.name', DummyEntity::class, 'my_op', ['GET'], true),
79+
$this->getRoute('/dummies.{_format}', 'some.service.name', DummyEntity::class, 'my_op', ['GET'], true, ['_format' => 'a valid format']),
8080
$routeCollection->get('api_dummies_my_op_collection')
8181
);
8282

@@ -261,7 +261,7 @@ private function getApiLoaderWithResourceMetadata(ResourceMetadata $resourceMeta
261261
return $apiLoader;
262262
}
263263

264-
private function getRoute(string $path, string $controller, string $resourceClass, string $operationName, array $methods, bool $collection = false): Route
264+
private function getRoute(string $path, string $controller, string $resourceClass, string $operationName, array $methods, bool $collection = false, array $requirements = []): Route
265265
{
266266
return new Route(
267267
$path,
@@ -271,15 +271,15 @@ private function getRoute(string $path, string $controller, string $resourceClas
271271
'_api_resource_class' => $resourceClass,
272272
sprintf('_api_%s_operation_name', $collection ? 'collection' : 'item') => $operationName,
273273
],
274-
[],
274+
$requirements,
275275
[],
276276
'',
277277
[],
278278
$methods
279279
);
280280
}
281281

282-
private function getSubresourceRoute(string $path, string $controller, string $resourceClass, string $operationName, array $context): Route
282+
private function getSubresourceRoute(string $path, string $controller, string $resourceClass, string $operationName, array $context, array $requirements = []): Route
283283
{
284284
return new Route(
285285
$path,
@@ -290,7 +290,7 @@ private function getSubresourceRoute(string $path, string $controller, string $r
290290
'_api_subresource_operation_name' => $operationName,
291291
'_api_subresource_context' => $context,
292292
],
293-
[],
293+
$requirements,
294294
[],
295295
'',
296296
[],

0 commit comments

Comments
 (0)