Skip to content

Commit

Permalink
fix: reorder early return QueryParameterValidateListener (#6300)
Browse files Browse the repository at this point in the history
  • Loading branch information
louismariegaborit committed Apr 9, 2024
1 parent 50f4f0e commit 5e908c8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
32 changes: 16 additions & 16 deletions src/Symfony/EventListener/QueryParameterValidateListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
Expand All @@ -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']
Expand Down

0 comments on commit 5e908c8

Please sign in to comment.