8000 [Serializer] Allow Context to target classes · symfony/symfony@caaf0a6 · GitHub
[go: up one dir, main page]

Skip to content

Commit caaf0a6

Browse files
mtarldnicolas-grekas
authored andcommitted
[Serializer] Allow Context to target classes
1 parent d8a4b3c commit caaf0a6

File tree

6 files changed

+38
-7
lines changed

6 files changed

+38
-7
lines changed

src/Symfony/Component/Serializer/Annotation/Context.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*
2323
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
2424
*/
25-
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
25+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
2626
class Context
2727
{
2828
private array $groups;

src/Symfony/Component/Serializer/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
6.4
55
---
66

7+
* Allow `Context` attribute to target classes
78
* Deprecate Doctrine annotations support in favor of native attributes
89
* Deprecate passing an annotation reader to the constructor of `AnnotationLoader`
910
* Allow the `Groups` attribute/annotation on classes

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

+11
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool
5757
$className = $reflectionClass->name;
5858
$loaded = false;
5959
$classGroups = [];
60+
$classContextAnnotation = null;
6061

6162
$attributesMetadata = $classMetadata->getAttributesMetadata();
6263

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

7273
if ($annotation instanceof Groups) {
7374
$classGroups = $annotation->getGroups();
75+
76+
continue;
77+
}
78+
79+
if ($annotation instanceof Context) {
80+
$classContextAnnotation = $annotation;
7481
}
7582
}
7683

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

8390
if ($property->getDeclaringClass()->name === $className) {
91+
if ($classContextAnnotation) {
92+
$this->setAttributeContextsForGroups($classContextAnnotation, $attributesMetadata[$property->name]);
93+
}
94+
8495
foreach ($classGroups as $group) {
8596
$attributesMetadata[$property->name]->addGroup($group);
8697
}

src/Symfony/Component/Serializer/Tests/Normalizer/Features/ContextMetadataTestTrait.php

+22-3
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public function contextMetadataDummyProvider(): array
8282
return [
8383
[ContextMetadataDummy::class],
8484
[ContextChildMetadataDummy::class],
85+
[ClassAndPropertyContextMetadataDummy::class],
8586
];
8687
}
8788

@@ -100,7 +101,7 @@ public function testContextDenormalizeWithNameConverter()
100101
class ContextMetadataDummy
101102
{
102103
/**
103-
* @var \DateTime
104+
* @var \DateTimeImmutable
104105
*/
105106
#[Groups(['extended', 'simple'])]
106107
#[Context([DateTimeNormalizer::FORMAT_KEY => \DateTimeInterface::RFC3339])]
@@ -118,7 +119,7 @@ class ContextMetadataDummy
118119
class ContextChildMetadataDummy
119120
{
120121
/**
121-
* @var \DateTime
122+
* @var \DateTimeImmutable
122123
*/
123124
#[Groups(['extended', 'simple'])]
124125
#[DummyContextChild([DateTimeNormalizer::FORMAT_KEY => \DateTimeInterface::RFC3339])]
@@ -133,10 +134,28 @@ class ContextChildMetadataDummy
133134
public $date;
134135
}
135136

137+
#[Context(context: [DateTimeNormalizer::FORMAT_KEY => \DateTimeInterface::RFC3339])]
138+
#[Context(
139+
context: [DateTimeNormalizer::FORMAT_KEY => \DateTimeInterface::RFC3339_EXTENDED],
140+
groups: ['extended'],
141+
)]
142+
class ClassAndPropertyContextMetadataDummy
143+
{
144+
/**
145+
* @var \DateTimeImmutable
146+
*/
147+
#[Groups(['extended', 'simple'])]
148+
#[Context(
149+
denormalizationContext: [DateTimeNormalizer::FORMAT_KEY => 'd/m/Y'],
150+
groups: ['simple'],
151+
)]
152+
public $date;
153+
}
154+
136155
class ContextMetadataNamingDummy
137156
{
138157
/**
139-
* @var \DateTime
158+
* @var \DateTimeImmutable
140159
*/
141160
#[Context([DateTimeNormalizer::FORMAT_KEY => 'd/m/Y'])]
142161
public $createdAt;

src/Symfony/Component/Serializer/Tests/Normalizer/Features/DummyContextChild.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use Symfony\Component\Serializer\Annotation\Context;
1515

16-
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
16+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
1717
class DummyContextChild extends Context
1818
{
1919
}

src/Symfony/Component/Serializer/Tests/SerializerTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1477,12 +1477,12 @@ public function __construct($value)
14771477
class DummyUnionType
14781478
{
14791479
/**
1480-
* @var \DateTime|bool|null
1480+
* @var \DateTimeImmutable|bool|null
14811481
*/
14821482
public $changed = false;
14831483

14841484
/**
1485-
* @param \DateTime|bool|null
1485+
* @param \DateTimeImmutable|bool|null
14861486
*
14871487
* @return $this
14881488
*/

0 commit comments

Comments
 (0)
0