Json endpoint circular reference handler HTTP 500 ERROR

Im currently making an json endpoint for objects (tickets).

This is my function:

/**
 * @Route("/json/tickets", name="json_tickets")
 */
public function jsonTickets()
{
    $encoders = [new XmlEncoder(), new JsonEncoder()];
    $normalizers = [new ObjectNormalizer()];
    $serializer = new Serializer($normalizers, $encoders);
    $tickets = $this->getDoctrine()->getRepository(Ticket::class)->findAll();

    $jsonObject = $serializer->serialize($tickets, 'json', [
        'circular_reference_handler' => function ($object) {
            return $object->getId();
        }
    ]);

    return new Response($jsonObject);
}

This is the entity for Tickets

    class Ticket
  {
    /**
     * @ORMId()
     * @ORMGeneratedValue()
     * @ORMColumn(type="integer")
     */
    private $id;

    /**
     * @ORMManyToOne(targetEntity="AppEntityUser", inversedBy="tickets")
     */
    private $user;

    /**
     * @ORMManyToMany(targetEntity="AppEntityUser", inversedBy="ticket_responsibility")
     */
    private $responsible;

    /**
     * @ORMManyToOne(targetEntity="AppEntityCompany", inversedBy="tickets")
     */
    private $company;

    /**
     * @ORMColumn(type="integer")
     */
    private $priority;

    /**
     * @ORMColumn(type="text", nullable=true)
     */
    private $description;

    /**
     * @ORMColumn(type="string", length=255)
     */
    private $status;


----------

This function returns HTTP 500 ERROR.
In the dev.log it gets all the right values and shows no error.

I searched for awnsers but couldn’t find any right awnsers for this function.

What is wrong in my function that the server returns an error?
Thanks!

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *