-
-
Notifications
You must be signed in to change notification settings - Fork 978
feat(state): apply uri variable provider values #8491
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
Open
soyuka
wants to merge
2
commits into
api-platform:main
Choose a base branch
from
soyuka:feat/uri-variable-parameter-provider
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/State/ParameterProvider/PreservesUriVariableInterface.php
This file contains hidden or 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,31 @@ | ||
| <?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\State\ParameterProvider; | ||
|
|
||
| use ApiPlatform\Metadata\Parameter; | ||
|
|
||
| /** | ||
| * Implemented by a ParameterProviderInterface whose value is not always the uri variable it was computed | ||
| * from, typically because it resolves a resource for a security expression instead of an identifier. | ||
| * | ||
| * A provider that does not implement this interface is assumed to transform its parameter value, and the | ||
| * result becomes the uri variable the resource is queried with. | ||
| */ | ||
| interface PreservesUriVariableInterface | ||
| { | ||
| /** | ||
| * Whether the uri variable this parameter was computed from must be left untouched. | ||
| */ | ||
| public function preservesUriVariable(Parameter $parameter): bool; | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
47 changes: 47 additions & 0 deletions
47
tests/Fixtures/TestBundle/ApiResource/WriteUriVariableLinkResource.php
This file contains hidden or 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,47 @@ | ||
| <?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\Tests\Fixtures\TestBundle\ApiResource; | ||
|
|
||
| use ApiPlatform\Metadata\Get; | ||
| use ApiPlatform\Metadata\Link; | ||
| use ApiPlatform\Metadata\Operation; | ||
| use ApiPlatform\State\ParameterProvider\ReadLinkParameterProvider; | ||
| use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy; | ||
|
|
||
| #[Get( | ||
| uriTemplate: '/write_uri_variable_link_resources/{id}', | ||
| uriVariables: [ | ||
| 'id' => new Link( | ||
| provider: ReadLinkParameterProvider::class, | ||
| fromClass: Dummy::class, | ||
| extraProperties: ['write_uri_variable' => true], | ||
| ), | ||
| ], | ||
| provider: [self::class, 'provide'] | ||
| )] | ||
| class WriteUriVariableLinkResource | ||
| { | ||
| public string $id; | ||
| public string $dummyName; | ||
|
|
||
| public static function provide(Operation $operation, array $uriVariables = []) | ||
| { | ||
| $r = new self(); | ||
| $r->id = '1'; | ||
| // Fails with a TypeError unless the resolved Dummy replaced the identifier. | ||
| $r->dummyName = $uriVariables['id']->getName(); | ||
|
|
||
| return $r; | ||
| } | ||
| } |
52 changes: 52 additions & 0 deletions
52
tests/Fixtures/TestBundle/Entity/Base64UriVariableDummy.php
This file contains hidden or 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,52 @@ | ||
| <?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\Tests\Fixtures\TestBundle\Entity; | ||
|
|
||
| use ApiPlatform\Metadata\Get; | ||
| use ApiPlatform\Metadata\Link; | ||
| use ApiPlatform\Metadata\Parameter; | ||
| use Doctrine\ORM\Mapping as ORM; | ||
|
|
||
| #[ORM\Entity] | ||
| // The plain Get must come first so IRI generation resolves to {id}, not the encodedName template. | ||
| #[Get] | ||
| #[Get( | ||
| uriTemplate: '/base64_uri_variable_dummies/encoded/{encodedName}', | ||
| uriVariables: [ | ||
| 'encodedName' => new Link( | ||
| fromClass: self::class, | ||
| identifiers: ['name'], | ||
| provider: [self::class, 'decodeName'], | ||
| ), | ||
| ], | ||
| )] | ||
| class Base64UriVariableDummy | ||
| { | ||
| #[ORM\Id] | ||
| #[ORM\GeneratedValue] | ||
| #[ORM\Column(type: 'integer')] | ||
| public ?int $id = null; | ||
|
|
||
| #[ORM\Column] | ||
| public string $name; | ||
|
|
||
| /** | ||
| * @param array<string, mixed> $parameters | ||
| * @param array<string, mixed> $context | ||
| */ | ||
| public static function decodeName(Parameter $parameter, array $parameters = [], array $context = []): void | ||
| { | ||
| $parameter->setValue(base64_decode((string) $parameter->getValue(), true)); | ||
| } | ||
| } |
81 changes: 81 additions & 0 deletions
81
tests/Functional/Parameters/UriVariableParameterProviderTest.php
This file contains hidden or 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,81 @@ | ||
| <?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\Tests\Functional\Parameters; | ||
|
|
||
| use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; | ||
| use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\WriteUriVariableLinkResource; | ||
| use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Base64UriVariableDummy; | ||
| use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy; | ||
| use ApiPlatform\Tests\RecreateSchemaTrait; | ||
| use ApiPlatform\Tests\SetupClassResourcesTrait; | ||
|
|
||
| final class UriVariableParameterProviderTest extends ApiTestCase | ||
| { | ||
| use RecreateSchemaTrait; | ||
| use SetupClassResourcesTrait; | ||
|
|
||
| protected static ?bool $alwaysBootKernel = false; | ||
|
|
||
| /** | ||
| * @return class-string[] | ||
| */ | ||
| public static function getResources(): array | ||
| { | ||
| return [Base64UriVariableDummy::class, WriteUriVariableLinkResource::class, Dummy::class]; | ||
| } | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| $this->recreateSchema([Base64UriVariableDummy::class, Dummy::class]); | ||
| } | ||
|
|
||
| /** | ||
| * @see https://github.com/api-platform/core/pull/8431 | ||
| */ | ||
| public function testLinkParameterProviderDecodesUriVariableBeforeQuery(): void | ||
| { | ||
| $container = static::getContainer(); | ||
| if ('mongodb' === $container->getParameter('kernel.environment')) { | ||
| $this->markTestSkipped(); | ||
| } | ||
|
|
||
| $manager = $this->getManager(); | ||
| $dummy = new Base64UriVariableDummy(); | ||
| $dummy->name = 'Blip'; | ||
| $manager->persist($dummy); | ||
| $manager->flush(); | ||
|
|
||
| $response = self::createClient()->request('GET', '/base64_uri_variable_dummies/encoded/'.base64_encode('Blip')); | ||
| self::assertResponseStatusCodeSame(200); | ||
| self::assertEquals('Blip', $response->toArray()['name']); | ||
| } | ||
|
|
||
| public function testReadLinkParameterProviderWritesResolvedResourceWhenOptedIn(): void | ||
| { | ||
| $container = static::getContainer(); | ||
| if ('mongodb' === $container->getParameter('kernel.environment')) { | ||
| $this->markTestSkipped(); | ||
| } | ||
|
|
||
| $manager = $this->getManager(); | ||
| $dummy = new Dummy(); | ||
| $dummy->setName('hi'); | ||
| $manager->persist($dummy); | ||
| $manager->flush(); | ||
|
|
||
| $response = self::createClient()->request('GET', '/write_uri_variable_link_resources/'.$dummy->getId()); | ||
| self::assertResponseStatusCodeSame(200); | ||
| self::assertEquals('hi', $response->toArray()['dummyName']); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
we need to keep this for BC layer and deprecate adding this argument