-
-
Notifications
You must be signed in to change notification settings - Fork 975
feat(metadata): add parameter name to uriVariablesConverter context #8431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 4.4
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <dunglas@gmail.com> | ||
| * | ||
| * 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\Metadata\Tests; | ||
|
|
||
| use ApiPlatform\Metadata\ApiProperty; | ||
| use ApiPlatform\Metadata\Get; | ||
| use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface; | ||
| use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; | ||
| use ApiPlatform\Metadata\UriVariablesConverter; | ||
| use ApiPlatform\Metadata\UriVariableTransformerInterface; | ||
| use ApiPlatform\Tests\Fixtures\TestBundle\Document\Dummy; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Symfony\Component\TypeInfo\Type\BuiltinType; | ||
| use Symfony\Component\TypeInfo\TypeIdentifier; | ||
|
|
||
| class UriVariablesConverterTest extends TestCase | ||
| { | ||
| public function testParameterNameAddedToContext(): void | ||
| { | ||
| $transformer = $this->createMock(UriVariableTransformerInterface::class); | ||
|
|
||
| $contextDuringSupports = []; | ||
| $transformer | ||
| ->expects($this->once()) | ||
| ->method('supportsTransformation') | ||
| ->willReturnCallback(static function ($v, $t, $context) use (&$contextDuringSupports) { | ||
| $contextDuringSupports = $context; | ||
|
|
||
| return true; | ||
| }); | ||
|
|
||
| $contextduringTransform = []; | ||
| $transformer | ||
| ->expects($this->once()) | ||
| ->method('transform') | ||
| ->willReturnCallback(static function ($v, $t, $context) use (&$contextduringTransform) { | ||
| $contextduringTransform = $context; | ||
|
|
||
| return $v; | ||
| }); | ||
|
|
||
| $metadataFactory = $this->createStub(PropertyMetadataFactoryInterface::class); | ||
| $metadataFactory->method('create')->willReturn(new ApiProperty(nativeType: new BuiltinType(TypeIdentifier::STRING))); | ||
|
|
||
| $converter = new UriVariablesConverter( | ||
| $metadataFactory, | ||
| $this->createStub(ResourceMetadataCollectionFactoryInterface::class), | ||
| [$transformer], | ||
| ); | ||
|
|
||
| $context = ['operation' => new Get(), 'parameterName' => 'not-overwritten']; | ||
| $converter->convert(['foo' => 'bar'], Dummy::class, $context); | ||
|
|
||
| self::assertArrayHasKey('parameterName', $contextDuringSupports); | ||
| self::assertEquals($contextDuringSupports['parameterName'], 'foo'); | ||
|
|
||
| self::assertArrayHasKey('parameterName', $contextduringTransform); | ||
| self::assertEquals($contextDuringSupports['parameterName'], 'foo'); | ||
|
|
||
| self::assertEquals('not-overwritten', $context['parameterName']); | ||
| } | ||
| } |
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I initially wanted to add it as a fully qualified parameter to both the supports and transform functions, but that would be a backwards compatibility break. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,12 +60,14 @@ public function convert(array $uriVariables, string $class, array $context = []) | |
| } | ||
|
|
||
| foreach ($this->uriVariableTransformers as $uriVariableTransformer) { | ||
| if (!$uriVariableTransformer->supportsTransformation($value, $types, $context)) { | ||
| $paramContext = $context; | ||
| $paramContext['parameterName'] = $parameterName; | ||
|
Comment on lines
+63
to
+64
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just so we don't overwrite anything that might've been added by the user to the context. The context remains the same outside of this function. Downside? We have to copy the entire context array for every uri variable, so there might be a tiny performance overhead. Could also check outside of the foreach and keep whatever value was set out there, and reset it again after the foreach. |
||
| if (!$uriVariableTransformer->supportsTransformation($value, $types, $paramContext)) { | ||
| continue; | ||
| } | ||
|
|
||
| try { | ||
| $uriVariables[$parameterName] = $uriVariableTransformer->transform($value, $types, $context); | ||
| $uriVariables[$parameterName] = $uriVariableTransformer->transform($value, $types, $paramContext); | ||
| break; | ||
| } catch (InvalidUriVariableException $e) { | ||
| throw new InvalidUriVariableException(\sprintf('Identifier "%s" could not be transformed.', $parameterName), $e->getCode(), $e); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needed to change this to have a valid installable state,... without this all workflows and local
composer installresult in a non-resolvable set of dependencies.