From 6a3b3554a7d3163a8199d740903558040e828c57 Mon Sep 17 00:00:00 2001 From: Valentin Date: Fri, 28 Aug 2020 23:42:48 +0200 Subject: [PATCH 1/3] [Serializer] Add an @Ignore annotation #28744 Update serializer.rst Update components/serializer.rst Co-authored-by: Oskar Stark Update components/serializer.rst Co-authored-by: Antoine Makdessi Update components/serializer.rst Co-authored-by: Antoine Makdessi Update components/serializer.rst Co-authored-by: Oskar Stark Update components/serializer.rst Co-authored-by: Antoine Makdessi Update serializer.rst --- components/serializer.rst | 77 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 2 deletions(-) diff --git a/components/serializer.rst b/components/serializer.rst index ef205bf59be..b3735deb32a 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -413,8 +413,81 @@ As for groups, attributes can be selected during both the serialization and dese Ignoring Attributes ------------------- -As an option, there's a way to ignore attributes from the origin object. -To remove those attributes provide an array via the ``AbstractNormalizer::IGNORED_ATTRIBUTES`` +All attributes are included by default when serializing objects. You have two alternatives to ignore some of those attributes. + +* `Option 1: Using @Ignore annotation`_ +* `Option 2: Using the context`_ + +Option 1: Using ``@Ignore`` annotation +-------------------------------------- + +.. configuration-block:: + + .. code-block:: php-annotations + + namespace App\Model; + + use Symfony\Component\Serializer\Annotation\Ignore; + + class MyClass + { + public $foo; + + /** + * @Ignore() + */ + public $bar; + } + + .. code-block:: yaml + + App\Model\MyClass: + attributes: + foo: + ignore: false + bar: + ignore: true + + .. code-block:: xml + + + + + + false + + + + true + + + + +You are now able to ignore specific attributes during serialization:: + + use App\Model\MyClass; + use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; + use Symfony\Component\Serializer\Serializer; + + $obj = new MyClass(); + $obj->foo = 'foo'; + $obj->bar = 'bar'; + + $normalizer = new ObjectNormalizer($classMetadataFactory); + $serializer = new Serializer([$normalizer]); + + $data = $serializer->normalize($obj); + // $data = ['foo' => 'foo']; + + +Option 2: Using the context +--------------------------- + +By providing an array via the ``AbstractNormalizer::IGNORED_ATTRIBUTES`` key in the ``context`` parameter of the desired serializer method:: use Acme\Person; From 56be70089e07a3d36c47bf298b1bb065d13f0030 Mon Sep 17 00:00:00 2001 From: Valentin Silvestre <17164385+vasilvestre@users.noreply.github.com> Date: Mon, 31 Aug 2020 11:44:16 +0200 Subject: [PATCH 2/3] Update serializer.rst --- components/serializer.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/serializer.rst b/components/serializer.rst index b3735deb32a..fefa16f55f1 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -419,7 +419,7 @@ All attributes are included by default when serializing objects. You have two al * `Option 2: Using the context`_ Option 1: Using ``@Ignore`` annotation --------------------------------------- +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. configuration-block:: @@ -485,7 +485,7 @@ You are now able to ignore specific attributes during serialization:: Option 2: Using the context ---------------------------- +~~~~~~~~~~~~~~~~~~~~~~~~~~~ By providing an array via the ``AbstractNormalizer::IGNORED_ATTRIBUTES`` key in the ``context`` parameter of the desired serializer method:: From dcd5b4ce86088786de05a6ee61930137d4746c0e Mon Sep 17 00:00:00 2001 From: Valentin Silvestre <17164385+vasilvestre@users.noreply.github.com> Date: Mon, 31 Aug 2020 14:24:01 +0200 Subject: [PATCH 3/3] Update serializer.rst Remove default value (false) from ignoring rules in XML and YML files as I didn't put them in PHP code. --- components/serializer.rst | 6 ------ 1 file changed, 6 deletions(-) diff --git a/components/serializer.rst b/components/serializer.rst index fefa16f55f1..8bc88446651 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -443,8 +443,6 @@ Option 1: Using ``@Ignore`` annotation App\Model\MyClass: attributes: - foo: - ignore: false bar: ignore: true @@ -457,10 +455,6 @@ Option 1: Using ``@Ignore`` annotation https://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd" > - - false - - true