Skip to content

Commit 3044c7b

Browse files
committed
Merge branch '5.4' into 6.0
* 5.4: Update serializer.rst [Serializer] Documenting the new SKIP_UNINITIALIZED_VALUES option
2 parents a5931d4 + f6f0755 commit 3044c7b

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

components/serializer.rst

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,41 @@ to ``true``::
11931193
$result = $normalizer->normalize($dummy, 'json', [AbstractObjectNormalizer::SKIP_NULL_VALUES => true]);
11941194
// ['bar' => 'notNull']
11951195

1196+
Skipping Uninitialized Properties
1197+
---------------------------------
1198+
1199+
In PHP, typed properties have an ``uninitialized`` state which is different
1200+
from the default ``null`` of untyped properties. When you try to access a typed
1201+
property before giving it an explicit value, you get an error.
1202+
1203+
To avoid the Serializer throwing an error when serializing or normalizing an
1204+
object with uninitialized properties, by default the object normalizer catches
1205+
these errors and ignores such properties.
1206+
1207+
You can disable this behavior by setting the ``AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES``
1208+
context option to ``false``::
1209+
1210+
class Dummy {
1211+
public string $foo = 'initialized';
1212+
public string $bar; // uninitialized
1213+
}
1214+
1215+
$normalizer = new ObjectNormalizer();
1216+
$result = $normalizer->normalize(new Dummy(), 'json', [AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES => false]);
1217+
// throws Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException as normalizer cannot read uninitialized properties
1218+
1219+
.. note::
1220+
1221+
Calling ``PropertyNormalizer::normalize`` or ``GetSetMethodNormalizer::normalize``
1222+
with ``AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES`` context option set
1223+
to ``false`` will throw an ``\Error`` instance if the given object has uninitialized
1224+
properties as the normalizer cannot read them (directly or via getter/isser methods).
1225+
1226+
.. versionadded:: 5.4
1227+
1228+
The ``AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES`` constant was
1229+
introduced in Symfony 5.4.
1230+
11961231
.. _component-serializer-handling-circular-references:
11971232

11981233
Collecting Type Errors While Denormalizing

0 commit comments

Comments
 (0)