-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug #52172 [Serializer] Fix denormalizing empty string into `object|n…
…ull` parameter (Jeroeny) This PR was merged into the 5.4 branch. Discussion ---------- [Serializer] Fix denormalizing empty string into `object|null` parameter | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | License | MIT > In XML and CSV all basic datatypes are represented as strings The `AbstractObjectNormalizer` handles this for bool, int and float types. But if a parameter is typed `Object|null`, a serialization and then deserialization will fail. This will throw: ```php final class Xml { public function __construct(public Uuid|null $element = null) { } } $test = new Xml(null); $serialized = $this->serializer->serialize($test, XmlEncoder::FORMAT); $deserialized = $this->serializer->deserialize($serialized, Xml::class, XmlEncoder::FORMAT); ``` ``` [Symfony\Component\Serializer\Exception\NotNormalizableValueException] The data is not a valid "Symfony\Component\Uid\Uuid" string representation. ``` Reproducer: https://github.com/Jeroeny/reproduce/blob/xmlnull/src/Test.php Commits ------- 62f2203 Fix denormalizing empty string into object|null parameter
- Loading branch information
Showing
9 changed files
with
245 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/Symfony/Component/Serializer/Tests/Fixtures/DummyString.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Serializer\Tests\Fixtures; | ||
|
||
use Symfony\Component\Serializer\Normalizer\DenormalizableInterface; | ||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; | ||
|
||
/** | ||
* @author Jeroen <github.com/Jeroeny> | ||
*/ | ||
class DummyString implements DenormalizableInterface | ||
{ | ||
/** @var string $value */ | ||
public $value; | ||
|
||
public function denormalize(DenormalizerInterface $denormalizer, $data, string $format = null, array $context = []) | ||
{ | ||
$this->value = $data; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/Symfony/Component/Serializer/Tests/Fixtures/DummyWithNotNormalizable.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Serializer\Tests\Fixtures; | ||
|
||
/** | ||
* @author Jeroen <github.com/Jeroeny> | ||
*/ | ||
class DummyWithNotNormalizable | ||
{ | ||
public function __construct(public NotNormalizableDummy|null $value) | ||
{ | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/Symfony/Component/Serializer/Tests/Fixtures/DummyWithObjectOrBool.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Serializer\Tests\Fixtures; | ||
|
||
/** | ||
* @author Jeroen <github.com/Jeroeny> | ||
*/ | ||
class DummyWithObjectOrBool | ||
{ | ||
public function __construct(public Php80WithPromotedTypedConstructor|bool $value) | ||
{ | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/Symfony/Component/Serializer/Tests/Fixtures/DummyWithObjectOrNull.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Serializer\Tests\Fixtures; | ||
|
||
/** | ||
* @author Jeroen <github.com/Jeroeny> | ||
*/ | ||
class DummyWithObjectOrNull | ||
{ | ||
public function __construct(public Php80WithPromotedTypedConstructor|null $value) | ||
{ | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/Symfony/Component/Serializer/Tests/Fixtures/DummyWithStringObject.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Serializer\Tests\Fixtures; | ||
|
||
/** | ||
* @author Jeroen <github.com/Jeroeny> | ||
*/ | ||
class DummyWithStringObject | ||
{ | ||
public function __construct(public DummyString|null $value) | ||
{ | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Symfony/Component/Serializer/Tests/Fixtures/NotNormalizableDummy.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Serializer\Tests\Fixtures; | ||
|
||
use Symfony\Component\Serializer\Exception\NotNormalizableValueException; | ||
use Symfony\Component\Serializer\Normalizer\DenormalizableInterface; | ||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; | ||
|
||
/** | ||
* @author Jeroen <github.com/Jeroeny> | ||
*/ | ||
class NotNormalizableDummy implements DenormalizableInterface | ||
{ | ||
public function __construct() | ||
{ | ||
} | ||
|
||
public function denormalize(DenormalizerInterface $denormalizer, $data, string $format = null, array $context = []) | ||
{ | ||
throw new NotNormalizableValueException(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters