Skip to content

[11.x] Prevent attributes cast to an enum from being set to another enum #47465

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

Merged
merged 2 commits into from
Jun 18, 2023

Conversation

PHLAK
Copy link
Contributor

@PHLAK PHLAK commented Jun 16, 2023

Prevent an attribute that is cast to an enum from being set to another enum value or a value not defined as a case on a BackedEnum. This will throw a ValueError like the following when attempting to do so.

Value [\App\SomeEnum::CASE] is not of the expected enum type [\App\AnotherEnum]

NOTE: This is technically a breaking change but I'd consider it more of a bug fix so handle that as you will.

Resolves #47454

@driesvints driesvints marked this pull request as draft June 16, 2023 18:24
@PHLAK PHLAK force-pushed the strict-enum-casts branch from 0c43398 to 391485d Compare June 16, 2023 18:24
@PHLAK
Copy link
Contributor Author

PHLAK commented Jun 16, 2023

Hmm... I'm not sure why this is getting errors in CI. It's generating the error and passing locally. This line should be catching the invalid string and throwing a ValueError. Could this be because of a difference in PHP versions (CI: 8.1, Local: 8.2)?

@PHLAK PHLAK force-pushed the strict-enum-casts branch from 391485d to 14230a7 Compare June 16, 2023 19:26
@PHLAK PHLAK changed the base branch from 10.x to master June 16, 2023 19:26
@PHLAK PHLAK changed the title Prevent attributes cast to an enum from being set to another enum [11.x] Prevent attributes cast to an enum from being set to another enum Jun 16, 2023
@PHLAK
Copy link
Contributor Author

PHLAK commented Jun 16, 2023

I've updated the PR to merge to master.

@PHLAK PHLAK marked this pull request as ready for review June 16, 2023 19:47
@PHLAK PHLAK requested a review from driesvints June 16, 2023 19:58
@driesvints driesvints requested review from taylorotwell and removed request for driesvints June 16, 2023 20:08
@taylorotwell taylorotwell merged commit 8647dcf into laravel:master Jun 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Casting a model attribute to an enum does not prevent assigning the attribute to a different enum
3 participants