Skip to content

Commit 03ee7fe

Browse files
fix: reorder early return QueryParameterValidateListener
1 parent 50f4f0e commit 03ee7fe

File tree

2 files changed

+86
-16
lines changed

2 files changed

+86
-16
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

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Symfony\EventListener;
15+
use ApiPlatform\Metadata\ApiResource;
16+
use ApiPlatform\Metadata\Get;
17+
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
18+
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
19+
use ApiPlatform\State\ProviderInterface;
20+
use ApiPlatform\Symfony\EventListener\QueryParameterValidateListener;
21+
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy;
22+
use PHPUnit\Framework\TestCase;
23+
use Prophecy\PhpUnit\ProphecyTrait;
24+
use Symfony\Component\HttpFoundation\Request;
25+
use Symfony\Component\HttpKernel\Event\RequestEvent;
26+
use Symfony\Component\HttpKernel\HttpKernelInterface;
27+
28+
class QueryParameterValidateListenerTest extends TestCase
29+
{
30+
use ProphecyTrait;
31+
32+
public function testOnKernelRequest(): void
33+
{
34+
$request = new Request([], [], ['_api_resource_class' => Dummy::class, '_api_operation_name' => 'get']);
35+
$parameterValidator = $this->prophesize(ProviderInterface::class);
36+
$parameterValidator->provide(new Get(name: 'get', queryParameterValidationEnabled: true), [], ['request' => $request, 'uri_variables' => [], 'resource_class' => null])->shouldBeCalled();
37+
38+
$factory = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
39+
$factory->create(Dummy::class)->willReturn(new ResourceMetadataCollection(Dummy::class, [new ApiResource(operations: ['get' => new Get(name: 'get')])]))->shouldBeCalled();
40+
41+
$listener = new QueryParameterValidateListener($parameterValidator->reveal(), $factory->reveal());
42+
43+
$event = new RequestEvent(
44+
$this->prophesize(HttpKernelInterface::class)->reveal(),
45+
$request,
46+
\defined(HttpKernelInterface::class.'::MAIN_REQUEST') ? HttpKernelInterface::MAIN_REQUEST : HttpKernelInterface::MASTER_REQUEST,
47+
);
48+
49+
$listener->onKernelRequest($event);
50+
}
51+
52+
public function testDoNothingWhenListenersDisabled(): void
53+
{
54+
$parameterValidator = $this->prophesize(ProviderInterface::class);
55+
$parameterValidator->provide()->shouldNotBeCalled();
56+
57+
$factory = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
58+
$factory->create(Dummy::class)->willReturn(new ResourceMetadataCollection(Dummy::class, [new ApiResource(operations: ['get' => new Get(name: 'get')])]))->shouldBeCalled();
59+
60+
$listener = new QueryParameterValidateListener($parameterValidator->reveal(), $factory->reveal());
61+
62+
$event = new RequestEvent(
63+
$this->prophesize(HttpKernelInterface::class)->reveal(),
64+
new Request([], [], ['_api_resource_class' => Dummy::class, '_api_operation_name' => 'get', '_api_platform_disable_listeners' => true]),
65+
\defined(HttpKernelInterface::class.'::MAIN_REQUEST') ? HttpKernelInterface::MAIN_REQUEST : HttpKernelInterface::MASTER_REQUEST,
66+
);
67+
68+
$listener->onKernelRequest($event);
69+
}
70+
}

0 commit comments

Comments
 (0)