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 extend serializer`s annotations are not ignored by the serialization process #43208

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

Merged
merged 1 commit into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
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;
}
}
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;
}
}
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']);
}
}
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
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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.