diff --git a/composer.json b/composer.json index c7c4272b39..47a8479718 100644 --- a/composer.json +++ b/composer.json @@ -142,7 +142,7 @@ "jangregor/phpstan-prophecy": "^2.1.11", "justinrainbow/json-schema": "^6.5.2", "laravel/framework": "^11.0 || ^12.0 || ^13.0", - "mcp/sdk": "^0.6", + "mcp/sdk": "^0.7", "orchestra/testbench": "^10.9 || ^11.0", "phpspec/prophecy-phpunit": "^2.2", "phpstan/extension-installer": "^1.1", diff --git a/src/Metadata/Tests/UriVariablesConverterTest.php b/src/Metadata/Tests/UriVariablesConverterTest.php new file mode 100644 index 0000000000..3a3e19b739 --- /dev/null +++ b/src/Metadata/Tests/UriVariablesConverterTest.php @@ -0,0 +1,73 @@ + + * + * 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']); + } +} diff --git a/src/Metadata/UriVariablesConverter.php b/src/Metadata/UriVariablesConverter.php index c33a00a03a..fb274f81f4 100644 --- a/src/Metadata/UriVariablesConverter.php +++ b/src/Metadata/UriVariablesConverter.php @@ -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; + 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);