Skip to content

Commit 08599e8

Browse files
Update serializer.rst
1 parent 034f0f6 commit 08599e8

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

components/serializer.rst

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,15 +1181,23 @@ PHP 7.4 introduced typed properties, which have a new state - ``uninitialized``.
11811181
This is different from the default ``null`` of untyped properties.
11821182
When you try to access it before giving it an explicit value - you get an error.
11831183

1184-
To avoid serializer throwing an error when serializing or normalizing an object with
1185-
uninitialized properties - then you can set ``AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES`` to ``true``.
1184+
By default, to avoid the Serializer throwing an error when serializing or normalizing an object with
1185+
uninitialized properties, object normalizer catches these errors and ignores such properties.
1186+
1187+
You can disable this behavior by setting the ``AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES`` context option to ``false``::
1188+
1189+
class Dummy {
1190+
public string $foo = 'initialized';
1191+
public string $bar; // uninitialized
1192+
}
1193+
1194+
$normalizer = new ObjectNormalizer();
1195+
$result = $normalizer->normalize(new Dummy(), 'json', [AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES => false]);
1196+
// throws Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException as normalizer cannot read uninitialized properties
11861197

11871198
.. note::
11881199

1189-
Error is thrown only if you inject a ``ClassMetadataFactory`` into the normalizer.
1190-
Otherwise the properties are checked with reflection and uninitialized ones are skipped.
1191-
This option is useful when, for example, you want to serialize subset of properties by serialization groups,
1192-
which requires the ``ClassMetadataFactory``
1200+
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).
11931201

11941202
.. versionadded:: 5.4
11951203

0 commit comments

Comments
 (0)