Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",

Copy link
Copy Markdown
Contributor Author

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 install result in a non-resolvable set of dependencies.

"orchestra/testbench": "^10.9 || ^11.0",
"phpspec/prophecy-phpunit": "^2.2",
"phpstan/extension-installer": "^1.1",
Expand Down
73 changes: 73 additions & 0 deletions src/Metadata/Tests/UriVariablesConverterTest.php
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']);
}
}
6 changes: 4 additions & 2 deletions src/Metadata/UriVariablesConverter.php

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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
Expand Up @@ -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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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);
Expand Down
Loading