Search code examples
doctrine-ormsymfonyfosrestbundlejmsserializerbundle

Empty JSON Array when using @Groups annotation with JMS Serializer and FOSRestBundle v2.1


I'm trying to use the @Groups annotation to serialize my data but I'm just receiving an empty array.

I've tried using @Serializer\Expose on the fields I want to show up, but then they show up everywhere and ignore the groups.

From what I've read I'm pretty sure that I should only need to use the @Groups annotation but I just end up with this (there are two schedule entities):

{
  "schedules": [
    {},
    {}
  ]
}

There seems to have been a change to how this is supposed to work, I've found examples for previous versions FOSRestBundle, I just have not been able to port them over to the new version.

From what I understand you can set groups in the serializer context and use that to set the views context, but I've had no luck with that.

https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/UPGRADING-2.0.md

View::setSerializationContext and View::getSerializationContext have been removed. Use View::setContext and View::getContext together with the new Context class instead.

Before:

use JMS\Serializer\SerializationContext;

$view = new View();
$context = new SerializationContext();
$view->setSerializationContext($context);
$context = $view->getSerializationContext();

After:

use FOS\RestBundle\Context\Context;

$view = new View();
$context = new Context();
$view->setContext($context);
$context = $view->getContext();

I'm having a hell of a time getting my head around all this, and would ever so grateful for any assistance to point me in the right direction.

As well as the following I attempted to use FOS\RestBundle\Controller\Annotations as Rest; and @Rest\View(serializerGroups({"schedule"}) annotation with the on the controller, it had the same effect as I'm seeing now (ie none).

Schedule Controller

use FOS\RestBundle\Context\Context;

class ScheduleController extends BaseController
{
    /**
     * @param $date
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function getSchedulesScheduleAction($date)
    {

        $em = $this->getDoctrine()->getManager();
        list($startDate, $endDate) = $this->get('radio.utils.date_and_time')->findWeekRange($date);
        $schedules = $em->getRepository('RadioBundle:Schedule')->findByRange($startDate, $endDate);

        $view = $this->view(
            [
                'schedules' => $schedules,
            ],
            200
        );

        $context = new Context();
        $context->setGroups(["schedule"]);
        $view->setContext($context);

        return $this->handleView($view);
    }
}

Schedule Entity

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use JMS\Serializer\Annotation as Serializer;

/**
 * Schedule
 *
 * @ORM\Table(name="schedule")
 * @ORM\Entity(repositoryClass="RadioBundle\Repository\ScheduleRepository")
 * @Serializer\ExclusionPolicy("all")
 */
class Schedule
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var \DateTime
     * @Assert\NotBlank()
     * @Serializer\Groups({"schedule"})
     * @ORM\Column(name="start_time", type="datetime")
\     */
    private $startTime;

    /**
     * @var \DateTime
     * @Assert\NotBlank()
     * @Serializer\Groups({"schedule"})
     * @ORM\Column(name="end_time", type="datetime")
     */
    private $endTime;



    /**
     * @Assert\NotBlank()
     * @Serializer\Groups({"schedule"})
     * @ORM\ManyToOne(targetEntity="RadioBundle\Entity\RadioShow", inversedBy="schedule")
     */
    private $radioShow;

    ...

app/config.yml

fos_rest:
    param_fetcher_listener: true
    body_listener: true
    format_listener: true
    view:
        view_response_listener: 'force'
    routing_loader:
        default_format: json

jms_serializer:
    metadata:
        auto_detection: true

Solution

  • With ExclusionPolicy("all") on the entity you will need to expose the property with the groups.

    E.g.

       /**
         * @var \DateTime
         * @Assert\NotBlank()
         * @Serializer\Groups({"schedule"})
         * @Serializer\Expose()
         * @ORM\Column(name="start_time", type="datetime")
         */
        private $startTime;