Skip to content

Commit

Permalink
fix: multiple parameter provider #6673 (#6732)
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka authored Oct 17, 2024
1 parent e7fb04f commit ad5efa5
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/State/Provider/ParameterProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
$request->attributes->set('_api_header_parameters', $request->headers->all());
}

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

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6673;

use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Parameter;
use ApiPlatform\Metadata\QueryParameter;

#[GetCollection(
uriTemplate: 'issue6673_multiple_parameter_provider',
shortName: 'multiple_parameter_provider',
outputFormats: ['json'],
parameters: [
'a' => new QueryParameter(
provider: [self::class, 'parameterOneProvider'],
),
'b' => new QueryParameter(
provider: [self::class, 'parameterTwoProvider'],
),
],
provider: [self::class, 'provide']
)]
final class MutlipleParameterProvider
{
public function __construct(public readonly string $id)
{
}

public static function provide(Operation $operation): ?array
{
return $operation->getNormalizationContext();
}

public static function parameterOneProvider(Parameter $parameter, array $parameters = [], array $context = []): ?Operation
{
$operation = $context['operation'];
$context = $operation->getNormalizationContext() ?? [];
$context['a'] = $parameter->getValue();

return $operation->withNormalizationContext($context);
}

public static function parameterTwoProvider(Parameter $parameter, array $parameters = [], array $context = []): ?Operation
{
$operation = $context['operation'];
$context = $operation->getNormalizationContext() ?? [];
$context['b'] = $parameter->getValue();

return $operation->withNormalizationContext($context);
}
}
25 changes: 25 additions & 0 deletions tests/Functional/Parameters/ParameterProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Functional\Parameters;

use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;

final class ParameterProviderTest extends ApiTestCase
{
public function testMultipleParameterProviderShouldChangeTheOperation(): void
{
$response = self::createClient()->request('GET', 'issue6673_multiple_parameter_provider?a=1&b=2', ['headers' => ['accept' => 'application/json']]);
$this->assertArraySubset(['a' => '1', 'b' => '2'], $response->toArray());
}
}

0 comments on commit ad5efa5

Please sign in to comment.