Skip to content

Update annotations of Serialization #1267

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 2 commits into from
Feb 2, 2021
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
128 changes: 52 additions & 76 deletions core/serialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,10 @@ namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ApiResource(
* normalizationContext={"groups"={"read"}},
* denormalizationContext={"groups"={"write"}}
* )
*/
#[ApiResource(
normalizationContext: ['groups' => ['read']],
denormalizationContext: ['groups' => ['write']],
)]
class Book
{
/**
Expand Down Expand Up @@ -134,12 +132,10 @@ Alternatively, you can use the more verbose syntax:
<?php
// ...
/**
* @ApiResource(attributes={
* "normalization_context"={"groups"={"read"}},
* "denormalization_context"={"groups"={"write"}}
* })
*/
#[ApiResource(attributes: [
'normalization_context' => ['groups' => ['read']],
'denormalization_context' => ['groups' => ['write']],
])]
```

In the previous example, the `name` property will be visible when reading (`GET`) the object, and it will also be available
Expand Down Expand Up @@ -175,17 +171,15 @@ namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ApiResource(
* normalizationContext={"groups"={"get"}},
* itemOperations={
* "get",
* "put"={
* "normalization_context"={"groups"={"put"}}
* }
* }
* )
*/
#[ApiResource(
normalizationContext: ['groups' => ['get']],
itemOperations: [
'get',
'put' => [
'normalization_context' => ['groups' => ['put']],
],
],
)]
class Book
{
/**
Expand Down Expand Up @@ -244,9 +238,7 @@ namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ApiResource(normalizationContext={"groups"={"book"}})
*/
#[ApiResource(normalizationContext: ['groups' => ['book']])]
class Book
{
/**
Expand All @@ -272,9 +264,7 @@ namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ApiResource
*/
#[ApiResource]
class Person
{
/**
Expand Down Expand Up @@ -322,9 +312,7 @@ namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
/**
* @ApiResource(denormalizationContext={"groups"={"book"}})
*/
#[ApiResource(denormalizationContext: ['groups' => ['book']])]
class Book
{
// ...
Expand Down Expand Up @@ -352,16 +340,14 @@ namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ApiResource(
* normalizationContext = {
* "groups" = {"person"}
* },
* denormalizationContext = {
* "groups" = {"person"}
* }
* )
*/
#[ApiResource(
normalizationContext: [
'groups' => ['person'],
],
denormalizationContext: [
'groups' => ['person'],
],
)]
class Person
{
/**
Expand All @@ -383,7 +369,7 @@ class Person

The problem here is that the **$parent** property become automatically an embedded object. Besides, the property won't be shown on the OpenAPI view.

To force the **$parent** property to be used as an IRI, add an **@ApiProperty(readableLink=false, writableLink=false)** annotation:
To force the **$parent** property to be used as an IRI, add an **#[ApiProperty(readableLink: false, writableLink: false)]** annotation:

```php
<?php
Expand All @@ -394,16 +380,14 @@ namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ApiResource(
* normalizationContext = {
* "groups" = {"person"}
* },
* denormalizationContext = {
* "groups" = {"person"}
* }
* )
*/
#[ApiResource(
normalizationContext: [
'groups' => ['person'],
],
denormalizationContext: [
'groups' => ['person'],
],
)]
class Person
{
/**
Expand All @@ -415,8 +399,8 @@ class Person
/**
* @var Person
* @Groups("person")
* @ApiProperty(readableLink=false, writableLink=false)
*/
#[ApiProperty(readableLink: false, writableLink: false)]
public $parent; // This property is now serialized/deserialized as an IRI.
// ...
Expand All @@ -440,13 +424,13 @@ use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ApiResource(
* collectionOperations={
* "get"={"normalization_context"={"groups"="greeting:collection:get"}},
* }
* )
* @ORM\Entity
*/
#[ApiResource(
collectionOperations: [
'get' => ['normalization_context' => ['groups' => 'greeting:collection:get']],
],
)]
class Greeting
{
/**
Expand Down Expand Up @@ -501,12 +485,10 @@ namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ApiResource(
* normalizationContext={"groups"={"book:output"}},
* denormalizationContext={"groups"={"book:input"}}
* )
*/
#[ApiResource(
normalizationContext: ['groups' => ['book:output']],
denormalizationContext: ['groups' => ['book:input']],
)]
class Book
{
// ...
Expand Down Expand Up @@ -783,16 +765,12 @@ If you are not using the Doctrine ORM or MongoDB ODM Provider, you must explicit
the `ApiPlatform\Core\Annotation\ApiProperty` annotation. For example:

```php
/**
* @ApiResource()
*/
#[ApiResource]
class Book
{
// ...
/**
* @ApiProperty(identifier=true)
*/
#[ApiProperty(identifier: true)]
private $id;
/**
Expand Down Expand Up @@ -848,7 +826,7 @@ an IRI. A client that uses JSON-LD must send a second HTTP request to retrieve i
```

You can configure API Platform to embed the JSON-LD context in the root document by adding the `jsonld_embed_context`
attribute to the `@ApiResource` annotation:
attribute to the `#[ApiResource]` annotation:

```php
<?php
Expand All @@ -858,9 +836,7 @@ namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
/**
* @ApiResource(normalizationContext={"jsonld_embed_context"=true})
*/
#[ApiResource(normalizationContext: ['jsonld_embed_context' => true])]
class Book
{
// ...
Expand Down Expand Up @@ -899,9 +875,9 @@ use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ApiResource
* @ORM\Entity
*/
#[ApiResource]
final class Brand
{
/**
Expand Down