Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpKernel] Conversion of type in #[MapQueryString] constructor is no longer accepted since Symfony 7.1.10 #59353

Open
@ostrolucky

Description

@ostrolucky

Symfony version(s) affected

7.1.10

Description

Since #59134, DTO that changes type in a constructor is no longer accepted.

Trigger of this seem to be that in a given PR, disable_type_enforcement no longer defaults to true. However, this might be a deeper issue, as thanks to type juggling in constructor there should not be any type mismatch in the first place. Notice that in reproducer, although (private) property is declared to be an array, constructor accepts string. Hence, denormalization should accept string.

I bet this is another issue of a kind in #46918, since serializer has no way to distinguish class property named same way as constructor parameter

How to reproduce

Query DTO:

readonly class ScimQueryInput
{
    /** @var list<string> */
    private array $attributes;

    public function __construct(string $attributes = '') {
        $this->attributes = $attributes ? explode(',', $attributes) : [];
    }

    public function isAttributeAllowed(string $attribute): bool
    {
        return !\in_array($attribute, $this->attributes, true);
    }
}

Controller:

class GetUsersController
{
    #[Route('/api/datasync/scim/{connectorId}/Users')]
    public function __invoke(#[MapQueryString] ScimQueryInput $queryInput = new ScimQueryInput()): Response 
    {
        return new Response();
    }
}

Call: /api/datasync/scim/Users?attributes=displayName,userName

Response:

      Current response status code is 404, but 200 expected. (Behat\Mink\Exception\ExpectationException)
    │
    │  {
    │      "type": "https://symfony.com/errors/validation",
    │      "title": "Validation Failed",
    │      "detail": "attributes: This value should be of type array.",
    │      "violations": [
    │          {
    │              "propertyPath": "attributes",
    │              "title": "This value should be of type array.",
    │              "template": "This value should be of type {{ type }}.",
    │              "parameters": {
    │                  "{{ type }}": "array"
    │              }
    │          },
    │      ]
    │  }

Possible Solution

No response

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