Description
Symfony version(s) affected: >=5.3
Description
Classes for serialization, that are configured via Attributes, have different behavior from classes configured via doctrine annotations if these annotations||attributes are children of serializer`s annotation.
How to reproduce
F.e. I have some set of serialization groups, that I want to use many times. For it, I create a new class that extends Symfony\Component\Serializer\Annotation\Groups
and set up needed groups in __construct
and mark this class like DoctrineAnnotation and Attribute.
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @Annotation
* @Target({"PROPERTY", "METHOD"})
*/
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)]
final class SomeSetOfGroups extends Groups
{
public function __construct()
{
parent::__construct(['foo', 'bar', 'baz']);
}
}
Next, I create 2 models with the same fields, 1st configured via the doctrine annotation, 2nd configured via attributes
use SomeSetOfGroups;
use Symfony\Component\Serializer\Annotation\Groups;
class A
{
/**
* @SomeSetOfGroups
*/
public string $filed1 = 'value1';
/**
* @Groups("foo")
*/
public string $filed2 = 'value2';
}
use SomeSetOfGroups;
use Symfony\Component\Serializer\Annotation\Groups;
class B
{
#[SomeSetOfGroups]
public string $filed1 = 'value1';
#[Groups('foo')]
public string $filed2 = 'value2';
}
And I serialize both models, expecting that results will be equals. But results are different
$serializer = $container()->get('serializer');
$resultA = $serializer->serialize(new A(), 'json', ['groups' => 'foo']); // {"filed1":"value1","filed2":"value2"}
$resultB = $serializer->serialize(new B(), 'json', ['groups' => 'foo']); // {"filed2":"value2"}
These happened, because AnnotationLoader ignore all attributes except the base, even if these attributes extend base