Skip to content

Update AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES description #1

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

Closed
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
20 changes: 14 additions & 6 deletions components/serializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down