Skip to content

Commit 2f1e276

Browse files
committed
minor #18879 [Serializer] Add #[Context] support on classes (alexandre-daubois)
This PR was merged into the 6.4 branch. Discussion ---------- [Serializer] Add `#[Context]` support on classes Fix #18865 Commits ------- 431c871 [Serializer] Add `#[Context]` support on classes
2 parents c9810bc + 431c871 commit 2f1e276

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

serializer.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,31 @@ The attribute can be repeated as much as needed on a single property.
248248
Context without group is always applied first. Then context for the matching
249249
groups are merged in the provided order.
250250

251+
If you don't want to put the same context on multiple properties, it is
252+
possible to use the ``#[Context]`` attribute directly on your class::
253+
254+
namespace App\Model;
255+
256+
use Symfony\Component\Serializer\Annotation\Context;
257+
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
258+
259+
#[Context([DateTimeNormalizer::FORMAT_KEY => \DateTime::RFC3339])]
260+
#[Context(
261+
context: [DateTimeNormalizer::FORMAT_KEY => \DateTime::RFC3339_EXTENDED],
262+
groups: ['extended'],
263+
)]
264+
class Person
265+
{
266+
// ...
267+
}
268+
269+
The defined context will apply on all the properties of your class.
270+
271+
.. versionadded:: 6.4
272+
273+
The support of the ``#[Context]`` attribute on classes was introduced in
274+
Symfony 6.4.
275+
251276
.. _serializer-using-context-builders:
252277

253278
Using Context Builders

0 commit comments

Comments
 (0)