Skip to content

Commit 5e908c8

Browse files
fix: reorder early return QueryParameterValidateListener (#6300)
1 parent 50f4f0e commit 5e908c8

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

src/Symfony/EventListener/QueryParameterValidateListener.php

+16-16
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
use ApiPlatform\Doctrine\Odm\State\Options as ODMOptions;
1717
use ApiPlatform\Doctrine\Orm\State\Options;
18-
use ApiPlatform\Metadata\CollectionOperationInterface;
18+
use ApiPlatform\Metadata\HttpOperation;
1919
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
2020
use ApiPlatform\ParameterValidator\ParameterValidator;
2121
use ApiPlatform\State\ProviderInterface;
@@ -54,20 +54,6 @@ public function onKernelRequest(RequestEvent $event): void
5454
$request = $event->getRequest();
5555
$operation = $this->initializeOperation($request);
5656

57-
if ($operation && $this->provider instanceof ProviderInterface) {
58-
if (null === $operation->getQueryParameterValidationEnabled()) {
59-
$operation = $operation->withQueryParameterValidationEnabled($request->isMethodSafe() && 'GET' === $request->getMethod() && $operation instanceof CollectionOperationInterface);
60-
}
61-
62-
$this->provider->provide($operation, $request->attributes->get('_api_uri_variables') ?? [], [
63-
'request' => $request,
64-
'uri_variables' => $request->attributes->get('_api_uri_variables') ?? [],
65-
'resource_class' => $operation->getClass(),
66-
]);
67-
68-
return;
69-
}
70-
7157
if (
7258
!$request->isMethodSafe()
7359
|| !($attributes = RequestAttributesExtractor::extractAttributes($request))
@@ -81,7 +67,21 @@ public function onKernelRequest(RequestEvent $event): void
8167
return;
8268
}
8369

84-
if (!($operation?->getQueryParameterValidationEnabled() ?? true) || !$operation instanceof CollectionOperationInterface) {
70+
if (!($operation?->getQueryParameterValidationEnabled() ?? true) || !$operation instanceof HttpOperation) {
71+
return;
72+
}
73+
74+
if ($this->provider instanceof ProviderInterface) {
75+
if (null === $operation->getQueryParameterValidationEnabled()) {
76+
$operation = $operation->withQueryParameterValidationEnabled('GET' === $request->getMethod());
77+
}
78+
79+
$this->provider->provide($operation, $request->attributes->get('_api_uri_variables') ?? [], [
80+
'request' => $request,
81+
'uri_variables' => $request->attributes->get('_api_uri_variables') ?? [],
82+
'resource_class' => $operation->getClass(),
83+
]);
84+
8585
return;
8686
}
8787

src/Symfony/Tests/EventListener/QueryParameterValidateListenerTest.php

+26-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Prophecy\Prophecy\ObjectProphecy;
3030
use Symfony\Component\HttpFoundation\Request;
3131
use Symfony\Component\HttpKernel\Event\RequestEvent;
32+
use Symfony\Component\HttpKernel\HttpKernelInterface;
3233

3334
class QueryParameterValidateListenerTest extends TestCase
3435
{
@@ -185,6 +186,30 @@ public function testOnKernelRequestWithRequiredFilter(): void
185186
$this->testedInstance->onKernelRequest($eventProphecy->reveal());
186187
}
187188

189+
/**
190+
* if parameter use_symfony_listeners is true.
191+
*
192+
* @group legacy
193+
*/
194+
public function testDoNothingWhenListenersDisabled(): void
195+
{
196+
$parameterValidator = $this->prophesize(ProviderInterface::class);
197+
$parameterValidator->provide()->shouldNotBeCalled();
198+
199+
$factory = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
200+
$factory->create(Dummy::class)->willReturn(new ResourceMetadataCollection(Dummy::class, [new ApiResource(operations: ['get' => new Get(name: 'get')])]))->shouldBeCalled();
201+
202+
$listener = new QueryParameterValidateListener($parameterValidator->reveal(), $factory->reveal());
203+
204+
$event = new RequestEvent(
205+
$this->prophesize(HttpKernelInterface::class)->reveal(),
206+
new Request([], [], ['_api_resource_class' => Dummy::class, '_api_operation_name' => 'get', '_api_platform_disable_listeners' => true]),
207+
\defined(HttpKernelInterface::class.'::MAIN_REQUEST') ? HttpKernelInterface::MAIN_REQUEST : HttpKernelInterface::MASTER_REQUEST,
208+
);
209+
210+
$listener->onKernelRequest($event);
211+
}
212+
188213
private function setUpWithFilters(array $filters = []): void
189214
{
190215
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
@@ -207,7 +232,7 @@ public function testOnKernelRequest(): void
207232
$request = new Request(
208233
[],
209234
[],
210-
['_api_resource_class' => Dummy::class, '_api_operation' => new GetCollection()],
235+
['_api_resource_class' => Dummy::class, '_api_operation' => new GetCollection(), '_api_operation_name' => 'get'],
211236
[],
212237
[],
213238
['QUERY_STRING' => 'required=foo']

0 commit comments

Comments
 (0)