Description
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 ?