Skip to content

[Serializer] Add #[Context] support on classes #18879

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 19, 2023
Merged
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
25 changes: 25 additions & 0 deletions serializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,31 @@ The attribute can be repeated as much as needed on a single property.
Context without group is always applied first. Then context for the matching
groups are merged in the provided order.

If you don't want to put the same context on multiple properties, it is
possible to use the ``#[Context]`` attribute directly on your class::

namespace App\Model;

use Symfony\Component\Serializer\Annotation\Context;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;

#[Context([DateTimeNormalizer::FORMAT_KEY => \DateTime::RFC3339])]
#[Context(
context: [DateTimeNormalizer::FORMAT_KEY => \DateTime::RFC3339_EXTENDED],
groups: ['extended'],
)]
class Person
{
// ...
}

The defined context will apply on all the properties of your class.

.. versionadded:: 6.4

The support of the ``#[Context]`` attribute on classes was introduced in
Symfony 6.4.

.. _serializer-using-context-builders:

Using Context Builders
Expand Down