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

Skip to content

MapQueryString doesn't support string value for boolean type #53616

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
mvanduijker opened this issue Jan 23, 2024 · 1 comment · Fixed by #54153
Closed

MapQueryString doesn't support string value for boolean type #53616

mvanduijker opened this issue Jan 23, 2024 · 1 comment · Fixed by #54153

Comments

@mvanduijker
Copy link

mvanduijker commented Jan 23, 2024

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

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

@andrashegedus
Copy link

I have this problem too.
I use Symfony 6.3.7.
kép

@fabpot fabpot closed this as completed Mar 17, 2024
fabpot added a commit that referenced this issue Mar 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 (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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants