Fosrestbundle: serializerGroups annotation not behaving as expected

Created on 24 Jul 2014  路  12Comments  路  Source: FriendsOfSymfony/FOSRestBundle

I'm trying to set up exclusion policies for my entities using yaml for the properties, and setting the serializerGroups on my view actions.

/**
     * @Rest\View(
     *      serializerGroups={"api_message"}
     * )
     * @param Request $request
     * @return \FOS\RestBundle\View\View
*/

However, the annotation serializerGroups, although recognised, doesn't set the groups for the serializer. Instead, the View's SerializationContext must be set:

$view->setSerializationContext(SerializationContext::create()->setGroups(['api_message']));

Most helpful comment

Annotations are meant to configure the View when you don't create it yourself but return only the data and expect the ViewListener to build the View object itself AFAIK

All 12 comments

None of the annotations work either, havent tried using yml though. Using jms/serializer 0.13.0.

can you also paste the action code?

Annotations are meant to configure the View when you don't create it yourself but return only the data and expect the ViewListener to build the View object itself AFAIK

Same problem here.

I have not been able to upgrade from FOSRestBundle 1.2.* since the @Rest\View annotation serializerGroups action does not work after upgrading to 1.3._, 1.4._ or 1.5.*.

I have no doubt it's my fault, I'm just not sure what I'm doing wrong.

can you paste your controller code?

Sure. Let me know if this is enough:

use Doctrine\DBAL\DBALException;
use JMS\Serializer\SerializationContext;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use FOS\RestBundle\View\View AS FOSView;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use FOS\RestBundle\Controller\Annotations as Rest;
use JMS\SecurityExtraBundle\Annotation as Security;

class StudentRestController extends Controller
{
    /**
     * Get student by id.
     *
     * @Rest\View(serializerGroups={"sg-default", "sg-details"})
     *
     * @ApiDoc()
     *
     * @param Student $student
     * @return FOSView
     *
     * @Security\Secure("ROLE_USER")
     */
    public function getStudentAction(Student $student)
    {
        return FOSView::create()->setStatusCode(200)->setData($student);
    }
}

Without looking too deeply into the code, it looks like the inclusion of $customViewDefined in ViewResponseListener is causing this issue.

Again, without looking too deeply into the code, there may be a logic error on line 89. I think serializer groups should be applied if $customViewDefined == true:

if ($configuration->getSerializerGroups() && !$customViewDefined) {

should be:

if ($configuration->getSerializerGroups() && $customViewDefined) {

I've never actually contributed to a project, but I can look into the process and give it a go if you'd like me to handle this one?

EDIT: I'm going to try to contribute this change. Please let me know if I do something wrong. :)

@robertblock I have the same issue and your change seems to work for me. Until a change has been implemented I will make sure I return a FOS\RestBundle\View\View which also seems to work (return $this->view($data, 200);).

@hallabro Thanks for the feedback!

I'm curious as to the intention behind $customViewDefined. I can't figure out why it was only used in checking whether to use groups when serializing.

see https://github.com/FriendsOfSymfony/FOSRestBundle/pull/692

the issue is that you are creating the View instance manually instead of letting the Listener create the instance.

@stof
Hi, i was confused that when @View annotaion is working, i see your answer in top:

Annotations are meant to configure the View when you don't create it yourself but return only the data and expect the ViewListener to build the View object itself AFAIK

I can use @View when return $view obj and set statusCode, template, templateVar but can not set serializerGroups or serializerEnableMaxDepthChecks (if i set, these no will work). why?

Was this page helpful?
0 / 5 - 0 ratings