8000 [Routing] Convert backed enums to their value when generating default routes by florentdestremau · Pull Request #49206 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[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

Conversation

florentdestremau
Copy link
Contributor
@florentdestremau florentdestremau commented Feb 2, 2023
Q A
Branch? 6.3
Bug fix? no
New feature? yes
Deprecations? no
Tickets Fix #49203
License MIT
Doc PR symfony/symfony-docs#...

This is a working update to allow usage of enums as php defaults in controllers.

@carsonbot
Copy link

Hey!

To help keep things organized, we don't allow "Draft" pull requests. Could you please click the "ready for review" button or close this PR and open a new one when you are done?

Note that a pull request does not have to be "perfect" or "ready for merge" when you first open it. We just want it to be ready for a first review.

Cheers!

Carsonbot

@florentdestremau florentdestremau marked this pull request as ready for review February 2, 2023 18:42
@carsonbot carsonbot added this to the 6.3 milestone Feb 2, 2023
@carsonbot
Copy link

Hey!

Thanks for your PR. You are targeting branch "6.3" but it seems your PR description refers to branch "6.3 for features".
Could you update the PR description or change target branch? This helps core maintainers a lot.

Cheers!

Carsonbot

@stof
Copy link
Member
stof commented Feb 2, 2023

Please add tests covering that to avoid regressions.

@@ -205,7 +205,8 @@ protected function addRoute(RouteCollection $collection, object $annot, array $g
}
foreach ($paths as $locale => $path) {
if (preg_match(sprintf('/\{%s(?:<.*?>)?\}/', preg_quote($param->name)), $path)) {
$defaults[$param->name] = $param->getDefaultValue();
$defaultValue = $param->getDefaultValue();
$defaults[$param->name] = $defaultValue instanceof \BackedEnum ? $defaultValue->value : $defaultValue;
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link
Member
@stof stof Feb 3, 2023

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.

Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make that guesser skip any default value that is not a scalar

shouldn't we do this?

$defaults[$param->name] = match (true) {
    $defaultValue instanceof \BackedEnum => $defaultValue->value,
    $defaultValue instanceof \Stringable => (string) $defaultValue,
    \is_object($defaultValue) => null,
    default => $defaultValue,
};

Copy link
Member

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 (a null default in the route is not the same than no default)

Copy link
Member
@nicolas-grekas nicolas-grekas Feb 24, 2023

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)?

if (\is_scalar($defaultValue = $param->getDefaultValue()) {
    $defaults[$param->name] = $defaultValue;
} elseif ($defaultValue instanceof \BackedEnum) {
    $defaults[$param->name] = $defaultValue->value;
}

Copy link
Member

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.

Copy link
Contributor Author

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 👍

@stof
Copy link
Member
stof commented Feb 24, 2023

@florentdestremau can you add tests to make this PR ready ?

@florentdestremau
Copy link
Contributor Author

Yes, I'll spend some time this weekend I hope.

@florentdestremau
Copy link
Contributor Author

hi @stof , I can't find a good place to start for the tests or some existing ones that are similar. Could you point me in the right direction?

@nicolas-grekas
Copy link
Member

Closing in favor of #50031

@florentdestremau florentdestremau deleted the feature/router-enum-defaults branch April 17, 2023 08:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Router] Can't put a default Enum value in a controller action
5 participants
0