Skip to content

Commit

Permalink
fix: add back support for custom controller with class method (#5681)
Browse files Browse the repository at this point in the history
* feat: allow defining controller as a class method

* chore: cover undefined controller case in test suite
  • Loading branch information
gquemener committed Jul 20, 2023
1 parent 0fe4f4d commit e21e9fa
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Symfony/Routing/ApiLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,11 @@ public function load(mixed $data, string $type = null): RouteCollection
$path = str_replace('{._format}', '.{_format}', $path);
}

if (($controller = $operation->getController()) && !$this->container->has($controller)) {
throw new RuntimeException(sprintf('There is no builtin action for the "%s" operation. You need to define the controller yourself.', $operationName));
if ($controller = $operation->getController()) {
$controllerId = explode('::', $controller)[0];
if (!$this->container->has($controllerId)) {
throw new RuntimeException(sprintf('Operation "%s" is defining an unknown service as controller "%s". Make sure it is properly registered in the dependency injection container.', $operationName, $controllerId));
}
}

$route = new Route(
Expand Down
32 changes: 32 additions & 0 deletions tests/Symfony/Routing/ApiLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function testApiLoader(): void
'api_dummies_my_path_op_collection' => (new GetCollection())->withUriTemplate('some/custom/path'),
// Custom path
'api_dummies_my_stateless_op_collection' => (new GetCollection())->withUriTemplate('/dummies.{_format}')->withStateless(true),
'api_dummies_my_controller_method_item' => (new Get())->withUriTemplate('/foo')->withController('Foo\\Bar\\MyController::method'),
])),
]);

Expand Down Expand Up @@ -180,6 +181,21 @@ public function testApiLoader(): void
),
$routeCollection->get('api_dummies_my_stateless_op_collection')
);

$this->assertEquals(
$this->getRoute(
'/foo',
'Foo\\Bar\\MyController::method',
null,
RelatedDummyEntity::class,
[],
'api_dummies_my_controller_method_item',
[],
['GET'],
[]
),
$routeCollection->get('api_dummies_my_controller_method_item')
);
}

public function testApiLoaderWithPrefix(): void
Expand Down Expand Up @@ -241,6 +257,20 @@ public function testApiLoaderWithPrefix(): void
);
}

public function testApiLoaderWithUndefinedControllerService(): void
{
$this->expectExceptionObject(new \RuntimeException('Operation "api_dummies_my_undefined_controller_method_item" is defining an unknown service as controller "Foo\\Bar\\MyUndefinedController". Make sure it is properly registered in the dependency injection container.'));

$resourceCollection = new ResourceMetadataCollection(Dummy::class, [
(new ApiResource())->withShortName('dummy')->withOperations(new Operations([
'api_dummies_my_undefined_controller_method_item' => (new Get())->withUriTemplate('/foo')->withController('Foo\\Bar\\MyUndefinedController::method'),
])),
]);

$routeCollection = $this->getApiLoaderWithResourceMetadataCollection($resourceCollection)->load(null);
$routeCollection->get('api_dummies_my_undefined_controller_method_item');
}

private function getApiLoaderWithResourceMetadataCollection(ResourceMetadataCollection $resourceCollection): ApiLoader
{
$routingConfig = __DIR__.'/../../../src/Symfony/Bundle/Resources/config/routing';
Expand All @@ -254,12 +284,14 @@ private function getApiLoaderWithResourceMetadataCollection(ResourceMetadataColl
'api_platform.action.get_item',
'api_platform.action.put_item',
'api_platform.action.delete_item',
'Foo\\Bar\\MyController',
];
$containerProphecy = $this->prophesize(ContainerInterface::class);

foreach ($possibleArguments as $possibleArgument) {
$containerProphecy->has($possibleArgument)->willReturn(true);
}
$containerProphecy->has('Foo\\Bar\\MyUndefinedController')->willReturn(false);

$containerProphecy->has(Argument::type('string'))->willReturn(false);

Expand Down

0 comments on commit e21e9fa

Please sign in to comment.