Skip to content

feat(serialization): describe property context #1379

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
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
109 changes: 109 additions & 0 deletions core/serialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,115 @@ class Person

```

## Property Normalization Context

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,
you can do so by using the `#[Context]` attribute from the Symfony Serializer component.

For instance:

```php
<?php
// api/src/Entity/Book.php

declare(strict_types=1);

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Context;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;

/**
* @ORM\Entity
*/
#[ApiResource]
class Book
{
/**
* @ORM\Column(type="date")
*/
#[Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
public ?\DateTimeInterface $publicationDate = null;
}
```

In the above example, you will receive the book's data like this:

```json
{
"@context": "/contexts/Book",
"@id": "/books/3",
"@type": "http://schema.org/Book",
"publicationDate": "1989-06-16"
}
```

It's also possible to only change the denormalization or normalization context:

```php
<?php
// api/src/Entity/Book.php

declare(strict_types=1);

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Context;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;

/**
* @ORM\Entity
*/
#[ApiResource]
class Book
{
/**
* @ORM\Column(type="date")
*/
#[Context(normalizationContext: [DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
public ?\DateTimeInterface $publicationDate = null;
}
```

Groups are also supported:

```php
<?php
// api/src/Entity/Book.php

declare(strict_types=1);

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Context;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;

/**
* @ORM\Entity
*/
#[ApiResource]
class Book
{
/**
* @ORM\Column(type="date")
*/
#[Groups(["extended"])]
#[Context([DateTimeNormalizer::FORMAT_KEY => \DateTime::RFC3339])]
#[Context(
context: [DateTimeNormalizer::FORMAT_KEY => \DateTime::RFC3339_EXTENDED],
groups: ['extended'],
)]
public ?\DateTimeInterface $publicationDate = null;
}
```

## Calculated Field

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.
Expand Down