Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

BackedEnumValueResolver fails to resolve \IntBackedEnum when passing string #52053

Copy link
Copy link
Closed
@ozahorulia

Description

@ozahorulia
Issue body actions

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.