Skip to content

Commit 7dcd023

Browse files
committed
minor #17345 [Serializer] Allow to provide (de)normalization context in mapping (ogizanagi)
This PR was merged into the 5.4 branch. Discussion ---------- [Serializer] Allow to provide (de)normalization context in mapping Fixes #14988 Commits ------- 800017d [Serializer] Allow to provide (de)normalization context in mapping
2 parents e7e142a + 800017d commit 7dcd023

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

components/serializer.rst

+5
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,11 @@ normalized data, instead of the denormalizer re-creating them. Note that
229229
arrays of objects. Those will still be replaced when present in the normalized
230230
data.
231231

232+
Context
233+
-------
234+
235+
Many Serializer features can be configured :doc:`using a context </serializer#serializer-context>`.
236+
232237
.. _component-serializer-attributes-groups:
233238

234239
Attributes Groups

serializer.rst

+106
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,112 @@ configuration:
172172
The ability to configure the ``default_context`` option in the
173173
Serializer was introduced in Symfony 5.4.
174174

175+
You can also specify the context on a per-property basis::
176+
177+
.. configuration-block::
178+
179+
.. code-block:: php-annotations
180+
181+
namespace App\Model;
182+
183+
use Symfony\Component\Serializer\Annotation\Context;
184+
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
185+
186+
class Person
187+
{
188+
/**
189+
* @Context({ DateTimeNormalizer::FORMAT_KEY = 'Y-m-d' })
190+
*/
191+
public $createdAt;
192+
193+
// ...
194+
}
195+
196+
.. code-block:: php-attributes
197+
198+
namespace App\Model;
199+
200+
use Symfony\Component\Serializer\Annotation\Context;
201+
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
202+
203+
class Person
204+
{
205+
#[Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
206+
public $createdAt;
207+
208+
// ...
209+
}
210+
211+
.. code-block:: yaml
212+
213+
App\Model\Person:
214+
attributes:
215+
createdAt:
216+
context:
217+
datetime_format: 'Y-m-d'
218+
219+
.. code-block:: xml
220+
221+
<?xml version="1.0" encoding="UTF-8" ?>
222+
<serializer xmlns="http://symfony.com/schema/dic/serializer-mapping"
223+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
224+
xsi:schemaLocation="http://symfony.com/schema/dic/serializer-mapping
225+
https://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd"
226+
>
227+
<class name="App\Model\Person">
228+
<attribute name="createdAt">
229+
<context>
230+
<entry name="datetime_format">Y-m-d</entry>
231+
</context>
232+
</attribute>
233+
</class>
234+
</serializer>
235+
236+
Use the options to specify context specific to normalization or denormalization::
237+
238+
namespace App\Model;
239+
240+
use Symfony\Component\Serializer\Annotation\Context;
241+
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
242+
243+
class Person
244+
{
245+
#[Context(
246+
normalizationContext: [DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'],
247+
denormalizationContext: [DateTimeNormalizer::FORMAT_KEY => \DateTime::RFC3339],
248+
)]
249+
public $createdAt;
250+
251+
// ...
252+
}
253+
254+
You can also restrict the usage of a context to some groups::
255+
256+
namespace App\Model;
257+
258+
use Symfony\Component\Serializer\Annotation\Context;
259+
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
260+
261+
class Person
262+
{
263+
#[Serializer\Groups(['extended'])]
264+
#[Serializer\Context([DateTimeNormalizer::FORMAT_KEY => \DateTime::RFC3339])]
265+
#[Serializer\Context(
266+
context: [DateTimeNormalizer::FORMAT_KEY => \DateTime::RFC3339_EXTENDED],
267+
groups: ['extended'],
268+
)]
269+
public $createdAt;
270+
271+
// ...
272+
}
273+
274+
The attribute/annotation can be repeated as much as needed on a single property.
275+
Context without group is always applied first. Then context for the matching groups are merged in the provided order.
276+
277+
.. versionadded:: 5.3
278+
279+
The ``Context`` attribute, annotation and the configuration options were introduced in Symfony 5.3.
280+
175281
.. _serializer-using-serialization-groups-annotations:
176282

177283
Using Serialization Groups Annotations

0 commit comments

Comments
 (0)