-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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 our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[HttpKernel] allow boolean argument support for MapQueryString #54153
Conversation
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? |
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. |
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? |
I totally agree.
We could pass |
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. |
I updated this PR and its description this way with a new |
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
Outdated
Show resolved
Hide resolved
|
||
return filter_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
to
5c20295
Compare
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 (see https://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: ```php final 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 (see https://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: ```php final 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
Will this fix apply to Symfony 7.0? |
It's not a fix but a new feature, so 7.1 |
Makes sense, thanks! |
Add a$filters
argument to theMapQueryString
attribute to allow filtering parameters usingfilter_var
function. It will be useful to cast "false", "0", "no" or "off" tofalse
before denormalizing the request data.EDIT
Add a
AbstractNormalizer::FILTER_BOOL
context option to cast to a boolean using thefilter_var
function with the\FILTER_VALIDATE_BOOL
filter (see https://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: #54153 (comment)
Example: