@@ -408,6 +408,115 @@ class Person
408
408
409
409
` ` `
410
410
411
+ # # Property Normalization Context
412
+
413
+ 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,
414
+ you can do so by using the `#[Context]` attribute from the Symfony Serializer component.
415
+
416
+ For instance :
417
+
418
+ ` ` ` php
419
+ <?php
420
+ // api/src/Entity/Book.php
421
+
422
+ declare(strict_types=1);
423
+
424
+ namespace App\E ntity;
425
+
426
+ use ApiPlatform\C ore\A nnotation\A piResource;
427
+ use Doctrine\O RM\M apping as ORM;
428
+ use Symfony\C omponent\S erializer\A nnotation\C ontext;
429
+ use Symfony\C omponent\S erializer\N ormalizer\D ateTimeNormalizer;
430
+
431
+ /**
432
+ * @ORM\E ntity
433
+ */
434
+ #[ApiResource]
435
+ class Book
436
+ {
437
+ /**
438
+ * @ORM\C olumn(type="date")
439
+ */
440
+ #[Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
441
+ public ?\D ateTimeInterface $publicationDate = null;
442
+ }
443
+ ` ` `
444
+
445
+ In the above example, you will receive the book's data like this :
446
+
447
+ ` ` ` json
448
+ {
449
+ "@context": "/contexts/Book",
450
+ "@id": "/books/3",
451
+ "@type": "http://schema.org/Book",
452
+ "publicationDate": "1989-06-16"
453
+ }
454
+ ` ` `
455
+
456
+ It's also possible to only change the denormalization or normalization context :
457
+
458
+ ` ` ` php
459
+ <?php
460
+ // api/src/Entity/Book.php
461
+
462
+ declare(strict_types=1);
463
+
464
+ namespace App\E ntity;
465
+
466
+ use ApiPlatform\C ore\A nnotation\A piResource;
467
+ use Doctrine\O RM\M apping as ORM;
468
+ use Symfony\C omponent\S erializer\A nnotation\C ontext;
469
+ use Symfony\C omponent\S erializer\N ormalizer\D ateTimeNormalizer;
470
+
471
+ /**
472
+ * @ORM\E ntity
473
+ */
474
+ #[ApiResource]
475
+ class Book
476
+ {
477
+ /**
478
+ * @ORM\C olumn(type="date")
479
+ */
480
+ #[Context(normalizationContext: [DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
481
+ public ?\D ateTimeInterface $publicationDate = null;
482
+ }
483
+ ` ` `
484
+
485
+ Groups are also supported :
486
+
487
+ ` ` ` php
488
+ <?php
489
+ // api/src/Entity/Book.php
490
+
491
+ declare(strict_types=1);
492
+
493
+ namespace App\E ntity;
494
+
495
+ use ApiPlatform\C ore\A nnotation\A piResource;
496
+ use Doctrine\O RM\M apping as ORM;
497
+ use Symfony\C omponent\S erializer\A nnotation\C ontext;
498
+ use Symfony\C omponent\S erializer\A nnotation\G roups;
499
+ use Symfony\C omponent\S erializer\N ormalizer\D ateTimeNormalizer;
500
+
501
+ /**
502
+ * @ORM\E ntity
503
+ */
504
+ #[ApiResource]
505
+ class Book
506
+ {
507
+ /**
508
+ * @ORM\C olumn(type="date")
509
+ */
510
+ #[Groups(["extended"])]
511
+ #[Context([DateTimeNormalizer::FORMAT_KEY => \D ateTime::RFC3339])]
512
+ #[Context(
513
+ context: [DateTimeNormalizer::FORMAT_KEY => \D ateTime::RFC3339_EXTENDED],
514
+ groups: ['extended'],
515
+ )]
516
+ public ?\D ateTimeInterface $publicationDate = null;
517
+ }
518
+ ` ` `
519
+
411
520
# # Calculated Field
412
521
413
522
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.
0 commit comments