Skip to content

[Serializer] Allow to provide (de)normalization context in mapping #17345

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
Oct 19, 2022
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions components/serializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,11 @@ normalized data, instead of the denormalizer re-creating them. Note that
arrays of objects. Those will still be replaced when present in the normalized
data.

Context
-------

Many Serializer features can be configured :doc:`using a context </serializer#serializer-context>`.

.. _component-serializer-attributes-groups:

Attributes Groups
Expand Down
106 changes: 106 additions & 0 deletions serializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,112 @@ configuration:
The ability to configure the ``default_context`` option in the
Serializer was introduced in Symfony 5.4.

You can also specify the context on a per-property basis::

.. configuration-block::

.. code-block:: php-annotations

namespace App\Model;

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

class Person
{
/**
* @Context({ DateTimeNormalizer::FORMAT_KEY = 'Y-m-d' })
*/
public $createdAt;

// ...
}

.. code-block:: php-attributes

namespace App\Model;

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

class Person
{
#[Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
public $createdAt;

// ...
}

.. code-block:: yaml

App\Model\Person:
attributes:
createdAt:
context:
datetime_format: 'Y-m-d'

.. code-block:: xml

<?xml version="1.0" encoding="UTF-8" ?>
<serializer xmlns="http://symfony.com/schema/dic/serializer-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/serializer-mapping
https://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd"
>
<class name="App\Model\Person">
<attribute name="createdAt">
<context>
<entry name="datetime_format">Y-m-d</entry>
</context>
</attribute>
</class>
</serializer>

Use the options to specify context specific to normalization or denormalization::

namespace App\Model;

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

class Person
{
#[Context(
normalizationContext: [DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'],
denormalizationContext: [DateTimeNormalizer::FORMAT_KEY => \DateTime::RFC3339],
)]
public $createdAt;

// ...
}

You can also restrict the usage of a context to some groups::

namespace App\Model;

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

class Person
{
#[Serializer\Groups(['extended'])]
#[Serializer\Context([DateTimeNormalizer::FORMAT_KEY => \DateTime::RFC3339])]
#[Serializer\Context(
context: [DateTimeNormalizer::FORMAT_KEY => \DateTime::RFC3339_EXTENDED],
groups: ['extended'],
)]
public $createdAt;

// ...
}

The attribute/annotation 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.

.. versionadded:: 5.3

The ``Context`` attribute, annotation and the configuration options were introduced in Symfony 5.3.

.. _serializer-using-serialization-groups-annotations:

Using Serialization Groups Annotations
Expand Down