-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Routing] Convert backed enums to their value when generating default routes #49206
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
florentdestremau
wants to merge
2
commits into
symfony:6.3
from
florentdestremau:feature/router-enum-defaults
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have rejected this kind of changes in the past. Please call
->value
on your enum case yourself if you need the backing value.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't do that in my own code, as stated in #49203, if I set a default value in my function is has to be an Enum, but the router asks for a string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be honest, to me it's more of a bug rather than a feature: as long as Enum is supported for route generation, the behavior that crashes in my issue (setting a default Enum in function parameters) is a bug. I understand that it's easier to port as a new feature but really this should not be a userland problem.
A lot of it still stems from php/php-src#8825 where Enums don't get their
__toString
method.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@derrabus this code path is a place where the loader guesses the default based on the controller default value.
In the associated issue, I explicitly asked to change only that place and not adding support for using enums directly in the Route attribute.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only other solution would be to make that guesser skip any default value that is not a scalar (to avoid generating an invalid default). But this would make DX worse.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't we do this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think Stringable objects should be casted to create a default. The controller would then receive the string value from the request attributes and not the object that it has a default value.
And having
null
as default value would be a bad idea as well, as that might not be acceptable by the controller (anull
default in the route is not the same than no default)Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should then just skip objects (and non scalars basically)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's probably better than guessing an invalid default, especially as we don't fail during loading for such case but only during route generation later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll update the code and make some test cases 👍