Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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

Merged

Conversation

@Jean-Beru
Copy link
Contributor

@Jean-BeruJean-Beru commentedMar 4, 2024
edited
Loading

QA
Branch?7.1
Bug fix?no
New feature?yes
Deprecations?no
IssuesFix#53616
LicenseMIT

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 aAbstractNormalizer::FILTER_BOOL context option to cast to a boolean using thefilter_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:#54153 (comment)

Example:

finalreadonlyclass SearchDto{publicfunction__construct(public ?bool$restricted =null)    {    }}finalclass MyController{    #[Route(name:'search', path:'/search')]publicfunction__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    }}

antoniovj1 reacted with heart emoji
@nicolas-grekas
Copy link
Member

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
Copy link
ContributorAuthor

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.#[MapQueryString(filters: ['restricted' => \FILTER_VALIDATE_BOOL])] will only apply this filter on therestricted parameter.

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
Copy link
Member

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
Copy link
ContributorAuthor

Jean-Beru commentedMar 5, 2024
edited
Loading

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.

I totally agree.

This maps to the serializer component, which could have a mode to do the cast on its own, don't you think?

We could pass[FILTER_BOOL => value] in the context to apply this filter. Should this filter be applied by default when using#[MapQueryString] ? Current request parameters likeactive=false will be cast tofalse in place oftrue.

@nicolas-grekas
Copy link
Member

nicolas-grekas commentedMar 5, 2024
edited
Loading

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.
Then yes, I would enable that flag for MapQueryString, but also for MapRequestPayloadwhen the payload is a form submission.

@Jean-BeruJean-Beru changed the title[HttpKernel] allow boolean argument support for MapQueryString[HttpKernel][Serializer] allow boolean argument support for MapQueryStringMar 6, 2024
@Jean-Beru
Copy link
ContributorAuthor

I updated this PR and its description this way with a newAbstractNormalizer::FILTER_BOOL context option.

Copy link
Member

@nicolas-grekasnicolas-grekas left a comment

Choose a reason for hiding this comment

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

I like that :)

Jean-Beru reacted with heart emoji
@carsonbotcarsonbot changed the title[HttpKernel][Serializer] allow boolean argument support for MapQueryString[HttpKernel] allow boolean argument support for MapQueryStringMar 13, 2024

returnfilter_var($value, \FILTER_VALIDATE_BOOL, \FILTER_NULL_ON_FAILURE) ??$value;
return$value;
}
Copy link
Member

Choose a reason for hiding this comment

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

Looks wrong :)

@fabpotfabpotforce-pushed thefix/53616-MapQueryString-with-bool branch frombe335e6 to5c20295CompareMarch 17, 2024 14:48
@fabpot
Copy link
Member

Thank you@Jean-Beru.

@fabpotfabpot merged commitd7d670e intosymfony:7.1Mar 17, 2024
symfony-splitter pushed a commit to symfony/http-kernel that referenced this pull requestMar 17, 2024
…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
symfony-splitter pushed a commit to symfony/serializer that referenced this pull requestMar 17, 2024
…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
Copy link

Will this fix apply to Symfony 7.0?

@nicolas-grekas
Copy link
Member

It's not a fix but a new feature, so 7.1

antoniovj1 reacted with thumbs up emojijejey reacted with laugh emoji

@antoniovj1
Copy link

Makes sense, thanks!

@Jean-BeruJean-Beru deleted the fix/53616-MapQueryString-with-bool branchApril 23, 2024 14:31
@fabpotfabpot mentioned this pull requestMay 2, 2024
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@fabpotfabpotfabpot requested changes

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

@dunglasdunglasAwaiting requested review from dunglasdunglas is a code owner

Assignees

No one assigned

Projects

None yet

Milestone

7.1

Development

Successfully merging this pull request may close these issues.

MapQueryString doesn't support string value for boolean type

5 participants

@Jean-Beru@nicolas-grekas@fabpot@antoniovj1@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp