Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Groups Annotations don't works #1628

Open
doums opened this issue Dec 6, 2016 · 3 comments
Open

Groups Annotations don't works #1628

doums opened this issue Dec 6, 2016 · 3 comments

Comments

@doums
Copy link

doums commented Dec 6, 2016

Hi,

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.

[EDIT2] fixed with

    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.

@alfupe
Copy link

alfupe commented Feb 5, 2017

Hi, take a look at #1662. I had a similar issue.

@achasseux
Copy link

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 !

@numediaweb
Copy link

see also: #1896

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants