Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1fc575c

Browse files
committed
minor #15823 [Serializer] Documenting the new SKIP_UNINITIALIZED_VALUES option (vuryss, ivannemets-sravniru)
This PR was merged into the 5.4 branch. Discussion ---------- [Serializer] Documenting the new SKIP_UNINITIALIZED_VALUES option Documents changes introduced here: symfony/symfony#41615 Original issue: symfony/symfony#40578 Docs issue: #15785 Commits ------- 08599e8 Update serializer.rst 034f0f6 [Serializer] Documenting the new SKIP_UNINITIALIZED_VALUES option
2 parents e569eb9 + 08599e8 commit 1fc575c

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

components/serializer.rst

+29
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,35 @@ to ``true``::
12731273
$result = $normalizer->normalize($dummy, 'json', [AbstractObjectNormalizer::SKIP_NULL_VALUES => true]);
12741274
// ['bar' => 'notNull']
12751275

1276+
Skipping uninitialized properties
1277+
---------------------------------
1278+
1279+
PHP 7.4 introduced typed properties, which have a new state - ``uninitialized``.
1280+
This is different from the default ``null`` of untyped properties.
1281+
When you try to access it before giving it an explicit value - you get an error.
1282+
1283+
By default, to avoid the Serializer throwing an error when serializing or normalizing an object with
1284+
uninitialized properties, object normalizer catches these errors and ignores such properties.
1285+
1286+
You can disable this behavior by setting the ``AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES`` context option to ``false``::
1287+
1288+
class Dummy {
1289+
public string $foo = 'initialized';
1290+
public string $bar; // uninitialized
1291+
}
1292+
1293+
$normalizer = new ObjectNormalizer();
1294+
$result = $normalizer->normalize(new Dummy(), 'json', [AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES => false]);
1295+
// throws Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException as normalizer cannot read uninitialized properties
1296+
1297+
.. note::
1298+
1299+
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).
1300+
1301+
.. versionadded:: 5.4
1302+
1303+
The ``AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES`` constant was introduced in Symfony 5.4.
1304+
12761305
.. _component-serializer-handling-circular-references:
12771306

12781307
Collecting Type Errors While Denormalizing

0 commit comments

Comments
 (0)