Thanks to visit codestin.com
Credit goes to github.com

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

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 👍

break;
}
}
Expand Down