You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: core/serialization.md
+85-19Lines changed: 85 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -139,7 +139,7 @@ App\Entity\Book:
139
139
```
140
140
141
141
In the previous example, the `name` property will be visible when reading (`GET`) the object, and it will also be available
142
-
to write (`PUT/POST`). The `author` property will be write-only; it will not be visible when serialized responses are
142
+
to write (`PUT/POST`). The `author` property will be write-only; it will not be visible when serialized responses are
143
143
returned by the API.
144
144
145
145
Internally, API Platform passes the value of the `normalization_context` as the 3rd argument of [the `Serializer::serialize()` method](https://api.symfony.com/master/Symfony/Component/Serializer/SerializerInterface.html#method_serialize) during the normalization
@@ -222,9 +222,9 @@ In the following JSON document, the relation from a book to an author is represe
222
222
}
223
223
```
224
224
225
-
However, for performance reasons, it is sometimes preferable to avoid forcing the client to issue extra HTTP requests.
226
-
It is possible to embed related objects (in their entirety, or only some of their properties) directly in the parent
227
-
response through the use of serialization groups. By using the following serialization groups annotations (`@Groups`),
225
+
However, for performance reasons, it is sometimes preferable to avoid forcing the client to issue extra HTTP requests.
226
+
It is possible to embed related objects (in their entirety, or only some of their properties) directly in the parent
227
+
response through the use of serialization groups. By using the following serialization groups annotations (`@Groups`),
228
228
a JSON representation of the author is embedded in the book response:
229
229
230
230
```php
@@ -419,7 +419,7 @@ final class BookContextBuilder implements SerializerContextBuilderInterface
public function supportsNormalization($data, $format = null, array $context = [])
497
497
{
498
498
// Make sure we're not called twice
@@ -502,7 +502,7 @@ class BookAttributeNormalizer implements ContextAwareNormalizerInterface, Normal
502
502
503
503
return $data instanceof Book;
504
504
}
505
-
505
+
506
506
private function userHasPermissionsForBook($object): bool
507
507
{
508
508
// Get permissions from user in $this->tokenStorage
@@ -512,10 +512,10 @@ class BookAttributeNormalizer implements ContextAwareNormalizerInterface, Normal
512
512
}
513
513
```
514
514
515
-
This will add the serialization group `can_retrieve_book` only if the currently logged-in user has access to the given book
515
+
This will add the serialization group `can_retrieve_book` only if the currently logged-in user has access to the given book
516
516
instance.
517
517
518
-
Note: In this example, we use the `TokenStorageInterface` to verify access to the book instance. However, Symfony
518
+
Note: In this example, we use the `TokenStorageInterface` to verify access to the book instance. However, Symfony
519
519
provides many useful other services that might be better suited to your use case. For example, the [`AuthorizationChecker`](https://symfony.com/doc/current/components/security/authorization.html#authorization-checker).
520
520
521
521
## Name Conversion
@@ -539,7 +539,7 @@ api_platform:
539
539
540
540
## Decorating a Serializer and Adding Extra Data
541
541
542
-
In the following example, we will see how we add extra informations to the serialized output. Here is how we add the
542
+
In the following example, we will see how we add extra informations to the serialized output. Here is how we add the
543
543
date on each request in `GET`:
544
544
545
545
```yaml
@@ -598,7 +598,7 @@ final class ApiNormalizer implements NormalizerInterface, DenormalizerInterface,
@@ -624,7 +624,7 @@ the `ApiPlatform\Core\Annotation\ApiProperty` annotation. For example:
624
624
class Book
625
625
{
626
626
// ...
627
-
627
+
628
628
/**
629
629
* @ApiProperty(identifier=true)
630
630
*/
@@ -659,7 +659,7 @@ App\Entity\Book:
659
659
```
660
660
661
661
In some cases, you will want to set the identifier of a resource from the client (e.g. a client-side generated UUID, or a slug).
662
-
In such cases, you must make the identifier property a writable class property. Specifically, to use client-generated IDs, you
662
+
In such cases, you must make the identifier property a writable class property. Specifically, to use client-generated IDs, you
663
663
must do the following:
664
664
665
665
1. create a setter for the identifier of the entity (e.g. `public function setId(string $id)`) or make it a `public` property ,
@@ -718,3 +718,69 @@ The JSON output will now include the embedded context:
718
718
"author": "/people/59"
719
719
}
720
720
```
721
+
722
+
## Collection relation
723
+
724
+
This is a special case where, in an entity, you have a `toMany` relation. By default, Doctrine will use an `ArrayCollection` to store your values. This is fine when you have a *read* operation, but when you try to *write* you can observe some an issue where the response is not reflecting the changes correctly. It can lead to client errors even though the update was correct.
725
+
Indeed, after an update on this relation, the collection looks wrong because `ArrayCollection`'s indexes are not sequential. To change this, we recommend to use a getter that returns `$collectionRelation->getValues()`. Thanks to this, the relation is now a real array which is sequentially indexed.
Copy file name to clipboardExpand all lines: core/validation.md
+33Lines changed: 33 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -367,3 +367,36 @@ api_platform:
367
367
```
368
368
369
369
In this example, only `severity` and `anotherPayloadField` will be serialized.
370
+
371
+
## Validation on collection relations
372
+
373
+
Note: this is related to the [collection relation denormalization](./serialization.md#collection-relation).
374
+
You may have an issue when trying to validate a relation representing a collection (`toMany`). After fixing the denormalization by using a getter that returns `$collectionRelation->getValues()`, you should define your validation on the getter instead of the property.
0 commit comments