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

Annotation Group not working on Entity using Symfony 4 #669

Closed
arcovoltaico opened this issue Jul 7, 2018 · 1 comment
Closed

Annotation Group not working on Entity using Symfony 4 #669

arcovoltaico opened this issue Jul 7, 2018 · 1 comment

Comments

@arcovoltaico
Copy link

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!

@arcovoltaico
Copy link
Author

The installation did not include the loader on config/bundles.php so I added this manually:
JMS\SerializerBundle\JMSSerializerBundle::class => ['all' => true]
Also I needed to add this to my config yml.
fos_rest:
service:
serializer: fos_rest.serializer.jms

So now everything is working fine :-)

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

1 participant