Skip to content

Commit ad5efa5

Browse files
authored
fix: multiple parameter provider #6673 (#6732)
1 parent e7fb04f commit ad5efa5

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-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: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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_multiple_parameter_provider',
23+
shortName: 'multiple_parameter_provider',
24+
outputFormats: ['json'],
25+
parameters: [
26+
'a' => new QueryParameter(
27+
provider: [self::class, 'parameterOneProvider'],
28+
),
29+
'b' => new QueryParameter(
30+
provider: [self::class, 'parameterTwoProvider'],
31+
),
32+
],
33+
provider: [self::class, 'provide']
34+
)]
35+
final class MutlipleParameterProvider
36+
{
37+
public function __construct(public readonly string $id)
38+
{
39+
}
40+
41+
public static function provide(Operation $operation): ?array
42+
{
43+
return $operation->getNormalizationContext();
44+
}
45+
46+
public static function parameterOneProvider(Parameter $parameter, array $parameters = [], array $context = []): ?Operation
47+
{
48+
$operation = $context['operation'];
49+
$context = $operation->getNormalizationContext() ?? [];
50+
$context['a'] = $parameter->getValue();
51+
52+
return $operation->withNormalizationContext($context);
53+
}
54+
55+
public static function parameterTwoProvider(Parameter $parameter, array $parameters = [], array $context = []): ?Operation
56+
{
57+
$operation = $context['operation'];
58+
$context = $operation->getNormalizationContext() ?? [];
59+
$context['b'] = $parameter->getValue();
60+
61+
return $operation->withNormalizationContext($context);
62+
}
63+
}
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_multiple_parameter_provider?a=1&b=2', ['headers' => ['accept' => 'application/json']]);
23+
$this->assertArraySubset(['a' => '1', 'b' => '2'], $response->toArray());
24+
}
25+
}

0 commit comments

Comments
 (0)