8000 bug #45154 [Serializer] Fix AbstractObjectNormalizer not considering … · symfony/symfony@1199672 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1199672

Browse files
bug #45154 [Serializer] Fix AbstractObjectNormalizer not considering pseudo type false (Thomas Nunninger)
This PR was squashed before being merged into the 5.3 branch. Discussion ---------- [Serializer] Fix AbstractObjectNormalizer not considering pseudo type false | Q | A | ------------- | --- | Branch? | 5.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #45151 | License | MIT | Doc PR | AbstractObjectNormalizer does not consider pseudo type false (bug #45151) When you have a PHP 8.0 object where an attribute uses the pseudo type false (in a union type) you can't denormalize an array to that object. Commits ------- 27d9eff [Serializer] Fix AbstractObjectNormalizer not considering pseudo type false
2 parents 4565edb + 27d9eff commit 1199672

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,10 @@ private function validateAndDenormalize(string $currentClass, string $attribute,
537537
return (float) $data;
538538
}
539539

540+
if (Type::BUILTIN_TYPE_FALSE === $builtinType && false === $data) {
541+
return $data;
542+
}
543+
540544
if (('is_'.$builtinType)($data)) {
541545
return $data;
542546
}

src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
use Symfony\Component\Serializer\Tests\Normalizer\Features\ObjectToPopulateTestTrait;
5151
use Symfony\Component\Serializer\Tests\Normalizer\Features\SkipNullValuesTestTrait;
5252
use Symfony\Component\Serializer\Tests\Normalizer\Features\TypeEnforcementTestTrait;
53+
use Symfony\Component\Serializer\Tests\Php80Dummy;
5354

5455
/**
5556
* @author Kévin Dunglas <dunglas@gmail.com>
@@ -699,6 +700,25 @@ public function testExtractAttributesRespectsContext()
699700
$this->assertSame(['foo' => 'bar', 'bar' => 'foo'], $normalizer->normalize($data, null, ['include_foo_and_bar' => true]));
700701
}
701702

703+
/**
704+
* @requires PHP 8
705+
*/
706+
public function testDenormalizeFalsePseudoType()
707+
{
708+
// given a serializer that extracts the attribute types of an object via ReflectionExtractor
709+
$propertyTypeExtractor = new PropertyInfoExtractor([], [new ReflectionExtractor()], [], [], []);
710+
$objectNormalizer = new ObjectNormalizer(null, null, null, $propertyTypeExtractor);
711+
712+
$serializer = new Serializer([$objectNormalizer]);
713+
714+
// when denormalizing some data into an object where an attribute uses the false pseudo type
715+
/** @var Php80Dummy $object */
716+
$object = $serializer->denormalize(['canBeFalseOrString' => false], Php80Dummy::class);
717+
718+
// then the attribute that declared false was filled correctly
719+
$this->assertFalse($object->canBeFalseOrString);
720+
}
721+
702722
public function testAdvancedNameConverter()
703723
{
704724
$nameConverter = new class() implements AdvancedNameConverterInterface {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Symfony\Component\Serializer\Tests;
15+
16+
final class Php80Dummy
17+
{
18+
public false|string $canBeFalseOrString;
19+
}

0 commit comments

Comments
 (0)
0