Closed
Description
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):
The second request will generate the following output (/test?id=x):
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).