Skip to content

Commit

Permalink
Support value types in serialization
Browse files Browse the repository at this point in the history
Allows serialization of value types (i.e. `true` and `false`, as opposed to `bool`), including in union types (e.g. `string|false`).

Otherwise, tries to create class with FQCN "true" and, for union types, filters out value types as non-primitive.

Fixes #1568
  • Loading branch information
gregtyler committed Nov 12, 2024
1 parent a093b21 commit 62f7bb4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/GraphNavigator/SerializationGraphNavigator.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ public function accept($data, ?array $type = null)

case 'bool':
case 'boolean':
case 'true':
case 'false':
return $this->visitor->visitBoolean((bool) $data, $type);

case 'double':
Expand Down
2 changes: 2 additions & 0 deletions src/Metadata/Driver/TypedPropertiesDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ private function getDefaultWhiteList(): array
'float',
'bool',
'boolean',
'true',
'false',
'string',
'double',
'iterable',
Expand Down
2 changes: 2 additions & 0 deletions tests/Fixtures/TypedProperties/UnionTypedProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class UnionTypedProperties

private int|bool|float|string|null $nullableData;

private string|false $valueTypedUnion;

public function __construct($data)
{
$this->data = $data;
Expand Down
25 changes: 25 additions & 0 deletions tests/Metadata/Driver/UnionTypedPropertiesDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,31 @@ public function testInferUnionTypesShouldResultInManyTypes()
);
}

public function testInferUnionTypesShouldIncludeValueTypes()
{
$m = $this->resolve(UnionTypedProperties::class);

self::assertEquals(
[
'name' => 'union',
'params' =>
[
[
[
'name' => 'string',
'params' => [],
],
[
'name' => 'false',
'params' => [],
],
],
],
],
$m->propertyMetadata['valueTypedUnion']->type,
);
}

private function resolve(string $classToResolve): ClassMetadata
{
$namingStrategy = new IdenticalPropertyNamingStrategy();
Expand Down

0 comments on commit 62f7bb4

Please sign in to comment.