I read this article about overriding groups of child properties:
use JMS\Serializer\SerializationContext;
$context = SerializationContext::create()->setGroups(array(
'Default', // Serialize John's name
'manager_group', // Serialize John's manager
'friends_group', // Serialize John's friends
'manager' => array( // Override the groups for the manager of John
'Default', // Serialize John manager's name
'friends_group', // Serialize John manager's friends. If you do not override the groups for the friends, it will default to Default.
),
'friends' => array( // Override the groups for the friends of John
'manager_group' // Serialize John friends' managers.
'manager' => array( // Override the groups for the John friends' manager
'Default', // This would be the default if you did not override the groups of the manager property.
),
),
));
$serializer->serialize($john, 'json', $context);
In FOSRestBundle I am using the @View
annotation with the serializerGroups
property:
/**
* @Rest\Get("/api/users/{id}", name="api_get_user")
* @Rest\View(serializerGroups={"Default", "detail", "friends":{"Default"})
*/
public function getAction(Request $request, User $user = null)
{
return $user;
}
How can I override child properties using that annotation?