I am building an API with Symfony and JMS Serializer (via FOSRestundle) that exposes trees. I've created an Tree entity that contains an id, a title and the root node of the tree. I've created a Node entity too, containg the chaining between nodes.
My API contains a public part and an admin part, and I want trees to be exposed differently according if the controller belongs to one or the other:
I've come to the following code:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
/**
* @ORM\Entity(repositoryClass="App\Repository\TreeRepository")
*/
class Tree {
/**
* Unique ID to identify the tree
*
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @Serializer\Groups({"ADMIN", "API"})
*/
private $id;
/**
* Name of the tree (example = a failure to diagnose)
*
* @ORM\Column(type="string", length=255)
* @Serializer\Groups({"ADMIN", "API"})
*/
private $title;
/**
* @ORM\OneToOne(targetEntity="App\Entity\Node")
* @ORM\JoinColumn(referencedColumnName="id")
*
* @Serializer\Groups({"ADMIN"})
*/
private $firstNode;
public function getId()
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getFirstNode(): ?Node
{
return $this->firstNode;
}
public function setFirstNode(?Node $firstNode): self
{
$this->firstNode = $firstNode;
return $this;
}
}
As you see, I've created two exclusion groups so that I can hide or expose the properties I want. That works great!
But for the properties inside the node to be visible, I have to add the @Serializer\Groups
annotations for all the properties, and propagate it to the classes of the properties all down along the dependencies.
I would like not to have to copy the @Serializer\Groups
annotations in all of my entity classes. So I tried with JMS Exclusion policies (@Serializer\ExclusionPolicy()
), but this does not seem to work.
Is there a way to expose/exclude a class, inconditionnaly of the current JMS exclusion group ? Thanks.
After 24 hours, I realised that I misused the concept of exclusion groups for jms serializer.
This resolves my problem nicely. But maybe my initial demand is still legit.