Skip to content

[Serializer] Fix AbstractObjectNormalizer not considering pseudo type false #45154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 src/Symfony/Component/Serializer/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;
}