Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Serializer] Attributes that extends the serializer`s annotations are ignored by the serialization process, while doctrine annotations processed #43207

Copy link
Copy link
Closed
@onatskyy

Description

@onatskyy
Issue body actions

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.