Skip to content

Commit

Permalink
[Serializer] Fix AbstractObjectNormalizer not considering pseudo type…
Browse files Browse the repository at this point in the history
… false
  • Loading branch information
ThomasNunninger authored and nicolas-grekas committed Jan 26, 2022
1 parent 8a2c496 commit 3613939
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Normalizer/AbstractObjectNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,10 @@ private function validateAndDenormalize(string $currentClass, string $attribute,
return (float) $data;
}

if (Type::BUILTIN_TYPE_FALSE === $builtinType && false === $data) {
return $data;
}

if (('is_'.$builtinType)($data)) {
return $data;
}
Expand Down
20 changes: 20 additions & 0 deletions Tests/Normalizer/ObjectNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
use Symfony\Component\Serializer\Tests\Normalizer\Features\ObjectToPopulateTestTrait;
use Symfony\Component\Serializer\Tests\Normalizer\Features\SkipNullValuesTestTrait;
use Symfony\Component\Serializer\Tests\Normalizer\Features\TypeEnforcementTestTrait;
use Symfony\Component\Serializer\Tests\Php80Dummy;

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

/**
* @requires PHP 8
*/
public function testDenormalizeFalsePseudoType()
{
// given a serializer that extracts the attribute types of an object via ReflectionExtractor
$propertyTypeExtractor = new PropertyInfoExtractor([], [new ReflectionExtractor()], [], [], []);
$objectNormalizer = new ObjectNormalizer(null, null, null, $propertyTypeExtractor);

$serializer = new Serializer([$objectNormalizer]);

// when denormalizing some data into an object where an attribute uses the false pseudo type
/** @var Php80Dummy $object */
$object = $serializer->denormalize(['canBeFalseOrString' => false], Php80Dummy::class);

// then the attribute that declared false was filled correctly
$this->assertFalse($object->canBeFalseOrString);
}

public function testAdvancedNameConverter()
{
$nameConverter = new class() implements AdvancedNameConverterInterface {
Expand Down
19 changes: 19 additions & 0 deletions Tests/Php80Dummy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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.
*/

declare(strict_types=1);

namespace Symfony\Component\Serializer\Tests;

final class Php80Dummy
{
public false|string $canBeFalseOrString;
}

0 comments on commit 3613939

Please sign in to comment.