Skip to content
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

Update serialization.md #1490

Open
wants to merge 9 commits into
base: 3.4
Choose a base branch
from
Open
Changes from 5 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
48 changes: 48 additions & 0 deletions core/serialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,54 @@ feature of the Symfony Serializer component.
In addition to groups, you can use any option supported by the Symfony Serializer. For example, you can use [`enable_max_depth`](https://symfony.com/doc/current/components/serializer.html#handling-serialization-depth)
to limit the serialization depth.

```php
vincentchalamon marked this conversation as resolved.
Show resolved Hide resolved
<?php
// api/src/Entity/Book.php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\MaxDepth;
Comment on lines +52 to +54
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend to target 4.0 branch instead of 3.4, and update the imports as following:

Suggested change
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\MaxDepth;
use ApiPlatform\Metadata\ApiResource;
use Symfony\Component\Serializer\Attribute\Groups;
use Symfony\Component\Serializer\Attribute\MaxDepth;


#[ApiResource(
normalizationContext: ['groups' => 'read', 'enable_max_depth' => true],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
normalizationContext: ['groups' => 'read', 'enable_max_depth' => true],
normalizationContext: ['groups' => ['read'], 'enable_max_depth' => true],

denormalizationContext: ['groups' => ['write']],
)]
class Book
{
#[Groups(["read", "write"])]
public $name;

#[Groups("write")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[Groups("write")]
#[Groups(["write"])]

#[MaxDepth(1)]
public $author;

// ...
}
```

```yaml
# api/config/api_platform/resources.yaml
resources:
App\Entity\Book:
attributes:
normalization_context:
groups: ['read']
enable_max_depth: true
denormalization_context:
groups: ['write']

# api/config/serialization/Book.yaml

App\Entity\Book:
properties:
author:
groups: ['write']
max_depth: 1
```
vincentchalamon marked this conversation as resolved.
Show resolved Hide resolved


### Configuration

Just like other Symfony and API Platform components, the Serializer component can be configured using annotations, XML
Expand Down