Skip to content

Commit db099a3

Browse files
bug symfony#52886 [HttpKernel] Catch TypeError if the wrong type is used in BackedEnumValueResolver (alexandre-daubois)
This PR was merged into the 6.3 branch. Discussion ---------- [HttpKernel] Catch `TypeError` if the wrong type is used in `BackedEnumValueResolver` | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix symfony#52053 | License | MIT Commits ------- 5b08257 [HttpKernel] Catch `TypeError` if the wrong type is used in `BackedEnumValueResolver`
2 parents 0b0484f + 5b08257 commit db099a3

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

src/Symfony/Component/HttpKernel/Controller/ArgumentResolver/BackedEnumValueResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function resolve(Request $request, ArgumentMetadata $argument): iterable
8686

8787
try {
8888
return [$enumType::from($value)];
89-
} catch (\ValueError $e) {
89+
} catch (\ValueError|\TypeError $e) {
9090
throw new NotFoundHttpException(sprintf('Could not resolve the "%s $%s" controller argument: ', $argument->getType(), $argument->getName()).$e->getMessage(), $e);
9191
}
9292
}

src/Symfony/Component/HttpKernel/Tests/Controller/ArgumentResolver/BackedEnumValueResolverTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\BackedEnumValueResolver;
1717
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
1818
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
19+
use Symfony\Component\HttpKernel\Tests\Fixtures\IntEnum;
1920
use Symfony\Component\HttpKernel\Tests\Fixtures\Suit;
2021

2122
class BackedEnumValueResolverTest extends TestCase
@@ -134,6 +135,18 @@ public function testResolveThrowsOnUnexpectedType()
134135
$resolver->resolve($request, $metadata);
135136
}
136137

138+
public function testResolveThrowsOnTypeError()
139+
{
140+
$resolver = new BackedEnumValueResolver();
141+
$request = self::createRequest(['suit' => 'value']);
142+
$metadata = self::createArgumentMetadata('suit', IntEnum::class);
143+
144+
$this->expectException(NotFoundHttpException::class);
145+
$this->expectExceptionMessage('Could not resolve the "Symfony\Component\HttpKernel\Tests\Fixtures\IntEnum $suit" controller argument: Symfony\Component\HttpKernel\Tests\Fixtures\IntEnum::from(): Argument #1 ($value) must be of type int, string given');
146+
147+
$resolver->resolve($request, $metadata);
148+
}
149+
137150
private static function createRequest(array $attributes = []): Request
138151
{
139152
return new Request([], [], $attributes);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\Tests\Fixtures;
13+
14+
enum IntEnum: int
15+
{
16+
case One = 1;
17+
case Two = 2;
18+
}

0 commit comments

Comments
 (0)