Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
onatskyy opened this issue Sep 28, 2021 · 0 comments

Comments

@onatskyy
Copy link

onatskyy commented Sep 28, 2021

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

@onatskyy onatskyy added the Bug label Sep 28, 2021
nicolas-grekas added a commit that referenced this issue Sep 29, 2021
…ns are not ignored by the serialization process (Alexander Onatskiy)

This PR was merged into the 5.3 branch.

Discussion
----------

[Serializer] Attributes that extend serializer`s annotations are not ignored by the serialization process

| Q             | A
| ------------- | ---
| Branch?       | 5.3 for bug fixes
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #43207
| License       | MIT
| Doc PR        |

Commits
-------

3407c35 [Serializer] Attributes that extends the serializer`s annotations do not ignore by the serialization process
symfony-splitter pushed a commit to symfony/serializer that referenced this issue Sep 30, 2021
…ns are not ignored by the serialization process (Alexander Onatskiy)

This PR was merged into the 5.3 branch.

Discussion
----------

[Serializer] Attributes that extend serializer`s annotations are not ignored by the serialization process

| Q             | A
| ------------- | ---
| Branch?       | 5.3 for bug fixes
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix symfony/symfony#43207
| License       | MIT
| Doc PR        |

Commits
-------

3407c350e1 [Serializer] Attributes that extends the serializer`s annotations do not ignore by the serialization process
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants