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

Commit 545fc34

Browse filesBrowse files
bug #43208 [Serializer] Attributes that extend serializer`s annotations 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
2 parents d121089 + 3407c35 commit 545fc34
Copy full SHA for 545fc34

File tree

6 files changed

+71
-8
lines changed
Filter options

6 files changed

+71
-8
lines changed

‎src/Symfony/Component/Serializer/Mapping/Loader/AnnotationLoader.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Mapping/Loader/AnnotationLoader.php
+18-7Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@
3333
class AnnotationLoader implements LoaderInterface
3434
{
3535
private const KNOWN_ANNOTATIONS = [
36-
DiscriminatorMap::class => true,
37-
Groups::class => true,
38-
Ignore::class => true,
39-
MaxDepth::class => true,
40-
SerializedName::class => true,
41-
Context::class => true,
36+
DiscriminatorMap::class,
37+
Groups::class,
38+
Ignore::class,
39+
MaxDepth::class,
40+
SerializedName::class,
41+
Context::class,
4242
];
4343

4444
private $reader;
@@ -157,7 +157,7 @@ public function loadAnnotations(object $reflector): iterable
157157
{
158158
if (\PHP_VERSION_ID >= 80000) {
159159
foreach ($reflector->getAttributes() as $attribute) {
160-
if (self::KNOWN_ANNOTATIONS[$attribute->getName()] ?? false) {
160+
if ($this->isKnownAttribute($attribute->getName())) {
161161
yield $attribute->newInstance();
162162
}
163163
}
@@ -193,4 +193,15 @@ private function setAttributeContextsForGroups(Context $annotation, AttributeMet
193193
$attributeMetadata->setDenormalizationContextForGroups($annotation->getDenormalizationContext(), $annotation->getGroups());
194194
}
195195
}
196+
197+
private function isKnownAttribute(string $attributeName): bool
198+
{
199+
foreach (self::KNOWN_ANNOTATIONS as $knownAnnotation) {
200+
if (is_a($attributeName, $knownAnnotation, true)) {
201+
return true;
202+
}
203+
}
204+
205+
return false;
206+
}
196207
}

‎src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/GroupDummy.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/GroupDummy.php
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Serializer\Annotation\Groups;
1515
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummyInterface;
16+
use Symfony\Component\Serializer\Tests\Fixtures\ChildOfGroupsAnnotationDummy;
1617

1718
/**
1819
* @author Kévin Dunglas <dunglas@gmail.com>
@@ -27,6 +28,11 @@ class GroupDummy extends GroupDummyParent implements GroupDummyInterface
2728
* @Groups({"b", "c", "name_converter"})
2829
*/
2930
protected $bar;
31+
/**
32+
* @ChildOfGroupsAnnotationDummy
33+
*/
34+
protected $quux;
35+
3036
private $fooBar;
3137
private $symfony;
3238

@@ -78,4 +84,14 @@ public function getSymfony()
7884
{
7985
return $this->symfony;
8086
}
87+
88+
public function getQuux()
89+
{
90+
return $this->quux;
91+
}
92+
93+
public function setQuux($quux): void
94+
{
95+
$this->quux = $quux;
96+
}
8197
}

‎src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/GroupDummy.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/GroupDummy.php
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Serializer\Tests\Fixtures\Attributes;
1313

1414
use Symfony\Component\Serializer\Annotation\Groups;
15+
use Symfony\Component\Serializer\Tests\Fixtures\ChildOfGroupsAnnotationDummy;
1516
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummyInterface;
1617

1718
/**
@@ -23,6 +24,8 @@ class GroupDummy extends GroupDummyParent implements GroupDummyInterface
2324
private $foo;
2425
#[Groups(["b", "c", "name_converter"])]
2526
protected $bar;
27+
#[ChildOfGroupsAnnotationDummy]
28+
protected $quux;
2629
private $fooBar;
2730
private $symfony;
2831

@@ -68,4 +71,14 @@ public function getSymfony()
6871
{
6972
return $this->symfony;
7073
}
74+
75+
public function getQuux()
76+
{
77+
return $this->quux;
78+
}
79+
80+
public function setQuux($quux): void
81+
{
82+
$this->quux = $quux;
83+
}
7184
}
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Symfony\Component\Serializer\Tests\Fixtures;
4+
5+
use Symfony\Component\Serializer\Annotation\Groups;
6+
7+
/**
8+
* @Annotation
9+
* @Target({"PROPERTY", "METHOD"})
10+
*/
11+
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)]
12+
final class ChildOfGroupsAnnotationDummy extends Groups
13+
{
14+
public function __construct()
15+
{
16+
parent::__construct(['d']);
17+
}
18+
}

‎src/Symfony/Component/Serializer/Tests/Mapping/TestClassMetadataFactory.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Mapping/TestClassMetadataFactory.php
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ public static function createClassMetadata(string $namespace, bool $withParent =
3333
$bar->addGroup('name_converter');
3434
$expected->addAttributeMetadata($bar);
3535

36+
$quux = new AttributeMetadata('quux');
37+
$quux->addGroup('d');
38+
$expected->addAttributeMetadata($quux);
39+
3640
$fooBar = new AttributeMetadata('fooBar');
3741
$fooBar->addGroup('a');
3842
$fooBar->addGroup('b');

‎src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,11 @@ public function testNormalizeWithParentClass()
115115
$group->setBaz('baz');
116116
$group->setFoo('foo');
117117
$group->setBar('bar');
118+
$group->setQuux('quux');
118119
$group->setKevin('Kevin');
119120
$group->setCoopTilleuls('coop');
120121
$this->assertEquals(
121-
['foo' => 'foo', 'bar' => 'bar', 'kevin' => 'Kevin', 'coopTilleuls' => 'coop', 'fooBar' => null, 'symfony' => null, 'baz' => 'baz'],
122+
['foo' => 'foo', 'bar' => 'bar', 'quux' => 'quux', 'kevin' => 'Kevin', 'coopTilleuls' => 'coop', 'fooBar' => null, 'symfony' => null, 'baz' => 'baz'],
122123
$this->normalizer->normalize($group, 'any')
123124
);
124125
}

0 commit comments

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