diff --git a/src/Handler/UnionHandler.php b/src/Handler/UnionHandler.php index ba57f339e..b2934fcee 100644 --- a/src/Handler/UnionHandler.php +++ b/src/Handler/UnionHandler.php @@ -131,7 +131,7 @@ private function testPrimitive(mixed $data, string $type, string $format): bool case 'bool': case 'boolean': - return (string) (bool) $data === (string) $data; + return (string) (bool) $data === (string) $data && 1 !== $data; // prevent false positive for 1/true case 'string': return (string) $data === (string) $data; diff --git a/src/Metadata/Driver/TypedPropertiesDriver.php b/src/Metadata/Driver/TypedPropertiesDriver.php index 2e7af8a66..9c9818274 100644 --- a/src/Metadata/Driver/TypedPropertiesDriver.php +++ b/src/Metadata/Driver/TypedPropertiesDriver.php @@ -60,12 +60,12 @@ public function __construct(DriverInterface $delegate, ?ParserInterface $typePar private function reorderTypes(array $types): array { uasort($types, static function ($a, $b) { - $order = ['null' => 0, 'true' => 1, 'false' => 2, 'bool' => 3, 'int' => 4, 'float' => 5, 'string' => 6]; + $order = ['null' => 0, 'true' => 1, 'false' => 2, 'int' => 3, 'float' => 4, 'bool' => 5, 'string' => 6]; return ($order[$a['name']] ?? 7) <=> ($order[$b['name']] ?? 7); }); - return $types; + return \array_values($types); } private function getDefaultWhiteList(): array diff --git a/tests/Metadata/Driver/UnionTypedPropertiesDriverTest.php b/tests/Metadata/Driver/UnionTypedPropertiesDriverTest.php index 1da90bfd4..9c745ee02 100644 --- a/tests/Metadata/Driver/UnionTypedPropertiesDriverTest.php +++ b/tests/Metadata/Driver/UnionTypedPropertiesDriverTest.php @@ -28,16 +28,12 @@ public function testInferUnionTypesShouldResultInManyTypes() { $m = $this->resolve(UnionTypedProperties::class); - self::assertEquals( + self::assertSame( [ 'name' => 'union', 'params' => [ [ - [ - 'name' => 'string', - 'params' => [], - ], [ 'name' => 'int', 'params' => [], @@ -50,6 +46,10 @@ public function testInferUnionTypesShouldResultInManyTypes() 'name' => 'bool', 'params' => [], ], + [ + 'name' => 'string', + 'params' => [], + ], ], ], ], @@ -61,18 +61,18 @@ public function testInferUnionTypesShouldIncludeValueTypes() { $m = $this->resolve(UnionTypedProperties::class); - self::assertEquals( + self::assertSame( [ 'name' => 'union', 'params' => [ [ [ - 'name' => 'string', + 'name' => 'false', 'params' => [], ], [ - 'name' => 'false', + 'name' => 'string', 'params' => [], ], ], diff --git a/tests/Serializer/JsonSerializationTest.php b/tests/Serializer/JsonSerializationTest.php index b41c771df..4e819e29e 100644 --- a/tests/Serializer/JsonSerializationTest.php +++ b/tests/Serializer/JsonSerializationTest.php @@ -151,6 +151,7 @@ protected static function getContent($key) $outputs['uninitialized_typed_props'] = '{"virtual_role":{},"id":1,"role":{},"tags":[]}'; $outputs['custom_datetimeinterface'] = '{"custom":"2021-09-07"}'; $outputs['data_integer'] = '{"data":10000}'; + $outputs['data_integer_one'] = '{"data":1}'; $outputs['data_float'] = '{"data":1.236}'; $outputs['data_bool'] = '{"data":false}'; $outputs['data_string'] = '{"data":"foo"}'; @@ -454,6 +455,9 @@ public function testDeserializingUnionProperties() $object = new UnionTypedProperties(10000); self::assertEquals($object, $this->deserialize(static::getContent('data_integer'), UnionTypedProperties::class)); + $object = new UnionTypedProperties(1); + self::assertEquals($object, $this->deserialize(static::getContent('data_integer_one'), UnionTypedProperties::class)); + $object = new UnionTypedProperties(1.236); self::assertEquals($object, $this->deserialize(static::getContent('data_float'), UnionTypedProperties::class)); @@ -475,6 +479,9 @@ public function testSerializingUnionProperties() $serialized = $this->serialize(new UnionTypedProperties(10000)); self::assertEquals(static::getContent('data_integer'), $serialized); + $serialized = $this->serialize(new UnionTypedProperties(1)); + self::assertEquals(static::getContent('data_integer_one'), $serialized); + $serialized = $this->serialize(new UnionTypedProperties(1.236)); self::assertEquals(static::getContent('data_float'), $serialized);