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 May 10, 2022
1 parent f437a51 commit 234c6d0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Normalizer/AbstractObjectNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,11 @@ private function validateAndDenormalize(string $currentClass, string $attribute,
return (float) $data;
}

if ('false' === $builtinType || ('is_'.$builtinType)($data)) {
if ('false' === $builtinType && false === $data) {
return $data;
}

if (('is_'.$builtinType)($data)) {
return $data;
}
} catch (NotNormalizableValueException $e) {
Expand Down
20 changes: 20 additions & 0 deletions Tests/Normalizer/ObjectNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,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 @@ -873,6 +874,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
17 changes: 17 additions & 0 deletions Tests/Php80Dummy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?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;

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

0 comments on commit 234c6d0

Please sign in to comment.