From 1b0e274848be1ca7830b1b491d20557a535fbdff Mon Sep 17 00:00:00 2001 From: Christian Kolb Date: Tue, 28 Feb 2023 08:46:51 +0100 Subject: [PATCH 1/7] Add feature description --- components/serializer.rst | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/components/serializer.rst b/components/serializer.rst index aecf25fea4f..f707b558590 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -1242,6 +1242,30 @@ to ``true``:: $result = $normalizer->normalize($dummy, 'json', [AbstractObjectNormalizer::SKIP_NULL_VALUES => true]); // ['bar' => 'notNull'] +Preventing ``null`` value fallback for nullable properties +---------------------------------------------------------- + +By default, the Serializer will add `null` to nullable properties when the parameters for those are not provided. +You can change this behavior by setting the ``AbstractNormalizer::PREVENT_NULLABLE_FALLBACK`` context option +to ``true``:: + + class Dummy { + public function __construct( + public string $foo, + public ?string $bar, + ) + } + + $data = ['foo' => 'notNull']; + + $normalizer = new ObjectNormalizer(); + $result = $normalizer->denormalize($data, Dummy::class, 'json', [AbstractNormalizer::PREVENT_NULLABLE_FALLBACK => true]); + // throws Symfony\Component\Serializer\Exception\MissingConstructorArgumentException + +.. versionadded:: 6.3 + + The context flag was introduced in Symfony 6.3. + Skipping Uninitialized Properties --------------------------------- From 0b49d9beb10535711d4af54e57a23842a039b537 Mon Sep 17 00:00:00 2001 From: Christian Kolb Date: Tue, 28 Feb 2023 08:51:53 +0100 Subject: [PATCH 2/7] Fix broken code example --- components/serializer.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/components/serializer.rst b/components/serializer.rst index f707b558590..abd324a133a 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -1253,7 +1253,8 @@ to ``true``:: public function __construct( public string $foo, public ?string $bar, - ) + ) { + } } $data = ['foo' => 'notNull']; From b665eeb361d9becc7c6949f8c7250cbe6149d45d Mon Sep 17 00:00:00 2001 From: Christian Kolb Date: Tue, 28 Feb 2023 09:40:39 +0100 Subject: [PATCH 3/7] Update components/serializer.rst Co-authored-by: Oskar Stark --- components/serializer.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/components/serializer.rst b/components/serializer.rst index abd324a133a..bca997b235d 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -1265,7 +1265,8 @@ to ``true``:: .. versionadded:: 6.3 - The context flag was introduced in Symfony 6.3. + The ``AbstractNormalizer::PREVENT_NULLABLE_FALLBACK`` context option + was introduced in Symfony 6.3. Skipping Uninitialized Properties --------------------------------- From e1f5d7c0c465be18f7766f7c24a065c10b6d2c03 Mon Sep 17 00:00:00 2001 From: Christian Kolb Date: Tue, 28 Feb 2023 09:41:01 +0100 Subject: [PATCH 4/7] Update components/serializer.rst Co-authored-by: Oskar Stark --- components/serializer.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/serializer.rst b/components/serializer.rst index bca997b235d..1befe4b5c7a 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -1245,7 +1245,7 @@ to ``true``:: Preventing ``null`` value fallback for nullable properties ---------------------------------------------------------- -By default, the Serializer will add `null` to nullable properties when the parameters for those are not provided. +By default, the Serializer will add ``null`` to nullable properties when the parameters for those are not provided. You can change this behavior by setting the ``AbstractNormalizer::PREVENT_NULLABLE_FALLBACK`` context option to ``true``:: From 3b24418e4e3b3a02a513a5a1f1a39d7fc6de7ba6 Mon Sep 17 00:00:00 2001 From: Christian Kolb Date: Tue, 28 Feb 2023 09:41:09 +0100 Subject: [PATCH 5/7] Update components/serializer.rst Co-authored-by: Oskar Stark --- components/serializer.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/components/serializer.rst b/components/serializer.rst index 1befe4b5c7a..e9f60fdbcbc 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -1249,7 +1249,8 @@ By default, the Serializer will add ``null`` to nullable properties when the par You can change this behavior by setting the ``AbstractNormalizer::PREVENT_NULLABLE_FALLBACK`` context option to ``true``:: - class Dummy { + class Dummy + { public function __construct( public string $foo, public ?string $bar, From ea5c341111961442d2fb57672ce34fd05603d05c Mon Sep 17 00:00:00 2001 From: Christian Kolb Date: Wed, 1 Mar 2023 08:48:45 +0100 Subject: [PATCH 6/7] Update components/serializer.rst Co-authored-by: Oskar Stark --- components/serializer.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/serializer.rst b/components/serializer.rst index e9f60fdbcbc..84b5e8775b6 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -1242,7 +1242,7 @@ to ``true``:: $result = $normalizer->normalize($dummy, 'json', [AbstractObjectNormalizer::SKIP_NULL_VALUES => true]); // ['bar' => 'notNull'] -Preventing ``null`` value fallback for nullable properties +Preventing ``null`` Value Fallback for Nullable Properties ---------------------------------------------------------- By default, the Serializer will add ``null`` to nullable properties when the parameters for those are not provided. From 71a4ef468a5312692451db6bc2729d5c6e80dad8 Mon Sep 17 00:00:00 2001 From: Christian Kolb Date: Fri, 5 May 2023 17:44:20 +0200 Subject: [PATCH 7/7] Rename context flag --- components/serializer.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/components/serializer.rst b/components/serializer.rst index 84b5e8775b6..1b165134f52 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -1242,11 +1242,11 @@ to ``true``:: $result = $normalizer->normalize($dummy, 'json', [AbstractObjectNormalizer::SKIP_NULL_VALUES => true]); // ['bar' => 'notNull'] -Preventing ``null`` Value Fallback for Nullable Properties ----------------------------------------------------------- +Require all Properties +---------------------- By default, the Serializer will add ``null`` to nullable properties when the parameters for those are not provided. -You can change this behavior by setting the ``AbstractNormalizer::PREVENT_NULLABLE_FALLBACK`` context option +You can change this behavior by setting the ``AbstractNormalizer::REQUIRE_ALL_PROPERTIES`` context option to ``true``:: class Dummy @@ -1261,7 +1261,7 @@ to ``true``:: $data = ['foo' => 'notNull']; $normalizer = new ObjectNormalizer(); - $result = $normalizer->denormalize($data, Dummy::class, 'json', [AbstractNormalizer::PREVENT_NULLABLE_FALLBACK => true]); + $result = $normalizer->denormalize($data, Dummy::class, 'json', [AbstractNormalizer::REQUIRE_ALL_PROPERTIES => true]); // throws Symfony\Component\Serializer\Exception\MissingConstructorArgumentException .. versionadded:: 6.3