Skip to content

Commit 8cc9e0e

Browse files
authored
Update annotations of Serialization (#1267)
* Update serialization.md * fix typo
1 parent 7e28783 commit 8cc9e0e

File tree

1 file changed

+52
-76
lines changed

1 file changed

+52
-76
lines changed

core/serialization.md

Lines changed: 52 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,10 @@ namespace App\Entity;
8585
use ApiPlatform\Core\Annotation\ApiResource;
8686
use Symfony\Component\Serializer\Annotation\Groups;
8787
88-
/**
89-
* @ApiResource(
90-
* normalizationContext={"groups"={"read"}},
91-
* denormalizationContext={"groups"={"write"}}
92-
* )
93-
*/
88+
#[ApiResource(
89+
normalizationContext: ['groups' => ['read']],
90+
denormalizationContext: ['groups' => ['write']],
91+
)]
9492
class Book
9593
{
9694
/**
@@ -134,12 +132,10 @@ Alternatively, you can use the more verbose syntax:
134132
<?php
135133
// ...
136134
137-
/**
138-
* @ApiResource(attributes={
139-
* "normalization_context"={"groups"={"read"}},
140-
* "denormalization_context"={"groups"={"write"}}
141-
* })
142-
*/
135+
#[ApiResource(attributes: [
136+
'normalization_context' => ['groups' => ['read']],
137+
'denormalization_context' => ['groups' => ['write']],
138+
])]
143139
```
144140

145141
In the previous example, the `name` property will be visible when reading (`GET`) the object, and it will also be available
@@ -175,17 +171,15 @@ namespace App\Entity;
175171
use ApiPlatform\Core\Annotation\ApiResource;
176172
use Symfony\Component\Serializer\Annotation\Groups;
177173
178-
/**
179-
* @ApiResource(
180-
* normalizationContext={"groups"={"get"}},
181-
* itemOperations={
182-
* "get",
183-
* "put"={
184-
* "normalization_context"={"groups"={"put"}}
185-
* }
186-
* }
187-
* )
188-
*/
174+
#[ApiResource(
175+
normalizationContext: ['groups' => ['get']],
176+
itemOperations: [
177+
'get',
178+
'put' => [
179+
'normalization_context' => ['groups' => ['put']],
180+
],
181+
],
182+
)]
189183
class Book
190184
{
191185
/**
@@ -244,9 +238,7 @@ namespace App\Entity;
244238
use ApiPlatform\Core\Annotation\ApiResource;
245239
use Symfony\Component\Serializer\Annotation\Groups;
246240
247-
/**
248-
* @ApiResource(normalizationContext={"groups"={"book"}})
249-
*/
241+
#[ApiResource(normalizationContext: ['groups' => ['book']])]
250242
class Book
251243
{
252244
/**
@@ -272,9 +264,7 @@ namespace App\Entity;
272264
use ApiPlatform\Core\Annotation\ApiResource;
273265
use Symfony\Component\Serializer\Annotation\Groups;
274266
275-
/**
276-
* @ApiResource
277-
*/
267+
#[ApiResource]
278268
class Person
279269
{
280270
/**
@@ -322,9 +312,7 @@ namespace App\Entity;
322312
323313
use ApiPlatform\Core\Annotation\ApiResource;
324314
325-
/**
326-
* @ApiResource(denormalizationContext={"groups"={"book"}})
327-
*/
315+
#[ApiResource(denormalizationContext: ['groups' => ['book']])]
328316
class Book
329317
{
330318
// ...
@@ -352,16 +340,14 @@ namespace App\Entity;
352340
use ApiPlatform\Core\Annotation\ApiResource;
353341
use Symfony\Component\Serializer\Annotation\Groups;
354342
355-
/**
356-
* @ApiResource(
357-
* normalizationContext = {
358-
* "groups" = {"person"}
359-
* },
360-
* denormalizationContext = {
361-
* "groups" = {"person"}
362-
* }
363-
* )
364-
*/
343+
#[ApiResource(
344+
normalizationContext: [
345+
'groups' => ['person'],
346+
],
347+
denormalizationContext: [
348+
'groups' => ['person'],
349+
],
350+
)]
365351
class Person
366352
{
367353
/**
@@ -383,7 +369,7 @@ class Person
383369

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

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

388374
```php
389375
<?php
@@ -394,16 +380,14 @@ namespace App\Entity;
394380
use ApiPlatform\Core\Annotation\ApiResource;
395381
use Symfony\Component\Serializer\Annotation\Groups;
396382
397-
/**
398-
* @ApiResource(
399-
* normalizationContext = {
400-
* "groups" = {"person"}
401-
* },
402-
* denormalizationContext = {
403-
* "groups" = {"person"}
404-
* }
405-
* )
406-
*/
383+
#[ApiResource(
384+
normalizationContext: [
385+
'groups' => ['person'],
386+
],
387+
denormalizationContext: [
388+
'groups' => ['person'],
389+
],
390+
)]
407391
class Person
408392
{
409393
/**
@@ -415,8 +399,8 @@ class Person
415399
/**
416400
* @var Person
417401
* @Groups("person")
418-
* @ApiProperty(readableLink=false, writableLink=false)
419402
*/
403+
#[ApiProperty(readableLink: false, writableLink: false)]
420404
public $parent; // This property is now serialized/deserialized as an IRI.
421405
422406
// ...
@@ -440,13 +424,13 @@ use Doctrine\ORM\Mapping as ORM;
440424
use Symfony\Component\Serializer\Annotation\Groups;
441425
442426
/**
443-
* @ApiResource(
444-
* collectionOperations={
445-
* "get"={"normalization_context"={"groups"="greeting:collection:get"}},
446-
* }
447-
* )
448427
* @ORM\Entity
449428
*/
429+
#[ApiResource(
430+
collectionOperations: [
431+
'get' => ['normalization_context' => ['groups' => 'greeting:collection:get']],
432+
],
433+
)]
450434
class Greeting
451435
{
452436
/**
@@ -501,12 +485,10 @@ namespace App\Entity;
501485
use ApiPlatform\Core\Annotation\ApiResource;
502486
use Symfony\Component\Serializer\Annotation\Groups;
503487
504-
/**
505-
* @ApiResource(
506-
* normalizationContext={"groups"={"book:output"}},
507-
* denormalizationContext={"groups"={"book:input"}}
508-
* )
509-
*/
488+
#[ApiResource(
489+
normalizationContext: ['groups' => ['book:output']],
490+
denormalizationContext: ['groups' => ['book:input']],
491+
)]
510492
class Book
511493
{
512494
// ...
@@ -783,16 +765,12 @@ If you are not using the Doctrine ORM or MongoDB ODM Provider, you must explicit
783765
the `ApiPlatform\Core\Annotation\ApiProperty` annotation. For example:
784766

785767
```php
786-
/**
787-
* @ApiResource()
788-
*/
768+
#[ApiResource]
789769
class Book
790770
{
791771
// ...
792772
793-
/**
794-
* @ApiProperty(identifier=true)
795-
*/
773+
#[ApiProperty(identifier: true)]
796774
private $id;
797775
798776
/**
@@ -848,7 +826,7 @@ an IRI. A client that uses JSON-LD must send a second HTTP request to retrieve i
848826
```
849827

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

853831
```php
854832
<?php
@@ -858,9 +836,7 @@ namespace App\Entity;
858836
859837
use ApiPlatform\Core\Annotation\ApiResource;
860838
861-
/**
862-
* @ApiResource(normalizationContext={"jsonld_embed_context"=true})
863-
*/
839+
#[ApiResource(normalizationContext: ['jsonld_embed_context' => true])]
864840
class Book
865841
{
866842
// ...
@@ -899,9 +875,9 @@ use Doctrine\Common\Collections\ArrayCollection;
899875
use Doctrine\ORM\Mapping as ORM;
900876
901877
/**
902-
* @ApiResource
903878
* @ORM\Entity
904879
*/
880+
#[ApiResource]
905881
final class Brand
906882
{
907883
/**

0 commit comments

Comments
 (0)