FriendsOfSymfony / FOSRestBundle

This Bundle provides various tools to rapidly develop RESTful API's with Symfony
http://symfony.com/doc/master/bundles/FOSRestBundle/index.html
MIT License
2.79k stars 708 forks source link

Warning: Invalid argument supplied for foreach() in /vendor/jms/serializer/src/Exclusion/GroupsExclusionStrategy.php on line 67 #2363

Closed cywbl closed 2 years ago

cywbl commented 2 years ago

Hi, I am using JMS Serializer with FOS REST Bundle... And I cannot figure out what is causing this warning when I run my phpunit tests...

Warning: Invalid argument supplied for foreach() in /vendor/jms/serializer/src/Exclusion/GroupsExclusionStrategy.php on line 67

I've checked the library versions (fos rest bundle 3.2, jms serializer-bundle 3.5) and the documentation for both, and cannot figure out what is going wrong. Inside the entity to be serialized, the annotations look like this for example:

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     * @JMS\Groups({"Details", "List"})
     */
    private string $name = '';

And inside the controller, using it like this:

        $context = new Context();
        $context->addGroup('Details');

        $view->setContext($context);
        return $this->handleView($view);

The last line there (handleView) is what is ostensibly triggering the warning. Please help :disappointed:

cywbl commented 2 years ago

Resolved! Elsewhere in the class, there was an annotation like this:

@JMS\Groups("Details")

And it needed to be fixed like this:

@JMS\Groups({"Details"})

Closing this issue, but I wonder I can help update the JMS docs to make it clearer that it must always take a list, never just a string https://jmsyst.com/libs/serializer/master/reference/annotations#groups