3

I have been using the Group annotation for years on SF2 and SF3. I'm trying SF4.1. And I'm getting an empty JSON when I send a GET to my endpoint.

The interesting parts of my composer.json:

 "friendsofsymfony/rest-bundle": "^2.3",
 "jms/serializer-bundle": "^2.3",
 "sensio/framework-extra-bundle": "^5.1",
 "symfony/serializer-pack": "^1.0"

The config:

framework:
    serializer:
       enabled: true
       enable_annotations: true

sensio_framework_extra:
    view: { annotations: true }

fos_rest:
   routing_loader:
        default_format: json

   view:
        view_response_listener: 'force'

   format_listener:
        rules:
               - { path: ^/, prefer_extension: true, fallback_format: json, priorities: [ json,xml, html  ] }

The Entity

use JMS\Serializer\Annotation\Groups;

class User implements UserInterface, \Serializable
{

private $id;

/**
 * @Groups({"api"})
 */
private $username;

And the endpoint API Controller:

use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Context\Context;
use FOS\RestBundle\View\View;

class UserController extends FOSRestController {

public function getUserAction(Request $request, EntityManagerInterface $em)
{
    $user = $em->getReference('App:User', 1);
    $view = View::create();
    $context = new Context();
    $context->setGroups(['api']);
    $view->setContext($context);
    $view->setData($user);
    return $this->handleView($view);
 }
}

If I remove `$context->setGroups(['api']), the JSON has all the User attributes.

Any idea? Thanks!

Debug Info:

bin/console debug:container jms

Select one of the following services to display its information [fos_rest.serializer.jms]: [0] fos_rest.serializer.jms

0

Information for Service "fos_rest.serializer.jms" =================================================


Option Value


Service ID fos_rest.serializer.jms
Class FOS\RestBundle\Serializer\JMSSerializerAdapter
Tags -
Public no
Synthetic no
Lazy yes
Shared yes
Abstract no
Autowired no
Autoconfigured no


Arco Voltaico
  • 860
  • 13
  • 29
  • Can you try and add the following annotation: `@Symfony\Component\Serializer\Annotation\Groups({"api"})` to your `$username` field? If it then works you're using the symfony serializer and not JMS. – vstm Jul 07 '18 at 18:25
  • You are right :-) And how I can use the JMS serializer? – Arco Voltaico Jul 08 '18 at 19:22
  • I've checked if the `jms_serializer.serializer` service exists, it has preference over the `serializer` service. So it should be the default if the bundle is loaded (check with `bin/console debug:container jms_serializer.serializer` if this is the case). If it is indeed loaded, it seems that the serializer service is overrided (check with `bin/console debug:config fos_rest service.serializer`). If it is you can configure it under the "fos_rest.service.serializer" key and set it to `fos_rest.serializer.jms`. – vstm Jul 08 '18 at 20:39
  • I have updated the question including the debug info. I added the fos_rest.service.serialize: fos_rest.serializer.jms and I'm getting this error: ServiceNotFoundException The service "fos_rest.serializer.jms" has a dependency on a non-existent service "jms_serializer.serializer". – Arco Voltaico Jul 08 '18 at 23:29
  • Hmm it seems like the `JMSSerializerBundle` is not loaded since the `jms_serializer.serializer` service does not exist - have you checked that the bundle is loaded? – vstm Jul 09 '18 at 07:10
  • Well, I found the jms serializer on vendor folder. What else I need to check? – Arco Voltaico Jul 09 '18 at 10:56
  • Do you have an entry in `config/bundles.php` like `JMS\SerializerBundle\JMSSerializerBundle::class => ['all' => true],`? If not try to add it. – vstm Jul 09 '18 at 12:24
  • It's now working after adding the service.serializer to the fos_rest.yml and adding the bundle to the bundles.php. Maybe the flex recipe failed. Now I am aware that bundles.php is the SF4 loader of bundles instead of AppKernel. Please answer the question, and I will vote it. Thanks a lot. – Arco Voltaico Jul 09 '18 at 13:09
  • Did you resolve this issue with the following answer? Because I have the same but not resolved by the following answer. – clem Dec 17 '19 at 10:05

1 Answers1

2

By default FOSRest prefers the JMSSerializer if it is installed. So first check if the service defined by the JMSSerializerBundle is defined:

./bin/console debug:container jms_serializer.serializer

If this command displays an error message (ServiceNotFound) then the bundle is not correctly installed. Check the config/bundles.php and add the following line if it's missing:

JMS\SerializerBundle\JMSSerializerBundle::class => ['all' => true],

If it actually is installed, you can check the fos_rest configuration, if it maybe changes the serializer service. You can configure it like that:

fos_rest:   
    service:
        serializer: "fos_rest.serializer.jms"
vstm
  • 12,407
  • 1
  • 51
  • 47