NelmioApiDocBundle
NelmioApiDocBundle copied to clipboard
Problems with FOSUserBundle - Property "AppBundle\Entity\User:roles" is an array
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.
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?
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?
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 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!
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.
Same problem any solution ?
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
@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?
have same problems, but not only for roles. for EquatableInterface
interface too and many others
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.
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.