Skip to content

Commit f6fddb9

Browse files
authored
feat(serialization): describe property context (#1379)
1 parent 599ae03 commit f6fddb9

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

core/serialization.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,115 @@ class Person
408408
409409
```
410410

411+
## Property Normalization Context
412+
413+
If you want to change the (de)normalization context of a property, for instance if you want to change the format of the date time,
414+
you can do so by using the `#[Context]` attribute from the Symfony Serializer component.
415+
416+
For instance:
417+
418+
```php
419+
<?php
420+
// api/src/Entity/Book.php
421+
422+
declare(strict_types=1);
423+
424+
namespace App\Entity;
425+
426+
use ApiPlatform\Core\Annotation\ApiResource;
427+
use Doctrine\ORM\Mapping as ORM;
428+
use Symfony\Component\Serializer\Annotation\Context;
429+
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
430+
431+
/**
432+
* @ORM\Entity
433+
*/
434+
#[ApiResource]
435+
class Book
436+
{
437+
/**
438+
* @ORM\Column(type="date")
439+
*/
440+
#[Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
441+
public ?\DateTimeInterface $publicationDate = null;
442+
}
443+
```
444+
445+
In the above example, you will receive the book's data like this:
446+
447+
```json
448+
{
449+
"@context": "/contexts/Book",
450+
"@id": "/books/3",
451+
"@type": "http://schema.org/Book",
452+
"publicationDate": "1989-06-16"
453+
}
454+
```
455+
456+
It's also possible to only change the denormalization or normalization context:
457+
458+
```php
459+
<?php
460+
// api/src/Entity/Book.php
461+
462+
declare(strict_types=1);
463+
464+
namespace App\Entity;
465+
466+
use ApiPlatform\Core\Annotation\ApiResource;
467+
use Doctrine\ORM\Mapping as ORM;
468+
use Symfony\Component\Serializer\Annotation\Context;
469+
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
470+
471+
/**
472+
* @ORM\Entity
473+
*/
474+
#[ApiResource]
475+
class Book
476+
{
477+
/**
478+
* @ORM\Column(type="date")
479+
*/
480+
#[Context(normalizationContext: [DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
481+
public ?\DateTimeInterface $publicationDate = null;
482+
}
483+
```
484+
485+
Groups are also supported:
486+
487+
```php
488+
<?php
489+
// api/src/Entity/Book.php
490+
491+
declare(strict_types=1);
492+
493+
namespace App\Entity;
494+
495+
use ApiPlatform\Core\Annotation\ApiResource;
496+
use Doctrine\ORM\Mapping as ORM;
497+
use Symfony\Component\Serializer\Annotation\Context;
498+
use Symfony\Component\Serializer\Annotation\Groups;
499+
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
500+
501+
/**
502+
* @ORM\Entity
503+
*/
504+
#[ApiResource]
505+
class Book
506+
{
507+
/**
508+
* @ORM\Column(type="date")
509+
*/
510+
#[Groups(["extended"])]
511+
#[Context([DateTimeNormalizer::FORMAT_KEY => \DateTime::RFC3339])]
512+
#[Context(
513+
context: [DateTimeNormalizer::FORMAT_KEY => \DateTime::RFC3339_EXTENDED],
514+
groups: ['extended'],
515+
)]
516+
public ?\DateTimeInterface $publicationDate = null;
517+
}
518+
```
519+
411520
## Calculated Field
412521

413522
Sometimes you need to expose calculated fields. This can be done by leveraging the groups. This time not on a property, but on a method.

0 commit comments

Comments
 (0)