Skip to content

[HttpKernel] #[MapQueryString] does not handle string and arrays properly, returning an empty / invalid dto #51235

@pedrocasado

Description

@pedrocasado

Symfony version(s) affected

6.3

Description

RequestPayloadValueResolver does not handle string and arrays properly, returning an empty / invalid dto even with all default values set.

class SearchDTO
{
    public function __construct(
        #[Assert\All([
            new Assert\NotBlank(),
            new Assert\Length(max: 50),
        ])]
        public readonly array $brands = [],

        #[Assert\All([
            new Assert\NotBlank(),
            new Assert\Length(max: 50),
        ])]
        public readonly array $colors = [],
    ) {
    }
}

How to reproduce

https://github.com/pedrocasado/dto
$ composer install
$ symfony serve

http://localhost:8000
http://localhost:8000/?brands[]=foo
http://localhost:8000/?brands=foo

DTO

class SearchDTO
{
    public function __construct(
        #[Assert\All([
            new Assert\NotBlank(),
            new Assert\Length(max: 50),
        ])]
        public readonly array $brands = [],

        #[Assert\All([
            new Assert\NotBlank(),
            new Assert\Length(max: 50),
        ])]
        public readonly array $colors = [],
    ) {
    }
}

Controller:

#[Route(path: '/', name: 'homepage')]
public function homepage(
    #[MapQueryString] SearchDTO $searchDTO = new SearchDTO()
): Response {
    dd($searchDTO);
}

Requests:

GET / (Ok, brands properly empty)

 App\DTO\SearchDTO {#22310 ▼
  +brands: []
  +colors: []
}

GET /?brands[]=foo (Ok, brands properly set)

 App\DTO\SearchDTO {#1053 ▼
  +brands: array:1 [▼
    0 => "foo"
  ]
  +colors: []
}

GET /?brands=foo (Nok, SearchDTO empty, not even initialized. It does only work when passing "brands[]")

App\DTO\SearchDTO {#1053}

Possible Solution

Am i missing the serializer context?

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

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy