Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[MapQueryString] Weird behavior while mapping integer variable #59104

Copy link
Copy link
Closed
@marcelowzd

Description

@marcelowzd
Issue body actions

Symfony version(s) affected

7.1.9

Description

While trying to map a GET request to a DTO which contains a nullable integer variable, symfony does not initialize the entity and also does not respond to the request with the validation error.

How to reproduce

Create the following DTO

use Symfony\Component\Validator\Constraints as Assert;

readonly class FilterDTO {
  public function __construct(
    #[Assert\NotBlank(allowNull: true, message: "The ID must not be blank.")]
    #[Assert\Positive(message: "The ID must be positive.")]
    public ?int $id = null,

    #[Assert\NotBlank(allowNull: true, message: "The name must not me blank.")]
    #[Assert\NoSuspiciousCharacters]
    public ?string $name = null,
  )
  {

  }
}

Create the following controller

#[Route(path: "/test", name: "test", methods: ["GET"], format: "json")]
public function readAll(
  #[MapQueryString] FilterDTO $filter = new FilterDTO
): JsonResponse 
{
   dd($filter);
   return $this->json(data: [$filter]);
}

In the browser, postman or any other tool, make the requests to the endpoint /test

The first request will generate the following output (/test):
ok_result
The second request will generate the following output (/test?id=x):
wrong_result

Possible Solution

No response

Additional Context

If using any other constraint, like Length, in a string field, symfony correctly validates the field and returns a 404 error, but with integer fields it does not respond with the validation error and also does not create the DTO (the fields become not initialized).

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

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.