Skip to content

Commit 3c4de50

Browse files
[Serializer][PropertyInfo] Add support of false built-in type (from PHP 8.2)
1 parent 7f0edd6 commit 3c4de50

File tree

7 files changed

+64
-1
lines changed

7 files changed

+64
-1
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,23 @@ public function php81TypesProvider()
272272
];
273273
}
274274

275+
/**
276+
* @dataProvider php82TypesProvider
277+
* @requires PHP 8.2
278+
*/
279+
public function testExtractPhp82Type($property, array $type = null)
280+
{
281+
$this->assertEquals($type, $this->extractor->getTypes('Symfony\Component\PropertyInfo\Tests\Fixtures\Php82Dummy', $property, []));
282+
}
283+
284+
public function php82TypesProvider()
285+
{
286+
if (\PHP_VERSION_ID >= 80200) {
287+
yield ['nil', null];
288+
yield ['false', [new Type(Type::BUILTIN_TYPE_FALSE)]];
289+
}
290+
}
291+
275292
/**
276293
* @dataProvider defaultValueProvider
277294
*/
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Symfony\Component\PropertyInfo\Tests\Fixtures;
4+
5+
class Php82Dummy
6+
{
7+
public null $nil = null;
8+
9+
public false $false = false;
10+
}

src/Symfony/Component/PropertyInfo/Tests/TypeTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,15 @@ public function testInvalidType()
4949
$this->expectExceptionMessage('"foo" is not a valid PHP type.');
5050
new Type('foo');
5151
}
52+
53+
public function testFalseType()
54+
{
55+
if (\PHP_VERSION_ID < 80200) {
56+
$this->expectException(\InvalidArgumentException::class);
57+
$this->expectExceptionMessage('"false" is not a valid PHP type.');
58+
}
59+
60+
$type = new Type('false');
61+
$this->assertEquals(Type::BUILTIN_TYPE_FALSE, $type->getBuiltinType());
62+
}
5263
}

src/Symfony/Component/PropertyInfo/Type.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ 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';
3132
public const BUILTIN_TYPE_CALLABLE = 'callable';
3233
public const BUILTIN_TYPE_ITERABLE = 'iterable';
3334

@@ -61,6 +62,10 @@ class Type
6162
*/
6263
public function __construct(string $builtinType, bool $nullable = false, string $class = null, bool $collection = false, self $collectionKeyType = null, self $collectionValueType = null)
6364
{
65+
if (\PHP_VERSION_ID >= 80200) {
66+
static::$builtinTypes[] = self::BUILTIN_TYPE_FALSE;
67+
}
68+
6469
if (!\in_array($builtinType, self::$builtinTypes)) {
6570
throw new \InvalidArgumentException(sprintf('"%s" is not a valid PHP type.', $builtinType));
6671
}

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

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

486-
if (('is_'.$builtinType)($data)) {
486+
if (Type::BUILTIN_TYPE_FALSE === $builtinType || ('is_'.$builtinType)($data)) {
487487
return $data;
488488
}
489489
} catch (NotNormalizableValueException $e) {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Symfony\Component\Serializer\Tests\Fixtures;
4+
5+
class FalseBuiltInDummy
6+
{
7+
public false $false = false;
8+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageInterface;
5252
use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberOne;
5353
use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberTwo;
54+
use Symfony\Component\Serializer\Tests\Fixtures\FalseBuiltInDummy;
5455
use Symfony\Component\Serializer\Tests\Fixtures\NormalizableTraversableDummy;
5556
use Symfony\Component\Serializer\Tests\Fixtures\TraversableDummy;
5657
use Symfony\Component\Serializer\Tests\Normalizer\TestDenormalizer;
@@ -572,6 +573,17 @@ public function testUnionTypeDeserializable()
572573
$this->assertEquals(new DummyUnionType(), $actual, 'Union type denormalization third case failed.');
573574
}
574575

576+
/**
577+
* @requires PHP 8.2
578+
*/
579+
public function testFalseBuiltInTypes()
580+
{
581+
$extractor = new PropertyInfoExtractor([], [new ReflectionExtractor()]);
582+
$serializer = new Serializer([new ObjectNormalizer(null, null, null, $extractor)], ['json' => new JsonEncoder()]);
583+
584+
$serializer->deserialize('{"false":false}', FalseBuiltInDummy::class, 'json');
585+
}
586+
575587
private function serializerWithClassDiscriminator()
576588
{
577589
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));

0 commit comments

Comments
 (0)