Skip to content

[Serializer] Serialization of empty ArrayCollection to JSON returns '{}' instead '[]' #37061

Closed
@yauhenko

Description

@yauhenko

Symfony version(s) affected: 5.0.9, 5.1.0

Description
Entity User

class User implements UserInterface {

	/**
	 * @ORM\Id()
	 * @ORM\GeneratedValue()
	 * @ORM\Column(type="integer", options={"unsigned": true})
	 * @Groups("main")
	 */
	private int $id;
       // ...
	/**
	 * @ORM\ManyToMany(targetEntity=Department::class)
	 * @ORM\JoinColumn(onDelete="CASCADE")
	 * @Groups("main")
	 */
	private $departments;

My controller

	public function getUser(User $id) {
            return $this->json($user, 200, [], ['groups' => ['main']);
	}

Expected:

{
    "id": 2,
   // ...
    "departments": []
  }

Actual result:

{
    "id": 2,
   // ...
    "departments": {}
  }

How to reproduce
Just upgraded symfony/serializer to 5.0.9

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