Skip to content

Commit

Permalink
bug #43208 [Serializer] Attributes that extend serializer`s annotatio…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
nicolas-grekas committed Sep 29, 2021
2 parents 61cf724 + e13f1be commit 5d7f068
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 8 deletions.
25 changes: 18 additions & 7 deletions Mapping/Loader/AnnotationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
class AnnotationLoader implements LoaderInterface
{
private const KNOWN_ANNOTATIONS = [
DiscriminatorMap::class => true,
Groups::class => true,
Ignore::class => true,
MaxDepth::class => true,
SerializedName::class => true,
Context::class => true,
DiscriminatorMap::class,
Groups::class,
Ignore::class,
MaxDepth::class,
SerializedName::class,
Context::class,
];

private $reader;
Expand Down Expand Up @@ -157,7 +157,7 @@ public function loadAnnotations(object $reflector): iterable
{
if (\PHP_VERSION_ID >= 80000) {
foreach ($reflector->getAttributes() as $attribute) {
if (self::KNOWN_ANNOTATIONS[$attribute->getName()] ?? false) {
if ($this->isKnownAttribute($attribute->getName())) {
yield $attribute->newInstance();
}
}
Expand Down Expand Up @@ -193,4 +193,15 @@ private function setAttributeContextsForGroups(Context $annotation, AttributeMet
$attributeMetadata->setDenormalizationContextForGroups($annotation->getDenormalizationContext(), $annotation->getGroups());
}
}

private function isKnownAttribute(string $attributeName): bool
{
foreach (self::KNOWN_ANNOTATIONS as $knownAnnotation) {
if (is_a($attributeName, $knownAnnotation, true)) {
return true;
}
}

return false;
}
}
16 changes: 16 additions & 0 deletions Tests/Fixtures/Annotations/GroupDummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummyInterface;
use Symfony\Component\Serializer\Tests\Fixtures\ChildOfGroupsAnnotationDummy;

/**
* @author Kévin Dunglas <dunglas@gmail.com>
Expand All @@ -27,6 +28,11 @@ class GroupDummy extends GroupDummyParent implements GroupDummyInterface
* @Groups({"b", "c", "name_converter"})
*/
protected $bar;
/**
* @ChildOfGroupsAnnotationDummy
*/
protected $quux;

private $fooBar;
private $symfony;

Expand Down Expand Up @@ -78,4 +84,14 @@ public function getSymfony()
{
return $this->symfony;
}

public function getQuux()
{
return $this->quux;
}

public function setQuux($quux): void
{
$this->quux = $quux;
}
}
13 changes: 13 additions & 0 deletions Tests/Fixtures/Attributes/GroupDummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Serializer\Tests\Fixtures\Attributes;

use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Tests\Fixtures\ChildOfGroupsAnnotationDummy;
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummyInterface;

/**
Expand All @@ -23,6 +24,8 @@ class GroupDummy extends GroupDummyParent implements GroupDummyInterface
private $foo;
#[Groups(["b", "c", "name_converter"])]
protected $bar;
#[ChildOfGroupsAnnotationDummy]
protected $quux;
private $fooBar;
private $symfony;

Expand Down Expand Up @@ -68,4 +71,14 @@ public function getSymfony()
{
return $this->symfony;
}

public function getQuux()
{
return $this->quux;
}

public function setQuux($quux): void
{
$this->quux = $quux;
}
}
18 changes: 18 additions & 0 deletions Tests/Fixtures/ChildOfGroupsAnnotationDummy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Symfony\Component\Serializer\Tests\Fixtures;

use Symfony\Component\Serializer\Annotation\Groups;

/**
* @Annotation
* @Target({"PROPERTY", "METHOD"})
*/
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)]
final class ChildOfGroupsAnnotationDummy extends Groups
{
public function __construct()
{
parent::__construct(['d']);
}
}
4 changes: 4 additions & 0 deletions Tests/Mapping/TestClassMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public static function createClassMetadata(string $namespace, bool $withParent =
$bar->addGroup('name_converter');
$expected->addAttributeMetadata($bar);

$quux = new AttributeMetadata('quux');
$quux->addGroup('d');
$expected->addAttributeMetadata($quux);

$fooBar = new AttributeMetadata('fooBar');
$fooBar->addGroup('a');
$fooBar->addGroup('b');
Expand Down
3 changes: 2 additions & 1 deletion Tests/Normalizer/PropertyNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,11 @@ public function testNormalizeWithParentClass()
$group->setBaz('baz');
$group->setFoo('foo');
$group->setBar('bar');
$group->setQuux('quux');
$group->setKevin('Kevin');
$group->setCoopTilleuls('coop');
$this->assertEquals(
['foo' => 'foo', 'bar' => 'bar', 'kevin' => 'Kevin', 'coopTilleuls' => 'coop', 'fooBar' => null, 'symfony' => null, 'baz' => 'baz'],
['foo' => 'foo', 'bar' => 'bar', 'quux' => 'quux', 'kevin' => 'Kevin', 'coopTilleuls' => 'coop', 'fooBar' => null, 'symfony' => null, 'baz' => 'baz'],
$this->normalizer->normalize($group, 'any')
);
}
Expand Down

0 comments on commit 5d7f068

Please sign in to comment.