Skip to content

Serializer should throw NotNormalizableValueException instead of InvalidArgumentException when an invalid enum is deserialized #49711

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 up 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

Closed
ludekbenedik opened this issue Mar 16, 2023 · 3 comments

Comments

@ludekbenedik
Copy link

Description

Hi,
when deserialize data to an entity with an enum attribute and data contains an invalid enum 's value, the serializer throws InvalidArgumentException which is a logic exception.

  1. This is not a program logic error so InvalidArgumentException is not correct.
  2. InvalidArgumentException is very a common exception so catching it is very difficult. A programmer must test the exception's message if an other logic exception have to be threw and logged.

For example, when deserialize a wrong date format then NotNormalizableValueException is thrown. For a programmer these situations are equal.

Example

enum MyEnum: int
{
    case A = 0;
    case B = 1;
}

class MyEntity
{
    public MyEnum $myEnum;
}

$entity = $serializer->deserialize('{"myEnum":2}', MyEntity::class, 'json')

Before:

Throws InvalidArgumentException

After:

Throws NotNormalizableValueException

@OskarStark OskarStark changed the title Serializer should throws NotNormalizableValueException instead of InvalidArgumentException when an invalid enum is deserialized Serializer should throw NotNormalizableValueException instead of InvalidArgumentException when an invalid enum is deserialized Mar 16, 2023
@Gwemox
Copy link
Contributor

Gwemox commented Apr 5, 2023

Looks like it was changed by #47128

I also have a related bug #47797

@secit-pl
Copy link

In my opinion we should throw NotNormalizableValueException instead of InvalidArgumentException. At this moment it's not possible to use backed enums in real world app while deserializing invalid data from user (user provide wrong value to backed enum setter). InvalidArgumentException does not have methods to return the detailed error message to user that the allowed values are x, y, z but the provided value is k, we can only return "Hey, something goes wrong, guess what!", without any details.

@fabpot
Copy link
Member

fabpot commented Aug 26, 2023

Should be fixed by #51475

@fabpot fabpot closed this as completed Aug 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants