Skip to content

BackedEnumValueResolver fails to resolve \IntBackedEnum when passing string #52053

@ozahorulia

Description

@ozahorulia

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

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy