Skip to content

Commit 4267ad5

Browse files
committed
docs: add missing serialization yaml documentation
1 parent 5e67f43 commit 4267ad5

File tree

1 file changed

+163
-0
lines changed

1 file changed

+163
-0
lines changed

core/serialization.md

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ level ignored.
162162

163163
In the following example we use different serialization groups for the `GET` and `PUT` operations:
164164

165+
[codeSelector]
166+
165167
```php
166168
<?php
167169
// api/src/Entity/Book.php
@@ -196,6 +198,29 @@ class Book
196198
}
197199
```
198200

201+
```yaml
202+
# config/api/resources/Book.yaml
203+
App\Entity\Book:
204+
attributes:
205+
normalization_context:
206+
groups: ['get']
207+
itemOperations:
208+
get: ~
209+
put:
210+
normalization_context:
211+
groups: ['put']
212+
213+
# config/serializer/Book.yaml
214+
App\Entity\Book:
215+
attributes:
216+
name:
217+
groups: ['get', 'put']
218+
author:
219+
groups: ['get']
220+
```
221+
222+
[/codeSelector]
223+
199224
The `name` and `author` properties will be included in the document generated during a `GET` operation because the configuration
200225
defined at the resource level is inherited. However the document generated when a `PUT` request will be received will only
201226
include the `name` property because of the specific configuration for this operation.
@@ -229,6 +254,8 @@ It is possible to embed related objects (in their entirety, or only some of thei
229254
response through the use of serialization groups. By using the following serialization groups annotations (`@Groups`),
230255
a JSON representation of the author is embedded in the book response:
231256

257+
[codeSelector]
258+
232259
```php
233260
<?php
234261
// api/src/Entity/Book.php
@@ -277,6 +304,28 @@ class Person
277304
}
278305
```
279306

307+
```yaml
308+
# config/api/resources/Book.yaml
309+
App\Entity\Book:
310+
attributes:
311+
normalization_context:
312+
groups: ['book']
313+
# config/serializer/Book.yaml
314+
App\Entity\Book:
315+
attributes:
316+
name:
317+
groups: ['book']
318+
author:
319+
groups: ['book']
320+
# config/serializer/Person.yaml
321+
App\Entity\Person:
322+
attributes:
323+
name:
324+
groups: ['book']
325+
```
326+
327+
[/codeSelector]
328+
280329
The generated JSON using previous settings is below:
281330

282331
```json
@@ -304,6 +353,8 @@ Instead of embedding relations in the main HTTP response, you may want [to "push
304353
It is also possible to embed a relation in `PUT` and `POST` requests. To enable that feature, set the serialization groups
305354
the same way as normalization. For example:
306355

356+
[codeSelector]
357+
307358
```php
308359
<?php
309360
// api/src/Entity/Book.php
@@ -319,6 +370,16 @@ class Book
319370
}
320371
```
321372

373+
```yaml
374+
# config/api/resources/Book.yaml
375+
App\Entity\Book:
376+
attributes:
377+
denormalization_context:
378+
groups: ['book']
379+
```
380+
381+
[/codeSelector]
382+
322383
The following rules apply when denormalizing embedded relations:
323384

324385
* If an `@id` key is present in the embedded resource, then the object corresponding to the given URI will be retrieved through
@@ -331,6 +392,8 @@ You can specify as many embedded relation levels as you want.
331392

332393
It is a common problem to have entities that reference other entities of the same type:
333394

395+
[codeSelector]
396+
334397
```php
335398
<?php
336399
// api/src/Entity/Person.php
@@ -364,13 +427,34 @@ class Person
364427
365428
// ...
366429
}
430+
```
367431

432+
```yaml
433+
# config/api/resources/Person.yaml
434+
App\Entity\Person:
435+
attributes:
436+
normalization_context:
437+
groups: ['person']
438+
denormalization_context:
439+
groups: ['person']
440+
441+
# config/serializer/Person.yaml
442+
App\Entity\Person:
443+
attributes:
444+
name:
445+
groups: ['person']
446+
parent:
447+
groups: ['person']
368448
```
369449

450+
[/codeSelector]
451+
370452
The problem here is that the **$parent** property become automatically an embedded object. Besides, the property won't be shown on the OpenAPI view.
371453

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

456+
[codeSelector]
457+
374458
```php
375459
<?php
376460
// api/src/Entity/Person.php
@@ -408,10 +492,36 @@ class Person
408492
409493
```
410494

495+
```yaml
496+
# config/api/resources/Person.yaml
497+
App\Entity\Person:
498+
attributes:
499+
normalization_context:
500+
groups: ['person']
501+
denormalization_context:
502+
groups: ['person']
503+
properties:
504+
parent:
505+
readableLink: false
506+
writableLink: false
507+
508+
# config/serializer/Person.yaml
509+
App\Entity\Person:
510+
attributes:
511+
name:
512+
groups: ['person']
513+
parent:
514+
groups: ['person']
515+
```
516+
517+
[/codeSelector]
518+
411519
## Calculated Field
412520

413521
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.
414522

523+
[codeSelector]
524+
415525
```php
416526
<?php
417527
@@ -470,12 +580,33 @@ class Greeting
470580
}
471581
```
472582

583+
```yaml
584+
# config/api/resources/Greeting.yaml
585+
App\Entity\Greeting:
586+
collectionOperations:
587+
get:
588+
normalization_context:
589+
groups: 'greeting:collection:get'
590+
591+
# config/serializer/Greeting.yaml
592+
App\Entity\Greeting:
593+
attributes:
594+
id:
595+
groups: 'greeting:collection:get'
596+
getSum:
597+
groups: 'greeting:collection:get'
598+
```
599+
600+
[/codeSelector]
601+
473602
## Changing the Serialization Context Dynamically
474603

475604
<p align="center" class="symfonycasts"><a href="https://symfonycasts.com/screencast/api-platform-security/service-decoration?cid=apip"><img src="../distribution/images/symfonycasts-player.png" alt="Context Builder & Service Decoration screencast"><br>Watch the Context Builder & Service Decoration screencast</a></p>
476605

477606
Let's imagine a resource where most fields can be managed by any user, but some can be managed only by admin users:
478607

608+
[codeSelector]
609+
479610
```php
480611
<?php
481612
// api/src/Entity/Book.php
@@ -515,6 +646,26 @@ class Book
515646
}
516647
```
517648

649+
```yaml
650+
# config/api/resources/Book.yaml
651+
App\Entity\Book:
652+
attributes:
653+
normalization_context:
654+
groups: ['book:output']
655+
denormalization_context:
656+
groups: ['book:input']
657+
658+
# config/serializer/Book.yaml
659+
App\Entity\Book:
660+
attributes:
661+
active:
662+
groups: ['book:output', 'admin:input']
663+
name:
664+
groups: ['book:output', 'book:input']
665+
```
666+
667+
[/codeSelector]
668+
518669
All entry points are the same for all users, so we should find a way to detect if the authenticated user is an admin, and if so
519670
dynamically add the `admin:input` value to deserialization groups in the `$context` array.
520671

@@ -830,6 +981,8 @@ an IRI. A client that uses JSON-LD must send a second HTTP request to retrieve i
830981
You can configure API Platform to embed the JSON-LD context in the root document by adding the `jsonld_embed_context`
831982
attribute to the `#[ApiResource]` annotation:
832983

984+
[codeSelector]
985+
833986
```php
834987
<?php
835988
// api/src/Entity/Book.php
@@ -845,6 +998,16 @@ class Book
845998
}
846999
```
8471000

1001+
```yaml
1002+
# config/api/resources/Book.yaml
1003+
App\Entity\Book:
1004+
attributes:
1005+
normalization_context:
1006+
jsonld_embed_context: true
1007+
```
1008+
1009+
[/codeSelector]
1010+
8481011
The JSON output will now include the embedded context:
8491012

8501013
```json

0 commit comments

Comments
 (0)