Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groups Annotations don't works

Symfony 3.1.7 + FOSRestBundle latest version

<?php
namespace PM\ApiBundle\Controller;

...
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\View\View;

class ArticlesController extends FOSRestController
{
    /**
     * @ApiDoc(
     *  section="articles",
     *  resource=true,
     *  description="Get articles published"
     * )
     * @Rest\View(serializerGroups={"article"})
     * @Rest\Get("/articles")
     */
    public function getArticlesAction(Request $request)
    {
        $articles = $this->getDoctrine()
            ->getManager()
            ->getRepository('PMPlatformBundle:Article')
            ->findAllDateDesc();
        /* @var $articles Article[] */
        return $articles;
    }

Then in my Article entity I added this annotation @Groups({"article"}) with the right use statement.

Whit default serializer I get :

[
    [],
    []
]

Whit JMS serializer (bundle) I get :

{
    "0": {},
    "1": {}
}

(I have two articles in db) it seems like the "article" group is not recognized. When I use the default serializer whithout this annotations I get a circular error.

What's wrong ?

[EDIT] Same behavior with

/**
 * @ApiDoc(
 *  section="articles",
 *  resource=true,
 *  description="Get articles published"
 * )
 * @Rest\View()
 * @Rest\Get("/articles")
 */
public function getArticlesAction(Request $request)
{
    $context = new Context();
    $context->addGroup('article');

    $articles = $this->getDoctrine()
        ->getManager()
        ->getRepository('PMPlatformBundle:Article')
        ->findAllDateDesc();
    /* @var $articles Article[] */
    $view = $this->view($articles, 200);
    $view->setContext($context);

    return $view;
} 

The response still empty.

like image 659
doums Avatar asked May 09 '26 12:05

doums


2 Answers

You can keep the default serializer of symfony. No need of JMSSerializer.

You may have forgotten to activate the annotations of serializer in config.yml (https://symfony.com/doc/current/serializer.html#using-serialization-groups-annotations)

#app/config/config.yml
framework:
    ....
    serializer: { enable_annotations: true }

It is necessary to force the view_response_listener in config.yml (http://symfony.com/doc/master/bundles/FOSRestBundle/3-listener-support.html, http://symfony.com/doc/master/bundles/FOSRestBundle/view_response_listener.html)

#app/config/config.yml
fos_rest:
    view:
        view_response_listener: 'force'

That should work !

like image 146
Arno Avatar answered May 12 '26 02:05

Arno


Ok I fixed it using JMS serializer like this :

use JMS\Serializer\SerializationContext;
use JMS\Serializer\SerializerBuilder;


class ArticlesController extends FOSRestController
{
    /**
     * @ApiDoc(
     *  section="articles",
     *  resource=true,
     *  description="Get articles published"
     * )
     * @Rest\View()
     * @Rest\Get("/articles")
     */
    public function getArticlesAction(Request $request)
    {
        $serializer = SerializerBuilder::create()->build();

        $articles = $this->getDoctrine()
            ->getManager()
            ->getRepository('PMPlatformBundle:Article')
            ->findAllDateDesc();
        /* @var $articles Article[] */


        return $serializer->serialize($articles, 'json', SerializationContext::create()->setGroups(array('article')));
    }

Now the groups annotations works fine.

like image 34
doums Avatar answered May 12 '26 02:05

doums



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!