JMS Serializer: How to limit the depth of serialisation for an object graph

后端 未结 2 486
走了就别回头了
走了就别回头了 2021-01-06 15:48

Maybe it is just my misunderstanding of this annotation however it does not seam to work as expected.

I have the following object graph

User
 -> C         


        
相关标签:
2条回答
  • 2021-01-06 16:09

    As of the latest version, using @MaxDepth() annotation and SerializationContext::create()->enableMaxDepthChecks() in the controller does the job.

    0 讨论(0)
  • 2021-01-06 16:25

    Because I did not have access to the latest version of the serializer I had to find a workaround to the @MaxDepth. This may also help you.

    Use @JMS\ExclusionPolicy("all") on all entities that are connected.

    Now use @JMS\Expose only on those properties that you want to have serialize. On join relations only use this annotation in one direction. That will fix your problem.

    namespace FinalConcept\TimeTracker\EntitiesBundle\Entity;
    
    use Doctrine\Common\Collections\ArrayCollection;
    use Doctrine\ORM\Mapping as ORM;
    use JMS\Serializer\Annotation as JMS;
    
    /**
     * FinalConcept\TimeTracker\EntitiesBundle\Entity
     *
     * @JMS\ExclusionPolicy("all")
     * @ORM\Table(name="users")
     * @ORM\Entity(repositoryClass="FinalConcept\TimeTracker\EntitiesBundle\Repository\UserRepository")
     */
    class User implements UserInterface, \Serializable
    {
         /**
         * @JMS\Expose
         * @ORM\ManyToOne(targetEntity="company", inversedBy="users")
         * @ORM\JoinColumn(name="company_id", referencedColumnName="id")
         */
        private $company;
    }
    
    0 讨论(0)
提交回复
热议问题