-
-
Notifications
You must be signed in to change notification settings - Fork 874
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: multiple parameter provider #6673
- Loading branch information
Showing
3 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
tests/Fixtures/TestBundle/ApiResource/Issue6673/MutlipleParameterProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?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', | ||
outputFormats: ['json'], | ||
parameters: [ | ||
'a' => new QueryParameter( | ||
provider: [self::class, 'parameterOneProvider'], | ||
), | ||
'b' => new QueryParameter( | ||
provider: [self::class, 'parameterTwoProvider'], | ||
), | ||
], | ||
provider: [self::class, 'provide'] | ||
)] | ||
final readonly class MutlipleParameterProvider | ||
{ | ||
public function __construct(public 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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?a=1&b=2', ['headers' => ['accept' => 'application/json']]); | ||
$this->assertArraySubset(['a' => '1', 'b' => '2'], $response->toArray()); | ||
} | ||
} |