Skip to content

Commit 27d9eff

Browse files
ThomasNunningernicolas-grekas
authored andcommitted
[Serializer] Fix AbstractObjectNormalizer not considering pseudo type false
1 parent b460e07 commit 27d9eff

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

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

+4
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

+20
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 {
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)