-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpKernel] Add the UidValueResolver argument value resolver #44665
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
Merged
Merged
Changes from all commits
Commits
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
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
48 changes: 48 additions & 0 deletions
48
...ny/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/UidController.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,48 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller; | ||
|
||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Routing\Annotation\Route; | ||
use Symfony\Component\Uid\Ulid; | ||
use Symfony\Component\Uid\UuidV1; | ||
|
||
class UidController | ||
{ | ||
#[Route(path: '/1/uuid-v1/{userId}')] | ||
public function anyFormat(UuidV1 $userId): Response | ||
{ | ||
return new Response($userId); | ||
} | ||
|
||
#[Route(path: '/2/ulid/{id}', requirements: ['id' => '[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{22}'])] | ||
public function specificFormatInAttribute(Ulid $id): Response | ||
{ | ||
return new Response($id); | ||
} | ||
|
||
#[Route(path: '/3/uuid-v1/{id<[0123456789ABCDEFGHJKMNPQRSTVWXYZabcdefghjkmnpqrstvwxyz]{26}>}')] | ||
public function specificFormatInPath(UuidV1 $id): Response | ||
{ | ||
return new Response($id); | ||
} | ||
|
||
#[Route(path: '/4/uuid-v1/{postId}/custom-uid/{commentId}')] | ||
public function manyUids(UuidV1 $postId, TestCommentIdentifier $commentId): Response | ||
{ | ||
return new Response($postId."\n".$commentId); | ||
} | ||
} | ||
|
||
class TestCommentIdentifier extends Ulid | ||
{ | ||
} |
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
90 changes: 90 additions & 0 deletions
90
src/Symfony/Bundle/FrameworkBundle/Tests/Functional/UidTest.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,90 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\UidController; | ||
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\UidValueResolver; | ||
use Symfony\Component\Uid\Ulid; | ||
use Symfony\Component\Uid\UuidV1; | ||
use Symfony\Component\Uid\UuidV4; | ||
use Symfony\Component\Uid\UuidV6; | ||
|
||
/** | ||
* @see UidController | ||
*/ | ||
class UidTest extends AbstractWebTestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
self::deleteTmpDir(); | ||
} | ||
|
||
public function testArgumentValueResolverDisabled() | ||
{ | ||
$this->expectException(\TypeError::class); | ||
$this->expectExceptionMessage('Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\UidController::anyFormat(): Argument #1 ($userId) must be of type Symfony\Component\Uid\UuidV1, string given'); | ||
|
||
$client = $this->createClient(['test_case' => 'Uid', 'root_config' => 'config_disabled.yml']); | ||
|
||
$client->request('GET', '/1/uuid-v1/'.new UuidV1()); | ||
} | ||
|
||
public function testArgumentValueResolverEnabled() | ||
{ | ||
if (!class_exists(UidValueResolver::class)) { | ||
$this->markTestSkipped('Needs symfony/http-kernel >= 6.1'); | ||
} | ||
|
||
$client = $this->createClient(['test_case' => 'Uid', 'root_config' => 'config_enabled.yml']); | ||
|
||
// Any format | ||
$client->request('GET', '/1/uuid-v1/'.$uuidV1 = new UuidV1()); | ||
$this->assertSame((string) $uuidV1, $client->getResponse()->getContent()); | ||
$client->request('GET', '/1/uuid-v1/'.$uuidV1->toBase58()); | ||
$this->assertSame((string) $uuidV1, $client->getResponse()->getContent()); | ||
$client->request('GET', '/1/uuid-v1/'.$uuidV1->toRfc4122()); | ||
$this->assertSame((string) $uuidV1, $client->getResponse()->getContent()); | ||
// Bad version | ||
$client->request('GET', '/1/uuid-v1/'.$uuidV4 = new UuidV4()); | ||
$this->assertSame(404, $client->getResponse()->getStatusCode()); | ||
|
||
// Only base58 format | ||
$client->request('GET', '/2/ulid/'.($ulid = new Ulid())->toBase58()); | ||
$this->assertSame((string) $ulid, $client->getResponse()->getContent()); | ||
$client->request('GET', '/2/ulid/'.$ulid); | ||
$this->assertSame(404, $client->getResponse()->getStatusCode()); | ||
$client->request('GET', '/2/ulid/'.$ulid->toRfc4122()); | ||
$this->assertSame(404, $client->getResponse()->getStatusCode()); | ||
|
||
// Only base32 format | ||
$client->request('GET', '/3/uuid-v1/'.$uuidV1->toBase32()); | ||
$this->assertSame((string) $uuidV1, $client->getResponse()->getContent()); | ||
$client->request('GET', '/3/uuid-v1/'.$uuidV1); | ||
$this->assertSame(404, $client->getResponse()->getStatusCode()); | ||
$client->request('GET', '/3/uuid-v1/'.$uuidV1->toBase58()); | ||
$this->assertSame(404, $client->getResponse()->getStatusCode()); | ||
// Bad version | ||
$client->request('GET', '/3/uuid-v1/'.(new UuidV6())->toBase32()); | ||
$this->assertSame(404, $client->getResponse()->getStatusCode()); | ||
|
||
// Any format for both | ||
$client->request('GET', '/4/uuid-v1/'.$uuidV1.'/custom-uid/'.$ulid->toRfc4122()); | ||
$this->assertSame($uuidV1."\n".$ulid, $client->getResponse()->getContent()); | ||
$client->request('GET', '/4/uuid-v1/'.$uuidV1->toBase58().'/custom-uid/'.$ulid->toBase58()); | ||
$this->assertSame($uuidV1."\n".$ulid, $client->getResponse()->getContent()); | ||
// Bad version | ||
$client->request('GET', '/4/uuid-v1/'.$uuidV4.'/custom-uid/'.$ulid); | ||
$this->assertSame(404, $client->getResponse()->getStatusCode()); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/Uid/bundles.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,18 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
use Symfony\Bundle\FrameworkBundle\FrameworkBundle; | ||
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle; | ||
|
||
return [ | ||
new FrameworkBundle(), | ||
new TestBundle(), | ||
]; |
2 changes: 2 additions & 0 deletions
2
src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/Uid/config_disabled.yml
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,2 @@ | ||
imports: | ||
- { resource: "../config/default.yml" } |
5 changes: 5 additions & 0 deletions
5
src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/Uid/config_enabled.yml
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,5 @@ | ||
imports: | ||
- { resource: "../config/default.yml" } | ||
|
||
framework: | ||
uid: ~ |
2 changes: 2 additions & 0 deletions
2
src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/Uid/routing.yml
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,2 @@ | ||
uid: | ||
resource: "@TestBundle/Resources/config/routing.yml" |
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
src/Symfony/Component/HttpKernel/Controller/ArgumentResolver/UidValueResolver.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 Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\Controller\ArgumentResolver; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface; | ||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
use Symfony\Component\Uid\AbstractUid; | ||
|
||
final class UidValueResolver implements ArgumentValueResolverInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports(Request $request, ArgumentMetadata $argument): bool | ||
{ | ||
return !$argument->isVariadic() | ||
&& \is_string($request->attributes->get($argument->getName())) | ||
&& null !== $argument->getType() | ||
&& is_subclass_of($argument->getType(), AbstractUid::class, true); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function resolve(Request $request, ArgumentMetadata $argument): iterable | ||
{ | ||
/** @var class-string<AbstractUid> $uidClass */ | ||
$uidClass = $argument->getType(); | ||
|
||
try { | ||
return [$uidClass::fromString($request->attributes->get($argument->getName()))]; | ||
} catch (\InvalidArgumentException $e) { | ||
throw new NotFoundHttpException(sprintf('The uid for the "%s" parameter is invalid.', $argument->getName()), $e); | ||
} | ||
} | ||
} |
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.
Check this file for different use cases.