From f53d88fdca8e7d7fc755ce84984cff414deddf92 Mon Sep 17 00:00:00 2001 From: Alexandre Daubois Date: Thu, 19 Oct 2023 12:03:54 +0200 Subject: [PATCH] [Serializer] Deprecate annotations --- components/property_info.rst | 21 ++++++++++++++++----- components/serializer.rst | 31 ++++++++++++++++++++++++++----- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/components/property_info.rst b/components/property_info.rst index d5699ea1bab..a95975aadd5 100644 --- a/components/property_info.rst +++ b/components/property_info.rst @@ -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). diff --git a/components/serializer.rst b/components/serializer.rst index f879d5167c6..fc047ad7467 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -279,6 +279,13 @@ for each format: $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); +* 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; @@ -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: @@ -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); @@ -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; @@ -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 { @@ -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);