diff --git a/src/Symfony/EventListener/QueryParameterValidateListener.php b/src/Symfony/EventListener/QueryParameterValidateListener.php index 6acf840209f..bfde23b726c 100644 --- a/src/Symfony/EventListener/QueryParameterValidateListener.php +++ b/src/Symfony/EventListener/QueryParameterValidateListener.php @@ -15,7 +15,7 @@ use ApiPlatform\Doctrine\Odm\State\Options as ODMOptions; use ApiPlatform\Doctrine\Orm\State\Options; -use ApiPlatform\Metadata\CollectionOperationInterface; +use ApiPlatform\Metadata\HttpOperation; use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; use ApiPlatform\ParameterValidator\ParameterValidator; use ApiPlatform\State\ProviderInterface; @@ -54,20 +54,6 @@ public function onKernelRequest(RequestEvent $event): void $request = $event->getRequest(); $operation = $this->initializeOperation($request); - if ($operation && $this->provider instanceof ProviderInterface) { - if (null === $operation->getQueryParameterValidationEnabled()) { - $operation = $operation->withQueryParameterValidationEnabled($request->isMethodSafe() && 'GET' === $request->getMethod() && $operation instanceof CollectionOperationInterface); - } - - $this->provider->provide($operation, $request->attributes->get('_api_uri_variables') ?? [], [ - 'request' => $request, - 'uri_variables' => $request->attributes->get('_api_uri_variables') ?? [], - 'resource_class' => $operation->getClass(), - ]); - - return; - } - if ( !$request->isMethodSafe() || !($attributes = RequestAttributesExtractor::extractAttributes($request)) @@ -81,7 +67,21 @@ public function onKernelRequest(RequestEvent $event): void return; } - if (!($operation?->getQueryParameterValidationEnabled() ?? true) || !$operation instanceof CollectionOperationInterface) { + if (!($operation?->getQueryParameterValidationEnabled() ?? true) || !$operation instanceof HttpOperation) { + return; + } + + if ($this->provider instanceof ProviderInterface) { + if (null === $operation->getQueryParameterValidationEnabled()) { + $operation = $operation->withQueryParameterValidationEnabled('GET' === $request->getMethod()); + } + + $this->provider->provide($operation, $request->attributes->get('_api_uri_variables') ?? [], [ + 'request' => $request, + 'uri_variables' => $request->attributes->get('_api_uri_variables') ?? [], + 'resource_class' => $operation->getClass(), + ]); + return; } diff --git a/src/Symfony/Tests/EventListener/QueryParameterValidateListenerTest.php b/src/Symfony/Tests/EventListener/QueryParameterValidateListenerTest.php index 17d4731e4ee..5041ac6e61f 100644 --- a/src/Symfony/Tests/EventListener/QueryParameterValidateListenerTest.php +++ b/src/Symfony/Tests/EventListener/QueryParameterValidateListenerTest.php @@ -29,6 +29,7 @@ use Prophecy\Prophecy\ObjectProphecy; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Event\RequestEvent; +use Symfony\Component\HttpKernel\HttpKernelInterface; class QueryParameterValidateListenerTest extends TestCase { @@ -185,6 +186,30 @@ public function testOnKernelRequestWithRequiredFilter(): void $this->testedInstance->onKernelRequest($eventProphecy->reveal()); } + /** + * if parameter use_symfony_listeners is true. + * + * @group legacy + */ + public function testDoNothingWhenListenersDisabled(): void + { + $parameterValidator = $this->prophesize(ProviderInterface::class); + $parameterValidator->provide()->shouldNotBeCalled(); + + $factory = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class); + $factory->create(Dummy::class)->willReturn(new ResourceMetadataCollection(Dummy::class, [new ApiResource(operations: ['get' => new Get(name: 'get')])]))->shouldBeCalled(); + + $listener = new QueryParameterValidateListener($parameterValidator->reveal(), $factory->reveal()); + + $event = new RequestEvent( + $this->prophesize(HttpKernelInterface::class)->reveal(), + new Request([], [], ['_api_resource_class' => Dummy::class, '_api_operation_name' => 'get', '_api_platform_disable_listeners' => true]), + \defined(HttpKernelInterface::class.'::MAIN_REQUEST') ? HttpKernelInterface::MAIN_REQUEST : HttpKernelInterface::MASTER_REQUEST, + ); + + $listener->onKernelRequest($event); + } + private function setUpWithFilters(array $filters = []): void { $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class); @@ -207,7 +232,7 @@ public function testOnKernelRequest(): void $request = new Request( [], [], - ['_api_resource_class' => Dummy::class, '_api_operation' => new GetCollection()], + ['_api_resource_class' => Dummy::class, '_api_operation' => new GetCollection(), '_api_operation_name' => 'get'], [], [], ['QUERY_STRING' => 'required=foo']