Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[HttpKernel] allow boolean argument support for MapQueryString#54153
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
nicolas-grekas commentedMar 5, 2024
I'm not sure how useful this could be: the query string can be a tuple of many different type of values, while this works only of all properties of the DTO are of the same type, isn't it? |
Jean-Beru commentedMar 5, 2024
The associative array allows to define which parameter is concerned. TBH, I think that this mapping will mainly be used to cast string to boolean since other types (ex: string to int) are already handled by the Serializer. |
nicolas-grekas commentedMar 5, 2024
Ah OK, so "restricted" is the name of the property. This wasn't obvious to me. It's a bit of a shame that a type that is already clearly described on the DTO need to be specified again somewhere else. This maps to the serializer component, which could have a mode to do the cast on its own, don't you think? |
Jean-Beru commentedMar 5, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
I totally agree.
We could pass |
nicolas-grekas commentedMar 5, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Yes, that'd be better IMHO. Dunno if bool is the only concerned type of if other ones could benefit from using filter_var to do the casting. Would need to be investigated. |
Jean-Beru commentedMar 6, 2024
I updated this PR and its description this way with a new |
nicolas-grekas left a comment
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 like that :)
src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
| returnfilter_var($value, \FILTER_VALIDATE_BOOL, \FILTER_NULL_ON_FAILURE) ??$value; | ||
| return$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.
Looks wrong :)
be335e6 to5c20295Comparefabpot commentedMar 17, 2024
Thank you@Jean-Beru. |
…ryString (Jean-Beru)This PR was squashed before being merged into the 7.1 branch.Discussion----------[HttpKernel] allow boolean argument support for MapQueryString| Q | A| ------------- | ---| Branch? | 7.1| Bug fix? | no| New feature? | yes| Deprecations? | no| Issues | Fix #53616| License | MIT~~Add a `$filters` argument to the `MapQueryString` attribute to allow filtering parameters using `filter_var` function. It will be useful to cast "false", "0", "no" or "off" to `false` before denormalizing the request data.~~**EDIT**Add a `AbstractNormalizer::FILTER_BOOL` context option to cast to a boolean using the `filter_var` function with the `\FILTER_VALIDATE_BOOL` filter (seehttps://www.php.net/manual/fr/filter.filters.validate.php).`MapQueryString` will use this context option when denormalizing the query string.`MapPayload` also but only with data coming from a form.See the relative comment:symfony/symfony#54153 (comment)Example:```phpfinal readonly class SearchDto{ public function __construct(public ?bool $restricted = null) { }}final class MyController{ #[Route(name: 'search', path: '/search')] public function __invoke(#[MapQueryString] SearchDto $search): Response { // /search?restricted= // "true", "1", "yes" and "on" will be cast to true // "false", "0", "no", "off" and "" will be cast to false // other will be cast to null }}```Commits-------5c20295662 [HttpKernel] allow boolean argument support for MapQueryString
…ryString (Jean-Beru)This PR was squashed before being merged into the 7.1 branch.Discussion----------[HttpKernel] allow boolean argument support for MapQueryString| Q | A| ------------- | ---| Branch? | 7.1| Bug fix? | no| New feature? | yes| Deprecations? | no| Issues | Fix #53616| License | MIT~~Add a `$filters` argument to the `MapQueryString` attribute to allow filtering parameters using `filter_var` function. It will be useful to cast "false", "0", "no" or "off" to `false` before denormalizing the request data.~~**EDIT**Add a `AbstractNormalizer::FILTER_BOOL` context option to cast to a boolean using the `filter_var` function with the `\FILTER_VALIDATE_BOOL` filter (seehttps://www.php.net/manual/fr/filter.filters.validate.php).`MapQueryString` will use this context option when denormalizing the query string.`MapPayload` also but only with data coming from a form.See the relative comment:symfony/symfony#54153 (comment)Example:```phpfinal readonly class SearchDto{ public function __construct(public ?bool $restricted = null) { }}final class MyController{ #[Route(name: 'search', path: '/search')] public function __invoke(#[MapQueryString] SearchDto $search): Response { // /search?restricted= // "true", "1", "yes" and "on" will be cast to true // "false", "0", "no", "off" and "" will be cast to false // other will be cast to null }}```Commits-------5c20295662 [HttpKernel] allow boolean argument support for MapQueryString
antoniovj1 commentedApr 18, 2024
Will this fix apply to Symfony 7.0? |
nicolas-grekas commentedApr 18, 2024
It's not a fix but a new feature, so 7.1 |
antoniovj1 commentedApr 18, 2024
Makes sense, thanks! |
Uh oh!
There was an error while loading.Please reload this page.
Add a$filtersargument to theMapQueryStringattribute to allow filtering parameters usingfilter_varfunction. It will be useful to cast "false", "0", "no" or "off" tofalsebefore denormalizing the request data.EDIT
Add a
AbstractNormalizer::FILTER_BOOLcontext option to cast to a boolean using thefilter_varfunction with the\FILTER_VALIDATE_BOOLfilter (seehttps://www.php.net/manual/fr/filter.filters.validate.php).MapQueryStringwill use this context option when denormalizing the query string.MapPayloadalso but only with data coming from a form.See the relative comment:#54153 (comment)
Example: