Skip to content

Commit 1b0e274

Browse files
author
Christian Kolb
committed
Add feature description
1 parent 065fac4 commit 1b0e274

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

components/serializer.rst

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

1245+
Preventing ``null`` value fallback for nullable properties
1246+
----------------------------------------------------------
1247+
1248+
By default, the Serializer will add `null` to nullable properties when the parameters for those are not provided.
1249+
You can change this behavior by setting the ``AbstractNormalizer::PREVENT_NULLABLE_FALLBACK`` context option
1250+
to ``true``::
1251+
1252+
class Dummy {
1253+
public function __construct(
1254+
public string $foo,
1255+
public ?string $bar,
1256+
)
1257+
}
1258+
1259+
$data = ['foo' => 'notNull'];
1260+
1261+
$normalizer = new ObjectNormalizer();
1262+
$result = $normalizer->denormalize($data, Dummy::class, 'json', [AbstractNormalizer::PREVENT_NULLABLE_FALLBACK => true]);
1263+
// throws Symfony\Component\Serializer\Exception\MissingConstructorArgumentException
1264+
1265+
.. versionadded:: 6.3
1266+
1267+
The context flag was introduced in Symfony 6.3.
1268+
12451269
Skipping Uninitialized Properties
12461270
---------------------------------
12471271

0 commit comments

Comments
 (0)