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

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
thijsBreker opened this issue Jan 2, 2024 · 3 comments

Comments

@thijsBreker
Copy link
Contributor

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

@webmake
Copy link

webmake commented Jan 12, 2024

Error occurs with 5.4.34 as well, after bumping from 5.4.31, so not sure which version broke exactly yet symfony/serializer@v5.4.31...v5.4.34

@xabbuh
Copy link
Member

xabbuh commented Jan 12, 2024

@webmake Can you confirm that #53361 fixes this?

@webmake
Copy link

webmake commented Jan 12, 2024

Yes, it looks like patch would help

nicolas-grekas added a commit that referenced this issue Jan 29, 2024
… when denormalizing (thijsBreker)

This PR was merged into the 5.4 branch.

Discussion
----------

[Serializer] Take unnamed variadic parameters into account when denormalizing

We shouldn't break when a constructor has variadic parameters without named keys in the array.

| Q             | A
| ------------- | ---
| Branch?       |  5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #53354
| License       | MIT

Commits
-------

cbecdfe [Serializer] Take unnamed variadic parameters into account when denormalizing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants