Skip to content

[Serializer] Error when trying to deserialize an object with a constructor using a variadic parameter #53354

@thijsBreker

Description

@thijsBreker

Symfony version(s) affected

6.4.2 (Other versions most likely as well)

Description

For some reason the serializer doesn't seem to like variadic arguments in the constructor

How to reproduce

When deserializing a json object like this

{
    "requiredParameter": "required",
    "optionalParameter": "optional",
    "variadicParameter": [
        {"param": 1},
        {"param": 2}
    ]
}

into a PHP class like this

class ChildExample {
    public function __construct(
        private int $param,
    ) {}
}

class Example {
    private array $variadicProperty;

    public function __construct(
        private string $requiredParameter,
        private string $optionalParameter = '',
        SomeClass ...$variadicParameter,
    ) {
        $this->variadicProperty = $variadicParameter;
    }
}

I get the PHP error Error : Cannot use positional argument after named argument on line 429 of AbstractNormalizer.php.

Possible Solution

When I wrap the $params variable in an array_values() call the problem goes away. I'll try to create a pull request with my fix later.

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