You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
MapQueryString attribute fails parsing string value for boolean type.
Problem is nicely described in this issue #42715
But in this case we know for sure that it's a string (just like the CSV and XML types in AbstractObjectNormalizer.php
Don't know if it's a bug or a feature.
How to reproduce
I expect the following to be false, but true is returned.
Request Url
https://example.com?myBoolean=false
DTO
<?php
readonly class MyDTO
{
public function __construct(
public bool $myBoolean
)
}
Controller
<?php
use Symfony\Component\HttpKernel\Attribute\MapQueryString;
use Symfony\Component\Routing\Annotation\Route;
class MyController
{
#[Route('/')]
public function __invoke(
#[MapQueryString]
MyDTO $dto
) {
dd($dto->myBoolean);
}
}
Possible Solution
No response
Additional Context
No response
The text was updated successfully, but these errors were encountered:
…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: #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
-------
5c20295 [HttpKernel] allow boolean argument support for MapQueryString
Symfony version(s) affected
6.3, 6.4, 7.0
Description
MapQueryString attribute fails parsing string value for boolean type.
Problem is nicely described in this issue #42715
But in this case we know for sure that it's a string (just like the CSV and XML types in AbstractObjectNormalizer.php
Don't know if it's a bug or a feature.
How to reproduce
I expect the following to be false, but true is returned.
Request Url
DTO
Controller
Possible Solution
No response
Additional Context
No response
The text was updated successfully, but these errors were encountered: