Skip to content

Commit dfcec29

Browse files
committed
[Serializer] Allow Context to target class
1 parent d8a4b3c commit dfcec29

File tree

5 files changed

+35
-2
lines changed

5 files changed

+35
-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: 11 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,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 (null !== $classContextAnnotation) {
92+
$this->setAttributeContextsForGroups($annotation, $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

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy