@@ -85,12 +85,10 @@ namespace App\Entity;
85
85
use ApiPlatform\C ore\A nnotation\A piResource;
86
86
use Symfony\C omponent\S erializer\A nnotation\G roups;
87
87
88
- /**
89
- * @ApiResource(
90
- * normalizationContext={"groups"={"read"}},
91
- * denormalizationContext={"groups"={"write"}}
92
- * )
93
- */
88
+ #[ApiResource(
89
+ normalizationContext: ['groups' => ['read']],
90
+ denormalizationContext: ['groups' => ['write']],
91
+ )]
94
92
class Book
95
93
{
96
94
/**
@@ -134,12 +132,10 @@ Alternatively, you can use the more verbose syntax:
134
132
<?php
135
133
// ...
136
134
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
+ ])]
143
139
` ` `
144
140
145
141
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;
175
171
use ApiPlatform\C ore\A nnotation\A piResource;
176
172
use Symfony\C omponent\S erializer\A nnotation\G roups;
177
173
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
+ )]
189
183
class Book
190
184
{
191
185
/**
@@ -244,9 +238,7 @@ namespace App\Entity;
244
238
use ApiPlatform\C ore\A nnotation\A piResource;
245
239
use Symfony\C omponent\S erializer\A nnotation\G roups;
246
240
247
- /**
248
- * @ApiResource(normalizationContext={"groups"={"book"}})
249
- */
241
+ #[ApiResource(normalizationContext: ['groups' => ['book']])]
250
242
class Book
251
243
{
252
244
/**
@@ -272,9 +264,7 @@ namespace App\Entity;
272
264
use ApiPlatform\C ore\A nnotation\A piResource;
273
265
use Symfony\C omponent\S erializer\A nnotation\G roups;
274
266
275
- /**
276
- * @ApiResource
277
- */
267
+ #[ApiResource]
278
268
class Person
279
269
{
280
270
/**
@@ -322,9 +312,7 @@ namespace App\Entity;
322
312
323
313
use ApiPlatform\C ore\A nnotation\A piResource;
324
314
325
- /**
326
- * @ApiResource(denormalizationContext={"groups"={"book"}})
327
- */
315
+ #[ApiResource(denormalizationContext: ['groups' => ['book']])]
328
316
class Book
329
317
{
330
318
// ...
@@ -352,16 +340,14 @@ namespace App\Entity;
352
340
use ApiPlatform\C ore\A nnotation\A piResource;
353
341
use Symfony\C omponent\S erializer\A nnotation\G roups;
354
342
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
+ )]
365
351
class Person
366
352
{
367
353
/**
@@ -383,7 +369,7 @@ class Person
383
369
384
370
The problem here is that the **$parent** property become automatically an embedded object. Besides, the property won't be shown on the OpenAPI view.
385
371
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:
387
373
388
374
` ` ` php
389
375
<?php
@@ -394,16 +380,14 @@ namespace App\Entity;
394
380
use ApiPlatform\C ore\A nnotation\A piResource;
395
381
use Symfony\C omponent\S erializer\A nnotation\G roups;
396
382
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
+ )]
407
391
class Person
408
392
{
409
393
/**
@@ -415,8 +399,8 @@ class Person
415
399
/**
416
400
* @var Person
417
401
* @Groups("person")
418
- * @ApiProperty(readableLink=false, writableLink=false)
419
402
*/
403
+ #[ApiProperty(readableLink: false, writableLink: false)]
420
404
public $parent; // This property is now serialized/deserialized as an IRI.
421
405
422
406
// ...
@@ -440,13 +424,13 @@ use Doctrine\ORM\Mapping as ORM;
440
424
use Symfony\C omponent\S erializer\A nnotation\G roups;
441
425
442
426
/**
443
- * @ApiResource(
444
- * collectionOperations={
445
- * "get"={"normalization_context"={"groups"="greeting:collection:get"}},
446
- * }
447
- * )
448
427
* @ORM\E ntity
449
428
*/
429
+ #[ApiResource(
430
+ collectionOperations: [
431
+ 'get' => ['normalization_context' => ['groups' => 'greeting:collection:get']],
432
+ ],
433
+ )]
450
434
class Greeting
451
435
{
452
436
/**
@@ -501,12 +485,10 @@ namespace App\Entity;
501
485
use ApiPlatform\C ore\A nnotation\A piResource;
502
486
use Symfony\C omponent\S erializer\A nnotation\G roups;
503
487
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
+ )]
510
492
class Book
511
493
{
512
494
// ...
@@ -783,16 +765,12 @@ If you are not using the Doctrine ORM or MongoDB ODM Provider, you must explicit
783
765
the `ApiPlatform\Core\Annotation\ApiProperty` annotation. For example :
784
766
785
767
` ` ` php
786
- /**
787
- * @ApiResource()
788
- */
768
+ #[ApiResource]
789
769
class Book
790
770
{
791
771
// ...
792
772
793
- /**
794
- * @ApiProperty(identifier=true)
795
- */
773
+ #[ApiProperty(identifier: true)]
796
774
private $id;
797
775
798
776
/**
@@ -848,7 +826,7 @@ an IRI. A client that uses JSON-LD must send a second HTTP request to retrieve i
848
826
` ` `
849
827
850
828
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:
852
830
853
831
` ` ` php
854
832
<?php
@@ -858,9 +836,7 @@ namespace App\Entity;
858
836
859
837
use ApiPlatform\C ore\A nnotation\A piResource;
860
838
861
- /**
862
- * @ApiResource(normalizationContext={"jsonld_embed_context"=true})
863
- */
839
+ #[ApiResource(normalizationContext: ['jsonld_embed_context' => true])]
864
840
class Book
865
841
{
866
842
// ...
@@ -899,9 +875,9 @@ use Doctrine\Common\Collections\ArrayCollection;
899
875
use Doctrine\O RM\M apping as ORM;
900
876
901
877
/**
902
- * @ApiResource
903
878
* @ORM\E ntity
904
879
*/
880
+ #[ApiResource]
905
881
final class Brand
906
882
{
907
883
/**
0 commit comments