Closed
Description
Symfony version(s) affected
6.3.5
Description
If I pass a string as the value for an \IntBackedEnum
controller argument with MapQueryParameter
attribute and BackedEnumValueResolver
resolver, symfony generates a \TypeError
exception.
How to reproduce
Controller:
#[Route('/category/{category}', name: 'list_category', methods: ['GET'])]
public function listAction(
#[MapQueryParameter(resolver: BackedEnumValueResolver::class)] CategoryEnum $category = null,
): Response {
}
CategoryEnum:
enum ArtistRank: int
{
case FOO = 1;
case BAR = 2;
}
Possible Solution
Create separate resolvers for \IntBackedEnum and \StringBackedEnum
OR
Catch \TypeError
along with the \ValueError
in BackedEnumValueResolver
and throw NotFoundHttpException
OR
Use tryFrom
instead of try
in BackedEnumValueResolver
(probably a bad idea, because it will return null
Additional Context
I can avoid this by using requirements in the router description like this:
requirements: ['category' => new EnumRequirement(CategoryEnum::class)],
But this makes the route attribute unnecessary huge and less readable