8000 [HttpKernel] Catch `TypeError` if the wrong type is used in `BackedEnumValueResolver` by alexandre-daubois · Pull Request #52886 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpKernel] Catch TypeError if the wrong type is used in BackedEnumValueResolver #52886

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 u 8000 p 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
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
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function resolve(Request $request, ArgumentMetadata $argument): iterable

try {
return [$enumType::from($value)];
} catch (\ValueError $e) {
} catch (\ValueError|\TypeError $e) {
throw new NotFoundHttpException(sprintf('Could not resolve the "%s $%s" controller argument: ', $argument->getType(), $argument->getName()).$e->getMessage(), $e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\BackedEnumValueResolver;
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Tests\Fixtures\IntEnum;
use Symfony\Component\HttpKernel\Tests\Fixtures\Suit;

class BackedEnumValueResolverTest extends TestCase
Expand Down Expand Up @@ -134,6 +135,18 @@ public function testResolveThrowsOnUnexpectedType()
$resolver->resolve($request, $metadata);
}

public function testResolveThrowsOnTypeError()
{
$resolver = new BackedEnumValueResolver();
$request = self::createRequest(['suit' => 'value']);
$metadata = self::createArgumentMetadata('suit', IntEnum::class);

$this->expectException(NotFoundHttpException::class);
$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');

$resolver->resolve($request, $metadata);
}

private static function createRequest(array $attributes = []): Request
{
return new Request([], [], $attributes);
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/HttpKernel/Tests/Fixtures/IntEnum.php
6C8E
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.
*/

namespace Symfony\Component\HttpKernel\Tests\Fixtures;

enum IntEnum: int
{
case One = 1;
case Two = 2;
}
0