Skip to content

#[MapRequestPayload] Returns full ViolationsList if object cannot be created due to a single missing value #50759

Closed
@Havrin

Description

@Havrin

Symfony version(s) affected

6.3.0

Description

If you use #[MapRequestPayload] and the payload is missing a single (or multiple) fields, the ViolationList created by the resolver does not contain the single missing field, but all asserts on every field.

How to reproduce

Controller:

...
public function testPayloadAction(
        #[MapRequestPayload] User $user
    ): JsonResponse {
        return new JsonResponse(data: $user->getEmail());
    }
...

DTO:

final readonly class User
{
    public function __construct(
        #[Assert\NotNull,
            Assert\NotBlank,
            Assert\Email]
        private string $email,
        #[Assert\NotNull,
            Assert\NotBlank]
        private string $password,
    ) {
    }

    public function getEmail(): string
    {
        return $this->email;
    }

    public function getPassword(): string
    {
        return $this->password;
    }
}

Payload to route:

{
  "email": "test@mail.com"
}

Excpected: I would expect the error message that PartialDenormalizationException holds in RequestPayloadValueResolver.php to be returned. In this case:

Failed to create object because the class misses the "password" property.

Actual:

:
    This value should be of type unknown.
Object(...\DTO\User).email:
    This value should not be null. (code ad32d13f-c3d4-423b-909a-857b961eb720)
Object(...\DTO\User).email:
    This value should not be blank. (code c1051bb4-d103-4f74-8988-acbcafc7fdc3)
Object(...\DTO\User).password:
    This value should not be null. (code ad32d13f-c3d4-423b-909a-857b961eb720)
Object(...\DTO\User).password:
    This value should not be blank. (code c1051bb4-d103-4f74-8988-acbcafc7fdc3)

Possible Solution

Do not run the validation on an empty object in RequestPayloadValueResolver.php

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