Skip to content

[Serializer] Add an @Ignore annotation #28744 #13701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 69 additions & 2 deletions components/serializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,75 @@ 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:
bar:
ignore: true

.. code-block:: xml

<?xml version="1.0" ?>
<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
https://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd"
>
<class name="App\Model\MyClass">
<attribute name="bar">
<ignore>true</ignore>
</attribute>
</class>
</serializer>

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;
Expand Down