From 034f0f6a14a854b8df3d1d5ee46e0caa42ef9472 Mon Sep 17 00:00:00 2001 From: Georgi Georgiev Date: Thu, 16 Sep 2021 10:28:04 +0300 Subject: [PATCH 1/2] [Serializer] Documenting the new SKIP_UNINITIALIZED_VALUES option --- components/serializer.rst | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/components/serializer.rst b/components/serializer.rst index 9c8b73a04a1..ac29ee47deb 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -1174,6 +1174,27 @@ to ``true``:: $result = $normalizer->normalize($dummy, 'json', [AbstractObjectNormalizer::SKIP_NULL_VALUES => true]); // ['bar' => 'notNull'] +Skipping uninitialized properties +--------------------------------- + +PHP 7.4 introduced typed properties, which have a new state - ``uninitialized``. +This is different from the default ``null`` of untyped properties. +When you try to access it before giving it an explicit value - you get an error. + +To avoid serializer throwing an error when serializing or normalizing an object with +uninitialized properties - then you can set ``AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES`` to ``true``. + +.. note:: + + Error is thrown only if you inject a ``ClassMetadataFactory`` into the normalizer. + Otherwise the properties are checked with reflection and uninitialized ones are skipped. + This option is useful when, for example, you want to serialize subset of properties by serialization groups, + which requires the ``ClassMetadataFactory`` + +.. versionadded:: 5.4 + + The ``AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES`` constant was introduced in Symfony 5.4. + .. _component-serializer-handling-circular-references: Handling Circular References From 08599e89de71c6a40d2fab5c1a7293d2f4e759a8 Mon Sep 17 00:00:00 2001 From: Ivan Nemets <79963574+ivannemets-sravniru@users.noreply.github.com> Date: Thu, 14 Oct 2021 13:56:59 +0300 Subject: [PATCH 2/2] Update serializer.rst --- components/serializer.rst | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/components/serializer.rst b/components/serializer.rst index ac29ee47deb..e87e9eafdbd 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -1181,15 +1181,23 @@ PHP 7.4 introduced typed properties, which have a new state - ``uninitialized``. This is different from the default ``null`` of untyped properties. When you try to access it before giving it an explicit value - you get an error. -To avoid serializer throwing an error when serializing or normalizing an object with -uninitialized properties - then you can set ``AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES`` to ``true``. +By default, to avoid the Serializer throwing an error when serializing or normalizing an object with +uninitialized properties, object normalizer catches these errors and ignores such properties. + +You can disable this behavior by setting the ``AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES`` context option to ``false``:: + + class Dummy { + public string $foo = 'initialized'; + public string $bar; // uninitialized + } + + $normalizer = new ObjectNormalizer(); + $result = $normalizer->normalize(new Dummy(), 'json', [AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES => false]); + // throws Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException as normalizer cannot read uninitialized properties .. note:: - Error is thrown only if you inject a ``ClassMetadataFactory`` into the normalizer. - Otherwise the properties are checked with reflection and uninitialized ones are skipped. - This option is useful when, for example, you want to serialize subset of properties by serialization groups, - which requires the ``ClassMetadataFactory`` + Calling ``PropertyNormalizer::normalize`` or ``GetSetMethodNormalizer::normalize`` with ``AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES`` context option set to ``false`` will throw an ``\Error`` instance if the given object has uninitialized properties as the normalizer cannot read them (directly or via getter/isser methods). .. versionadded:: 5.4 pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy