Skip to content

[Serializer] Deprecate annotations #19046

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 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
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
21 changes: 16 additions & 5 deletions components/property_info.rst
Original file line number Diff line number Diff line change
Expand Up @@ -464,19 +464,30 @@ the :class:`Symfony\\Component\\PropertyInfo\\Extractor\\SerializerExtractor`
provides list information. This extractor is *not* registered automatically
with the ``property_info`` service in the Symfony Framework::

use Doctrine\Common\Annotations\AnnotationReader;
use Symfony\Component\PropertyInfo\Extractor\SerializerExtractor;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader;

$serializerClassMetadataFactory = new ClassMetadataFactory(
new AnnotationLoader(new AnnotationReader)
);
$serializerClassMetadataFactory = new ClassMetadataFactory(new AttributeLoader());
$serializerExtractor = new SerializerExtractor($serializerClassMetadataFactory);

// the `serializer_groups` option must be configured (may be set to null)
$serializerExtractor->getProperties($class, ['serializer_groups' => ['mygroup']]);

.. versionadded:: 6.4

The
:class:`Symfony\\Component\\Serializer\\Mapping\\Loader\\AttributeLoader`
was introduced in Symfony 6.4. Prior to this, the
:class:`Symfony\\Component\\Serializer\\Mapping\\Loader\\AnnotationLoader`
must be used.

.. deprecated:: 6.4

The
:class:`Symfony\\Component\\Serializer\\Mapping\\Loader\\AnnotationLoader`
was deprecated in Symfony 6.4.

If ``serializer_groups`` is set to ``null``, serializer groups metadata won't be
checked but you will get only the properties considered by the Serializer
Component (notably the ``@Ignore`` annotation is taken into account).
Expand Down
31 changes: 26 additions & 5 deletions components/serializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,13 @@ for each format:

$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be removed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or remove it in 7.0?

Copy link
Member Author

@alexandre-daubois alexandre-daubois Oct 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is still valid in 6.4 so I think it should stay with the deprecation message below. If someone wants to migrate from one to another, this person may search for AnnotationLoader in the doc to understand how to migrate

I would remove it in 7.0 🙂

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you open a PR?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep 🙂


* Attributes in PHP files::

use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader;

$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());

* YAML files::

use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
Expand All @@ -293,6 +300,21 @@ for each format:

$classMetadataFactory = new ClassMetadataFactory(new XmlFileLoader('/path/to/your/definition.xml'));

.. versionadded:: 6.4

The
:class:`Symfony\\Component\\Serializer\\Mapping\\Loader\\AttributeLoader`
was introduced in Symfony 6.4. Prior to this, the
:class:`Symfony\\Component\\Serializer\\Mapping\\Loader\\AnnotationLoader`
must be used.

.. deprecated:: 6.4

Reading annotations in PHP files is deprecated since Symfony 6.4.
Also, the
:class:`Symfony\\Component\\Serializer\\Mapping\\Loader\\AnnotationLoader`
was deprecated in Symfony 6.4.

.. _component-serializer-attributes-groups-annotations:
.. _component-serializer-attributes-groups-attributes:

Expand Down Expand Up @@ -645,7 +667,7 @@ this is already set up and you only need to provide the configuration. Otherwise
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;

$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());

$metadataAwareNameConverter = new MetadataAwareNameConverter($classMetadataFactory);

Expand Down Expand Up @@ -1533,10 +1555,9 @@ Instead of throwing an exception, a custom callable can be executed when the
maximum depth is reached. This is especially useful when serializing entities
having unique identifiers::

use Doctrine\Common\Annotations\AnnotationReader;
use Symfony\Component\Serializer\Annotation\MaxDepth;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
Expand All @@ -1560,7 +1581,7 @@ having unique identifiers::
$level3->id = 3;
$level2->child = $level3;

$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());

// all callback parameters are optional (you can omit the ones you don't use)
$maxDepthHandler = function (object $innerObject, object $outerObject, string $attributeName, string $format = null, array $context = []): string {
Expand Down Expand Up @@ -1761,7 +1782,7 @@ this is already set up and you only need to provide the configuration. Otherwise
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;

$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());

$discriminator = new ClassDiscriminatorFromClassMetadata($classMetadataFactory);

Expand Down