NelmioApiDocBundle icon indicating copy to clipboard operation
NelmioApiDocBundle copied to clipboard

Problems with FOSUserBundle - Property "AppBundle\Entity\User:roles" is an array

Open StefanGla opened this issue 6 years ago • 11 comments

I use FOSUserBundle and have a very little user class:

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ORM\Entity
 * @ORM\Table(name="users")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @Groups("test")
     * @ORM\Column(name="api_auth", type="string", length=32)
     */
    private $apiAuth;
    ...

In my api-controller I reference to this user class as follow:

class ApiController extends Controller
{
    /**
     * List user specific details by id
     *
     * @Route("/api/user/login", methods={"POST"})
     * @SWG\Response(
     *     response=200,
     *     description="user api authorization",
     *     @Model(type=User::class, groups={"test"})
     * )
     * @SWG\Parameter(
     *     name="login",
     *     in="body",
     *     description="Login-Object",
     *     required=true,
     *     @Model(type=UserLogin::class)
     * )
     * @SWG\Tag(name="user")
     * @Security(name="Bearer")
     */
    public function userLoginAction(UserLogin $userLogin)
    {
      ...
    }

In the end I got the following error: Uncaught PHP Exception LogicException: "Property "AppBundle\Entity\User:roles" is an array, but no indication of the array elements are made. Use e.g. string[] for an array of string." at ../vendor/nelmio/api-doc-bundle/ModelDescriber/ObjectModelDescriber.php line 92

How can I fix that? I only would like to return one specific field (apiAuth) of the user class.

StefanGla avatar Jun 07 '18 11:06 StefanGla

I got the same problem... Moreover, i've used and annotation to specify that the field i have in the entity is an array of string but seems not working... Has anybody found a solution for this?

locojuhi avatar Jul 18 '18 20:07 locojuhi

And another one with the exact same problem. I tried to overwrite this property in my own class with:

    /**
     * @var string[]
     */
    protected $roles;

but the problem persisted. Is there probably an annotation option to overwrite a specific property?

kevinpapst avatar Aug 01 '18 22:08 kevinpapst

Not sure what's the issue and I can't dedicate it time to investigate unfortunately, meanwhile you can use @SWG\Property, see https://symfony.com/doc/current/bundles/NelmioApiDocBundle/index.html#general-php-objects.

GuilhemN avatar Aug 08 '18 15:08 GuilhemN

@GuilhemN That error went away after switching from Symfony serializer to JMSSerializer and hard deletion of the cache via rm -rf var/cache/* instead of using the Symphony cache:clear command (later one didn't remove the cache completely).

BTW for me that happened with the combination of Symfony 4.1.3 / FOSUserBundle 2.1.2 / FOSRestBundle 2.3.1

And thanks for your bundle, I really appreciate your work!

kevinpapst avatar Aug 08 '18 18:08 kevinpapst

meanwhile you can use @SWG\Property

The issue come from FOSUser model:

    /**
     * @var array
     */
    protected $roles;

We can't add an annotation on a vendor class.

soullivaneuh avatar Oct 09 '18 08:10 soullivaneuh

Same problem any solution ?

fleox avatar Nov 16 '18 18:11 fleox

Even if you are in dev environment, the cache will not be automatically cleared after just adding an annotation /** @var string[] */ so remember to php bin/console cache:clear use app/console for symfony < 3.0

adrianles avatar Nov 23 '18 08:11 adrianles

@fleox kevinpapst 's solution works. And in my case, I just installed composer require jms/serializer-bundle and even without do cache:clear, it just works.

And does that mean actually it rely on the serializer for outputting doc string?

yarcowang avatar May 01 '19 15:05 yarcowang

have same problems, but not only for roles. for EquatableInterface interface too and many others

Gemorroj avatar Feb 05 '21 14:02 Gemorroj

You may try using the recent @Ignore annotation (and it's equivalent in yaml/xml), see https://symfony.com/doc/current/components/serializer.html#option-1-using-ignore-annotation.

We could also consider adding support of the AbstractNormalizer::IGNORED_ATTRIBUTES context field in the @Model annotation if you think it would help.

GuilhemN avatar Feb 08 '21 21:02 GuilhemN

This is coming up a few times in issues i've been triaging where the class hierarchy of things seems to cause issues with this bundle reading properties that we'd not expect it to read.

For what it's worth, having a layer of DTOs between domain object (models) and API output has prevented me from seeing a lot of these issues in projects: example.

chrisguitarguy avatar Mar 26 '22 20:03 chrisguitarguy