Skip to content

Commit 6563914

Browse files
committed
Merge branch '6.0' into 6.1
* 6.0: Update serializer.rst [Serializer] Documenting the new SKIP_UNINITIALIZED_VALUES option
2 parents 57ab485 + d7c786b commit 6563914

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
@@ -1231,6 +1231,36 @@ to ``true``::
12311231
$result = $normalizer->normalize($dummy, 'json', [AbstractObjectNormalizer::SKIP_NULL_VALUES => true]);
12321232
// ['bar' => 'notNull']
12331233

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

12361266
Collecting Type Errors While Denormalizing

0 commit comments

Comments
 (0)