From 4dd7746ad813cb9615a975131ca151ffa15b90af Mon Sep 17 00:00:00 2001 From: Jeka Zabrodin <jzabrodin@gmail.com> Date: Mon, 13 Dec 2021 14:58:58 +0200 Subject: [PATCH 1/7] Update serialization.md Add enable_max_depth details --- core/serialization.md | 46 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/core/serialization.md b/core/serialization.md index e7c4f1a5f37..fc0bb3132cc 100644 --- a/core/serialization.md +++ b/core/serialization.md @@ -41,6 +41,52 @@ feature of the Symfony Serializer component. In addition to groups, you can use any option supported by the Symfony Serializer. For example, you can use [`enable_max_depth`](https://symfony.com/doc/current/components/serializer.html#handling-serialization-depth) to limit the serialization depth. +```php +<?php +// api/src/Entity/Book.php + +namespace App\Entity; + +use ApiPlatform\Core\Annotation\ApiResource; +use Symfony\Component\Serializer\Annotation\Groups; + +#[ApiResource( + normalizationContext: ['groups' => ['read'], 'enable_max_depth'=true], + denormalizationContext: ['groups' => ['write']], +)] +class Book +{ + #[Groups(["read", "write"])] + public $name; + + #[Groups("write"), MaxDepth(1)] + public $author; + + // ... +} +``` + +```yaml +# api/config/api_platform/resources.yaml +resources: + App\Entity\Book: + attributes: + normalization_context: + groups: ['read'] + enable_max_depth: true + denormalization_context: + groups: ['write'] + +# api/config/serialization/Book.yaml + +App\Entity\Book: + properties: + author: + groups: ['write'] + max_depth: 1 +``` + + ### Configuration Just like other Symfony and API Platform components, the Serializer component can be configured using annotations, XML From 17c170e2fd5503c1b3119bffd6957606670f053d Mon Sep 17 00:00:00 2001 From: Jeka Zabrodin <jzabrodin@gmail.com> Date: Tue, 28 Dec 2021 13:30:55 +0200 Subject: [PATCH 2/7] Update core/serialization.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Kévin Dunglas <kevin@dunglas.fr> --- core/serialization.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/serialization.md b/core/serialization.md index fc0bb3132cc..a4432735a30 100644 --- a/core/serialization.md +++ b/core/serialization.md @@ -51,7 +51,7 @@ use ApiPlatform\Core\Annotation\ApiResource; use Symfony\Component\Serializer\Annotation\Groups; #[ApiResource( - normalizationContext: ['groups' => ['read'], 'enable_max_depth'=true], + normalizationContext: ['groups' => 'read', 'enable_max_depth' => true], denormalizationContext: ['groups' => ['write']], )] class Book From 5babbc7b7afc6c91bddf5a04a4369dc2e9b3d4cf Mon Sep 17 00:00:00 2001 From: Jeka Zabrodin <jzabrodin@gmail.com> Date: Tue, 28 Dec 2021 13:31:06 +0200 Subject: [PATCH 3/7] Update core/serialization.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Kévin Dunglas <kevin@dunglas.fr> --- core/serialization.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/serialization.md b/core/serialization.md index a4432735a30..fe05e2e4141 100644 --- a/core/serialization.md +++ b/core/serialization.md @@ -59,7 +59,8 @@ class Book #[Groups(["read", "write"])] public $name; - #[Groups("write"), MaxDepth(1)] + #[Groups("write")] + #[MaxDepth(1)] public $author; // ... From ee974c1c1dfa4d57dc811f23dd591c18a9a04dd5 Mon Sep 17 00:00:00 2001 From: Jeka Zabrodin <jzabrodin@gmail.com> Date: Tue, 28 Dec 2021 13:35:39 +0200 Subject: [PATCH 4/7] Update serialization.md --- core/serialization.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core/serialization.md b/core/serialization.md index fe05e2e4141..3bfd76d7c2c 100644 --- a/core/serialization.md +++ b/core/serialization.md @@ -49,6 +49,7 @@ namespace App\Entity; use ApiPlatform\Core\Annotation\ApiResource; use Symfony\Component\Serializer\Annotation\Groups; +use Symfony\Component\Serializer\Annotation\MaxDepth; #[ApiResource( normalizationContext: ['groups' => 'read', 'enable_max_depth' => true], From 3cf7f0d041b63c67840ae09af76bfab03cff5c38 Mon Sep 17 00:00:00 2001 From: Vincent <vincentchalamon@protonmail.com> Date: Wed, 20 Jul 2022 12:23:00 +0200 Subject: [PATCH 5/7] Apply suggestions from code review --- core/serialization.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/serialization.md b/core/serialization.md index 3bfd76d7c2c..d166a9d67de 100644 --- a/core/serialization.md +++ b/core/serialization.md @@ -41,6 +41,8 @@ feature of the Symfony Serializer component. In addition to groups, you can use any option supported by the Symfony Serializer. For example, you can use [`enable_max_depth`](https://symfony.com/doc/current/components/serializer.html#handling-serialization-depth) to limit the serialization depth. +[codeSelector] + ```php <?php // api/src/Entity/Book.php @@ -88,6 +90,8 @@ App\Entity\Book: max_depth: 1 ``` +[/codeSelector] + ### Configuration From 5ee78a19aad8f8f053aa40e11cf005789a581939 Mon Sep 17 00:00:00 2001 From: Yevgheni Zabrodin <Yevgheni_Zabrodin@epam.com> Date: Mon, 15 Aug 2022 11:44:08 +0300 Subject: [PATCH 6/7] Fix review issues --- core/serialization.md | 56 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/core/serialization.md b/core/serialization.md index caf05b22495..86b560c21b3 100644 --- a/core/serialization.md +++ b/core/serialization.md @@ -11,7 +11,7 @@ The main serialization process has two stages: ![Serializer workflow](images/SerializerWorkflow.png) > As you can see in the picture above, an array is used as a man-in-the-middle. This way, Encoders will only deal with turning specific formats into arrays and vice versa. The same way, Normalizers will deal with turning specific objects into arrays and vice versa. --- [The Symfony documentation](https://symfony.com/doc/current/components/serializer.html) +> -- [The Symfony documentation](https://symfony.com/doc/current/components/serializer.html) Unlike Symfony itself, API Platform leverages custom normalizers, its router and the [data provider](data-providers.md) system to perform an advanced transformation. Metadata are added to the generated document including links, type information, pagination data or available filters. @@ -62,8 +62,7 @@ class Book #[Groups(["read", "write"])] public $name; - #[Groups("write")] - #[MaxDepth(1)] + #[Groups("write"), MaxDepth(1)] public $author; // ... @@ -82,7 +81,6 @@ resources: groups: ['write'] # api/config/serialization/Book.yaml - App\Entity\Book: properties: author: @@ -90,6 +88,56 @@ App\Entity\Book: max_depth: 1 ``` +```xml +<!-- api/config/api_platform/resources.xml --> +<?xml version="1.0" encoding="UTF-8" ?> +<resources xmlns="https://api-platform.com/schema/metadata/resources-3.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="https://api-platform.com/schema/metadata/resources-3.0 + https://api-platform.com/schema/metadata/resources-3.0.xsd"> + <resource class="App\Entity\Book"> + <normalizationContext> + <values> + <value name="groups"> + <values> + <value>read</value> + </values> + </value> + <value name="enable_max_depth"> + <values> + <value>true</value> + </values> + </value> + </values> + </normalizationContext> + <denormalizationContext> + <values> + <value name="groups"> + <values> + <value>write</value> + </values> + </value> + </values> + </denormalizationContext> + </resource> +</resources> + + +<!-- api/config/serialization/Book.xml --> +<?xml version="1.0" encoding="UTF-8" ?> +<serializer xmlns="http://symfony.com/schema/dic/serializer-mapping" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://symfony.com/schema/dic/serializer-mapping + http://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd"> + <class name="App\Entity\Book"> + <attribute name="author"> + <group>write</group> + <max_depth>1</max_depth> + </attribute> + </class> +</serializer> +``` + [/codeSelector] From cfce698191999145403ec5239e1647d5ca33b7ce Mon Sep 17 00:00:00 2001 From: Yevhen Zabrodin <jzabrodin@gmail.com> Date: Thu, 8 Sep 2022 11:43:44 +0300 Subject: [PATCH 7/7] Update core/serialization.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Kévin Dunglas <kevin@dunglas.fr> --- core/serialization.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/serialization.md b/core/serialization.md index 86b560c21b3..3c88b217b7b 100644 --- a/core/serialization.md +++ b/core/serialization.md @@ -62,7 +62,8 @@ class Book #[Groups(["read", "write"])] public $name; - #[Groups("write"), MaxDepth(1)] + #[Groups("write")] + #[MaxDepth(1)] public $author; // ...