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

Skip to content

MapQueryString mapped wrong on boolean values #57540

Closed
@gcorso-nx

Description

@gcorso-nx

Symfony version(s) affected

7.1

Description

Recently, with #54153 , support for boolean values on DTOs have been added within the #[MapQueryString].

I recently ran into an issue, where the paramter value false was mistakenly interpreted as true.

How to reproduce

While this works:

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=
        // "false" would be converted to false
    }
}

This doesn't work:

final readonly class SearchDto
{
    public ?bool $restricted = null;
}

final class MyController
{
    #[Route(name: 'search', path: '/search')]
    public function __invoke(#[MapQueryString] SearchDto $search): Response
    {
        // /search?restricted=
        // "false" would be converted to true
    }
}

Other conversions work.

Applying a serialization context with the FILTER_BOOL flag aren't working too.
This also might be related to #57487.

Possible Solution

Looks like the applyFilterBool function is not called within AbstractNormalizer, if no constructor is given. Other assertions / convertions seems to work in DTOs, even if no constructor given.

Additional Context

Not using constructor works for all other use cases I've tested. With constructor we had issues with enums, why we used the solution like here

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions