Skip to content

Commit cc07180

Browse files
committed
Merge branch '6.1' into 6.2
* 6.1: Update serializer.rst [Serializer] Documenting the new SKIP_UNINITIALIZED_VALUES option
2 parents bd75e99 + 6563914 commit cc07180

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

components/serializer.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,6 +1241,36 @@ to ``true``::
12411241
$result = $normalizer->normalize($dummy, 'json', [AbstractObjectNormalizer::SKIP_NULL_VALUES => true]);
12421242
// ['bar' => 'notNull']
12431243

1244+
Skipping Uninitialized Properties
1245+
---------------------------------
1246+
1247+
In PHP, typed properties have an ``uninitialized`` state which is different
1248+
from the default ``null`` of untyped properties. When you try to access a typed
1249+
property before giving it an explicit value, you get an error.
1250+
1251+
To avoid the Serializer throwing an error when serializing or normalizing an
1252+
object with uninitialized properties, by default the object normalizer catches
1253+
these errors and ignores such properties.
1254+
1255+
You can disable this behavior by setting the ``AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES``
1256+
context option to ``false``::
1257+
1258+
class Dummy {
1259+
public string $foo = 'initialized';
1260+
public string $bar; // uninitialized
1261+
}
1262+
1263+
$normalizer = new ObjectNormalizer();
1264+
$result = $normalizer->normalize(new Dummy(), 'json', [AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES => false]);
1265+
// throws Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException as normalizer cannot read uninitialized properties
1266+
1267+
.. note::
1268+
1269+
Calling ``PropertyNormalizer::normalize`` or ``GetSetMethodNormalizer::normalize``
1270+
with ``AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES`` context option set
1271+
to ``false`` will throw an ``\Error`` instance if the given object has uninitialized
1272+
properties as the normalizer cannot read them (directly or via getter/isser methods).
1273+
12441274
.. _component-serializer-handling-circular-references:
12451275

12461276
Collecting Type Errors While Denormalizing

0 commit comments

Comments
 (0)