Skip to content

Commit 0aa02fa

Browse files
committed
fix: multiple parameter provider #6673
1 parent e7fb04f commit 0aa02fa

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

src/State/Provider/ParameterProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
4949
$request->attributes->set('_api_header_parameters', $request->headers->all());
5050
}
5151

52-
$context = ['operation' => $operation] + $context;
5352
$parameters = $operation->getParameters();
5453
foreach ($parameters ?? [] as $parameter) {
5554
$extraProperties = $parameter->getExtraProperties();
5655
unset($extraProperties['_api_values']);
5756
$parameters->add($parameter->getKey(), $parameter = $parameter->withExtraProperties($extraProperties));
5857

58+
$context = ['operation' => $operation] + $context;
5959
$values = $this->getParameterValues($parameter, $request, $context);
6060
$value = $this->extractParameterValues($parameter, $values);
6161

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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\Fixtures\TestBundle\ApiResource\Issue6673;
15+
16+
use ApiPlatform\Metadata\GetCollection;
17+
use ApiPlatform\Metadata\Operation;
18+
use ApiPlatform\Metadata\Parameter;
19+
use ApiPlatform\Metadata\QueryParameter;
20+
21+
#[GetCollection(
22+
uriTemplate: 'issue6673',
23+
outputFormats: ['json'],
24+
parameters: [
25+
'a' => new QueryParameter(
26+
provider: [self::class, 'parameterOneProvider'],
27+
),
28+
'b' => new QueryParameter(
29+
provider: [self::class, 'parameterTwoProvider'],
30+
),
31+
],
32+
provider: [self::class, 'provide']
33+
)]
34+
final readonly class MutlipleParameterProvider
35+
{
36+
public function __construct(public string $id)
37+
{
38+
}
39+
40+
public static function provide(Operation $operation): ?array
41+
{
42+
return $operation->getNormalizationContext();
43+
}
44+
45+
public static function parameterOneProvider(Parameter $parameter, array $parameters = [], array $context = []): ?Operation
46+
{
47+
$operation = $context['operation'];
48+
$context = $operation->getNormalizationContext() ?? [];
49+
$context['a'] = $parameter->getValue();
50+
51+
return $operation->withNormalizationContext($context);
52+
}
53+
54+
public static function parameterTwoProvider(Parameter $parameter, array $parameters = [], array $context = []): ?Operation
55+
{
56+
$operation = $context['operation'];
57+
$context = $operation->getNormalizationContext() ?? [];
58+
$context['b'] = $parameter->getValue();
59+
60+
return $operation->withNormalizationContext($context);
61+
}
62+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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\Functional\Parameters;
15+
16+
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
17+
18+
final class ParameterProviderTest extends ApiTestCase
19+
{
20+
public function testMultipleParameterProviderShouldChangeTheOperation(): void
21+
{
22+
$response = self::createClient()->request('GET', 'issue6673?a=1&b=2', ['headers' => ['accept' => 'application/json']]);
23+
$this->assertArraySubset(['a' => '1', 'b' => '2'], $response->toArray());
24+
}
25+
}

0 commit comments

Comments
 (0)