2
votes

I have a simple REST api created using FOSRestBundle, and returns serialized objects using JMSSerializerBundle.

One of the entities of my bundle uses an entity located in another bundle.

I set up serialization groups for my entity, but they are obviously not set for the aggregated entity so its json result is empty.

I've followed the documentation for YAML groups configuration: http://jmsyst.com/libs/serializer/master/reference/yml_reference but the file seems to be unused (adding groups to the entity's properties has no effect, and I don't get any errors if my YAML file is invalid).

Here's the code:

MyCompany\MyBundle\Entity\Meeting.php

class Meeting
{
  /**
   * @var Point
   *
   * @ORM\Column(name="location", type="point", nullable=true)
   * @JMS\Groups({"privateContact"})
   */
  private $location;

  // Getters, setters and other stuff
}

ACME\SomeBundle\ORM\Point.php

class Point
{
  /**
   * @var float
   */
  private $latitude;

  /**
   * @var float
   */
  private $longitude;

  // Getters, setters
}

MyCompany\MyBundle\Resources\config\serializer\Model.Point.yml

ACME\SomeBundle\ORM\Point:
    properties:
        latitude:
            groups: ['privateContact']
        longitude:
            groups: ['privateContact']

MyCompany\MyBundle\Controller\ApiController.php

/**
 * @Rest\View(serializerGroups={"privateContact"})
 */
public function getMeetingAction()
{
  ...

  return array(
    'status' => 'OK',
    'meeting' => $meeting
  );
}

Resulting JSON

"meeting":{"id":10,"date":"2015-07-16T19:20:00+0200","location":{}}
1

1 Answers

1
votes

For anyone else who has encountered this problem, I found that the View annotation in this example did not work. However, if I set the serialization context with the correct group in the controller action then it worked just fine:

$view->setSerializationContext(SerializationContext::create()->setGroups('site'));