From d6ae837d855ee8817135b02e8f557370e3772e04 Mon Sep 17 00:00:00 2001 From: Bob van de Vijver Date: Thu, 19 Dec 2024 10:33:57 +0100 Subject: [PATCH] Add true/false as primitive types --- src/Handler/UnionHandler.php | 8 +++++++- .../TypedProperties/UnionTypedProperties.php | 6 ++++-- .../Serializer/BaseSerializationTestCase.php | 4 ++-- tests/Serializer/JsonSerializationTest.php | 19 +++++++++++++------ tests/Serializer/XmlSerializationTest.php | 2 +- .../xml/union_typed_properties_integer.xml | 5 +++++ 6 files changed, 32 insertions(+), 12 deletions(-) create mode 100644 tests/Serializer/xml/union_typed_properties_integer.xml diff --git a/src/Handler/UnionHandler.php b/src/Handler/UnionHandler.php index ca6045ad5..f5f9ffc2e 100644 --- a/src/Handler/UnionHandler.php +++ b/src/Handler/UnionHandler.php @@ -115,7 +115,7 @@ private function matchSimpleType(mixed $data, array $type, Context $context): mi private function isPrimitiveType(string $type): bool { - return in_array($type, ['int', 'integer', 'float', 'double', 'bool', 'boolean', 'string', 'array'], true); + return in_array($type, ['int', 'integer', 'float', 'double', 'bool', 'boolean', 'true', 'false', 'string', 'array'], true); } private function testPrimitive(mixed $data, string $type, string $format): bool @@ -136,6 +136,12 @@ private function testPrimitive(mixed $data, string $type, string $format): bool case 'boolean': return (string) (bool) $data === (string) $data; + case 'true': + return true === $data; + + case 'false': + return false === $data; + case 'string': return is_string($data); } diff --git a/tests/Fixtures/TypedProperties/UnionTypedProperties.php b/tests/Fixtures/TypedProperties/UnionTypedProperties.php index d8310e865..c3f26c8cb 100644 --- a/tests/Fixtures/TypedProperties/UnionTypedProperties.php +++ b/tests/Fixtures/TypedProperties/UnionTypedProperties.php @@ -8,12 +8,14 @@ class UnionTypedProperties { private int|bool|float|string|array $data; - private int|bool|float|string|null $nullableData; + private int|bool|float|string|null $nullableData = null; private string|false $valueTypedUnion; - public function __construct($data) + public function __construct($data, $nullableData, $valueTypedUnion) { $this->data = $data; + $this->nullableData = $nullableData; + $this->valueTypedUnion = $valueTypedUnion; } } diff --git a/tests/Serializer/BaseSerializationTestCase.php b/tests/Serializer/BaseSerializationTestCase.php index 52626e14e..9f7c99784 100644 --- a/tests/Serializer/BaseSerializationTestCase.php +++ b/tests/Serializer/BaseSerializationTestCase.php @@ -1973,9 +1973,9 @@ public function testSerializingUnionTypedProperties() $this->markTestSkipped(sprintf('%s requires PHP 8.0', TypedPropertiesDriver::class)); } - $object = new TypedProperties\UnionTypedProperties(10000); + $object = new TypedProperties\UnionTypedProperties(10000, null, false); - self::assertEquals(static::getContent('data_integer'), $this->serialize($object)); + self::assertEquals(static::getContent('union_typed_properties_integer'), $this->serialize($object)); } public function testSerializingUnionDocBlockTypesProperties() diff --git a/tests/Serializer/JsonSerializationTest.php b/tests/Serializer/JsonSerializationTest.php index f30319dcf..0e04797e9 100644 --- a/tests/Serializer/JsonSerializationTest.php +++ b/tests/Serializer/JsonSerializationTest.php @@ -167,6 +167,12 @@ protected static function getContent($key) $outputs['object_with_enums'] = '{"ordinary":"Clubs","backed_value":"C","backed_without_param":"C","ordinary_array":["Clubs","Spades"],"backed_array":["C","H"],"backed_array_without_param":["C","H"],"ordinary_auto_detect":"Clubs","backed_auto_detect":"C","backed_int_auto_detect":3,"backed_int":3,"backed_name":"C","backed_int_forced_str":3}'; $outputs['object_with_autodetect_enums'] = '{"ordinary_array_auto_detect":["Clubs","Spades"],"backed_array_auto_detect":["C","H"],"mixed_array_auto_detect":["Clubs","H"]}'; $outputs['object_with_enums_disabled'] = '{"ordinary_array_auto_detect":[{"name":"Clubs"},{"name":"Spades"}],"backed_array_auto_detect":[{"name":"Clubs","value":"C"},{"name":"Hearts","value":"H"}],"mixed_array_auto_detect":[{"name":"Clubs"},{"name":"Hearts","value":"H"}]}'; + $outputs['union_typed_properties_integer'] = '{"data":10000,"value_typed_union":false}'; + $outputs['union_typed_properties_float'] = '{"data":1.236,"value_typed_union":false}'; + $outputs['union_typed_properties_bool'] = '{"data":false,"value_typed_union":false}'; + $outputs['union_typed_properties_string'] = '{"data":"foo","value_typed_union":false}'; + $outputs['union_typed_properties_array'] = '{"data":[1,2,3],"value_typed_union":false}'; + $outputs['union_typed_properties_false_string'] = '{"data":false,"value_typed_union":"foo"}'; } if (!isset($outputs[$key])) { @@ -446,11 +452,12 @@ public static function getTypeHintedArraysAndStdClass() public static function getSimpleUnionProperties(): iterable { - yield 'int' => [10000, 'data_integer']; - yield [1.236, 'data_float']; - yield [false, 'data_bool']; - yield ['foo', 'data_string']; - yield [[1, 2, 3], 'data_array']; + yield 'int' => [[10000, null, false], 'union_typed_properties_integer']; + yield 'float' => [[1.236, null, false], 'union_typed_properties_float']; + yield 'bool' => [[false, null, false], 'union_typed_properties_bool']; + yield 'string' => [['foo', null, false], 'union_typed_properties_string']; + yield 'array' => [[[1, 2, 3], null, false], 'union_typed_properties_array']; + yield 'false_array' => [[false, null, "foo"], 'union_typed_properties_false_string']; } /** @@ -465,7 +472,7 @@ public function testUnionProperties($data, string $expected): void return; } - $object = new UnionTypedProperties($data); + $object = new UnionTypedProperties(...$data); self::assertEquals($object, $this->deserialize(static::getContent($expected), UnionTypedProperties::class)); self::assertEquals($this->serialize($object), static::getContent($expected)); } diff --git a/tests/Serializer/XmlSerializationTest.php b/tests/Serializer/XmlSerializationTest.php index 833a2a06f..11a3da373 100644 --- a/tests/Serializer/XmlSerializationTest.php +++ b/tests/Serializer/XmlSerializationTest.php @@ -616,7 +616,7 @@ public function testThrowingExceptionWhenDeserializingUnionProperties() $this->expectException(RuntimeException::class); - $object = new UnionTypedProperties(10000); + $object = new UnionTypedProperties(10000, null, false); self::assertEquals($object, $this->deserialize(static::getContent('data_integer'), UnionTypedProperties::class)); } diff --git a/tests/Serializer/xml/union_typed_properties_integer.xml b/tests/Serializer/xml/union_typed_properties_integer.xml new file mode 100644 index 000000000..250c45a40 --- /dev/null +++ b/tests/Serializer/xml/union_typed_properties_integer.xml @@ -0,0 +1,5 @@ + + + 10000 + false +