Skip to content

Commit d7ffaf5

Browse files
ThomasNunningernicolas-grekas
authored andcommitted
[Serializer] Fix AbstractObjectNormalizer not considering pseudo type false
1 parent 2f33e6e commit d7ffaf5

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,11 @@ private function validateAndDenormalize(string $currentClass, string $attribute,
483483
return (float) $data;
484484
}
485485

486-
if ('false' === $builtinType || ('is_'.$builtinType)($data)) {
486+
if ('false' === $builtinType && false === $data) {
487+
return $data;
488+
}
489+
490+
if (('is_'.$builtinType)($data)) {
487491
return $data;
488492
}
489493
} catch (NotNormalizableValueException $e) {

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
use Symfony\Component\Serializer\Tests\Normalizer\Features\ObjectToPopulateTestTrait;
5353
use Symfony\Component\Serializer\Tests\Normalizer\Features\SkipNullValuesTestTrait;
5454
use Symfony\Component\Serializer\Tests\Normalizer\Features\TypeEnforcementTestTrait;
55+
use Symfony\Component\Serializer\Tests\Php80Dummy;
5556

5657
/**
5758
* @author Kévin Dunglas <[email protected]>
@@ -873,6 +874,25 @@ public function testExtractAttributesRespectsContext()
873874
$this->assertSame(['foo' => 'bar', 'bar' => 'foo'], $normalizer->normalize($data, null, ['include_foo_and_bar' => true]));
874875
}
875876

877+
/**
878+
* @requires PHP 8
879+
*/
880+
public function testDenormalizeFalsePseudoType()
881+
{
882+
// given a serializer that extracts the attribute types of an object via ReflectionExtractor
883+
$propertyTypeExtractor = new PropertyInfoExtractor([], [new ReflectionExtractor()], [], [], []);
884+
$objectNormalizer = new ObjectNormalizer(null, null, null, $propertyTypeExtractor);
885+
886+
$serializer = new Serializer([$objectNormalizer]);
887+
888+
// when denormalizing some data into an object where an attribute uses the false pseudo type
889+
/** @var Php80Dummy $object */
890+
$object = $serializer->denormalize(['canBeFalseOrString' => false], Php80Dummy::class);
891+
892+
// then the attribute that declared false was filled correctly
893+
$this->assertFalse($object->canBeFalseOrString);
894+
}
895+
876896
public function testAdvancedNameConverter()
877897
{
878898
$nameConverter = new class() implements AdvancedNameConverterInterface {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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+
namespace Symfony\Component\Serializer\Tests;
13+
14+
final class Php80Dummy
15+
{
16+
public false|string $canBeFalseOrString;
17+
}

0 commit comments

Comments
 (0)