serializer icon indicating copy to clipboard operation
serializer copied to clipboard

AccessType attribute constructor expects array

Open SimonTod opened this issue 1 year ago • 3 comments

Q A
Bug report? yes
Feature request? no
BC Break report? yes
RFC? no

As declared in the documentation, the next annotation works

use JMS\Serializer\Annotation\AccessType;

/** @AccessType("public_method") */
class User { }

But I could not make it work using the php attribute

use JMS\Serializer\Annotation\AccessType;

#[AccessType('public_method')]
class User { }

Error thrown :

Uncaught Error: JMS\Serializer\Annotation\AccessType::__construct(): Argument #1 ($values) must be of type array, string given

As we can see in https://github.com/schmittjoh/serializer/blob/master/src/Annotation/AccessType.php#L24 :

    public function __construct(array $values = [], ?string $type = null)

SimonTod avatar Jan 03 '24 17:01 SimonTod

I fixed the issue with the next syntax :

use JMS\Serializer\Annotation\AccessType;

#[AccessType(type: 'public_method')]
class User { }

I think it would be a good thing to update the documentation

SimonTod avatar Jan 04 '24 08:01 SimonTod

Thanks for feedback! I think it is time to make docs Attributes first :)

scyzoryck avatar Jan 08 '24 16:01 scyzoryck

There is a small section about it : Converting your annotations to attributes

SimonTod avatar Jan 08 '24 17:01 SimonTod