Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Serializer][Normalizer] Doctrine Collection Normalizer #59786

Copy link
Copy link
Open
@rrajkomar

Description

@rrajkomar
Issue body actions

Description

While serializing from a Doctrine Collection type into the various format works perfectly, deserialization from an input format to an actual implementation of a Doctrine Collection does not.

In my case it was with a json content like the following :

{
    "users": [
        {"username": "suiteAdmin"},
        {"username": "user"}
    ]
}

With any specification this would be deserialized as an object with a property users containing an array of objects (no need to go further for this case)
However, in my case I want the final object to be an object with a property users containing a doctrine collection of objects

So I have the following PHP classes:
UserList

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;

final readonly class UserList
{
    public function __construct(
        /** @var ArrayCollection<User> */
        public Collection $users = new ArrayCollection()
    ) {
    }
}

User

final class User
{
    public function __construct(
        public readonly ?int $id = null,
        public readonly ?string $username = null
    ) {
    }
}

With those if I try to deserialize my JSON into the UserList object (using MapRequestPayload on my controller)

#[MapRequestPayload(acceptFormat: 'json')]
UserList $userList

And I follow the execution of the program, I can see that my json entry "users" is denormalized into an array but never converted into an ArrayCollection.

From what I gathered, it looks like what is missing is a normalizer to handle the collection types.
Considering that Symfony is often used with Doctrine as ORM, is there any reason not have one built-in ?
If not, could we have one in the list of built-in normalizers ?

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

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.