8000 [Serializer] Allow Context to target classes by mtarld · Pull Request #51476 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
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
2 changes: 1 addition & 1 deletion src/Symfony/Component/Serializer/Annotation/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
*/
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class Context
{
private array $groups;
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Serializer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
6.4
---

* Allow `Context` attribute to target classes
* Deprecate Doctrine annotations support in favor of native attributes
* Deprecate passing an annotation reader to the constructor of `AnnotationLoader`
* Allow the `Groups` attribute/annotation on classes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool
$className = $reflectionClass->name;
$loaded = false;
$classGroups = [];
$classContextAnnotation = null;

$attributesMetadata = $classMetadata->getAttributesMetadata();

Expand All @@ -71,6 +72,12 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool

if ($annotation instanceof Groups) {
$classGroups = $annotation->getGroups();

continue;
}

if ($annotation instanceof Context) {
$classContextAnnotation = $annotation;
}
}

Expand All @@ -81,6 +88,10 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool
}

if ($property->getDeclaringClass()->name === $className) {
if ($classContextAnnotation) {
$this->setAttributeContextsForGroups($classContextAnnotation, $attributesMetadata[$property->name]);
}

foreach ($classGroups as $group) {
$attributesMetadata[$property->name]->addGroup($group);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public function contextMetadataDummyProvider(): array
return [
[ContextMetadataDummy::class],
[ContextChildMetadataDummy::class],
[ClassAndPropertyContextMetadataDummy::class],
];
}

Expand All @@ -100,7 +101,7 @@ public function testContextDenormalizeWithNameConverter()
class ContextMetadataDummy
{
/**
* @var \DateTime
* @var \DateTimeImmutable
*/
#[Groups(['extended', 'simple'])]
#[Context([DateTimeNormalizer::FORMAT_KEY => \DateTimeInterface::RFC3339])]
Expand All @@ -118,7 +119,7 @@ class ContextMetadataDummy
class ContextChildMetadataDummy
{
/**
* @var \DateTime
* @var \DateTimeImmutable
*/
#[Groups(['extended', 'simple'])]
#[DummyContextChild([DateTimeNormalizer::FORMAT_KEY => \DateTimeInterface::RFC3339])]
Expand All @@ -133,10 +134,28 @@ class ContextChildMetadataDummy
public $date;
}

#[Context(context: [DateTimeNormalizer::FORMAT_KEY => \DateTimeInterface::RFC3339])]
#[Context(
context: [DateTimeNormalizer::FORMAT_KEY => \DateTimeInterface::RFC3339_EXTENDED],
groups: ['extended'],
)]
class ClassAndPropertyContextMetadataDummy
{
/**
* @var \DateTimeImmutable
*/
#[Groups(['extended', 'simple'])]
#[Context(
denormalizationContext: [DateTimeNormalizer::FORMAT_KEY => 'd/m/Y'],
groups: ['simple'],
)]
public $date;
}

class ContextMetadataNamingDummy
{
/**
* @var \DateTime
* @var \DateTimeImmutable
*/
#[Context([DateTimeNormalizer::FORMAT_KEY => 'd/m/Y'])]
public $createdAt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use Symfony\Component\Serializer\Annotation\Context;

#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
class DummyContextChild extends Context
{
}
4 changes: 2 additions & 2 deletions src/Symfony/Component/Serializer/Tests/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1477,12 +1477,12 @@ public function __construct($value)
class DummyUnionType
{
/**
* @var \DateTime|bool|null
* @var \DateTimeImmutable|bool|null
*/
public $changed = false;

/**
* @param \DateTime|bool|null
* @param \DateTimeImmutable|bool|null
*
* @return $this
*/
Expand Down
0