Skip to content

Commit 0608808

Browse files
[Serializer][PropertyInfo] Add support of true built-in type (from PHP 8.2)
1 parent 6e1675d commit 0608808

File tree

6 files changed

+37
-3
lines changed

6 files changed

+37
-3
lines changed

src/Symfony/Component/PropertyInfo/Tests/Extractor/ReflectionExtractorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ public function php82TypesProvider()
313313
{
314314
yield ['nil', null];
315315
yield ['false', [new Type(Type::BUILTIN_TYPE_FALSE)]];
316+
yield ['true', [new Type(Type::BUILTIN_TYPE_TRUE)]];
316317
}
317318

318319
/**

src/Symfony/Component/PropertyInfo/Tests/Fixtures/Php82Dummy.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ class Php82Dummy
1616
public null $nil = null;
1717

1818
public false $false = false;
19+
20+
public true $true = true;
1921
}

src/Symfony/Component/PropertyInfo/Type.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ class Type
2828
public const BUILTIN_TYPE_OBJECT = 'object';
2929
public const BUILTIN_TYPE_ARRAY = 'array';
3030
public const BUILTIN_TYPE_NULL = 'null';
31-
public const BUILTIN_TYPE_FALSE = 'false';
3231
public const BUILTIN_TYPE_TRUE = 'true';
32+
public const BUILTIN_TYPE_FALSE = 'false';
3333
public const BUILTIN_TYPE_CALLABLE = 'callable';
3434
public const BUILTIN_TYPE_ITERABLE = 'iterable';
3535

@@ -47,8 +47,8 @@ class Type
4747
self::BUILTIN_TYPE_OBJECT,
4848
self::BUILTIN_TYPE_ARRAY,
4949
self::BUILTIN_TYPE_CALLABLE,
50-
self::BUILTIN_TYPE_FALSE,
5150
self::BUILTIN_TYPE_TRUE,
51+
self::BUILTIN_TYPE_FALSE,
5252
self::BUILTIN_TYPE_NULL,
5353
self::BUILTIN_TYPE_ITERABLE,
5454
];

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ private function validateAndDenormalize(array $types, string $currentClass, stri
564564
return (float) $data;
565565
}
566566

567-
if (Type::BUILTIN_TYPE_FALSE === $builtinType && false === $data) {
567+
if ((Type::BUILTIN_TYPE_FALSE === $builtinType && false === $data) || (Type::BUILTIN_TYPE_TRUE === $builtinType && true === $data)) {
568568
return $data;
569569
}
570570

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 <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+
namespace Symfony\Component\Serializer\Tests\Fixtures;
13+
14+
class TrueBuiltInDummy
15+
{
16+
public true $true = true;
17+
}

src/Symfony/Component/Serializer/Tests/SerializerTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
use Symfony\Component\Serializer\Tests\Fixtures\Php74Full;
6262
use Symfony\Component\Serializer\Tests\Fixtures\Php80WithPromotedTypedConstructor;
6363
use Symfony\Component\Serializer\Tests\Fixtures\TraversableDummy;
64+
use Symfony\Component\Serializer\Tests\Fixtures\TrueBuiltInDummy;
6465
use Symfony\Component\Serializer\Tests\Normalizer\TestDenormalizer;
6566
use Symfony\Component\Serializer\Tests\Normalizer\TestNormalizer;
6667

@@ -764,6 +765,19 @@ public function testFalseBuiltInTypes()
764765
$this->assertEquals(new FalseBuiltInDummy(), $actual);
765766
}
766767

768+
/**
769+
* @requires PHP 8.2
770+
*/
771+
public function testTrueBuiltInTypes()
772+
{
773+
$extractor = new PropertyInfoExtractor([], [new ReflectionExtractor()]);
774+
$serializer = new Serializer([new ObjectNormalizer(null, null, null, $extractor)], ['json' => new JsonEncoder()]);
775+
776+
$actual = $serializer->deserialize('{"true":true}', TrueBuiltInDummy::class, 'json');
777+
778+
$this->assertEquals(new TrueBuiltInDummy(), $actual);
779+
}
780+
767781
private function serializerWithClassDiscriminator()
768782
{
769783
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));

0 commit comments

Comments
 (0)