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

Skip to content

Commit c764fcf

Browse files
committed
[Serializer] Allow Context to target class
1 parent 62e115f commit c764fcf

File tree

5 files changed

+37
-2
lines changed

5 files changed

+37
-2
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
* Annotation class for @Context().
1818
*
1919
* @Annotation
20+
*
2021
* @NamedArgumentConstructor
22+
*
2123
* @Target({"PROPERTY", "METHOD"})
2224
*
2325
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
2426
*/
25-
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
27+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
2628
class Context
2729
{
2830
private array $groups;

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
6.4
55
---
66

7+
* Allow `Context` attribute to target class
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

Lines changed: 13 additions & 0 deletions
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,14 @@ 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;
81+
82+
continue;
7483
}
7584
}
7685

@@ -81,6 +90,10 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool
8190
}
8291

8392
if ($property->getDeclaringClass()->name === $className) {
93+
if (null !== $classContextAnnotation) {
94+
$this->setAttributeContextsForGroups($annotation, $attributesMetadata[$property->name]);
95+
}
96+
8497
foreach ($classGroups as $group) {
8598
$attributesMetadata[$property->name]->addGroup($group);
8699
}

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

Lines changed: 19 additions & 0 deletions
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

@@ -133,6 +134,24 @@ 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 \DateTime
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
/**

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

Lines changed: 1 addition & 1 deletion
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
}

0 commit comments

Comments
 (0)
0