Skip to content

[HttpKernel] #[MapQueryString] with an empty query string always returns null #50690

Closed
@DrLuke

Description

@DrLuke

Symfony version(s) affected

6.3.0

Description

RequestPayloadValueResolver always returns null when mapping query strings and query string is empty.

This is problematic when using a DTO where all arguments have default values, e.g.

class MyDto {
    public function __construct(public string $foo = "bar")
    {}
}

How to reproduce

Controller:

class MyDto {
    public function __construct(public string $foo = "bar")
    {}
}

class FooController extends AbstractController
{
    #[Route("/foo", name: "foo", methods: ["GET"])]
    public function getPaginated(
        #[MapQueryString] MyDto $myDto,
    ): JsonResponse
    {
        return $this->json($myDto);
    }
}

GET /foo -> 404 HttpException
GET /foo?foo=baz -> 200 {"foo": "baz"}

Possible Solution

Remove this early return and let the serializer decide what to do with an empty query string.

Additional Context

No response

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