Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support value types in serialization #1569

Merged
merged 3 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/GraphNavigator/DeserializationGraphNavigator.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ public function accept($data, ?array $type = null)

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

case 'double':
Expand Down
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
16 changes: 16 additions & 0 deletions tests/Fixtures/DataFalse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace JMS\Serializer\Tests\Fixtures;

class DataFalse
{
public false $data;

public function __construct(
false $data
) {
$this->data = $data;
}
}
16 changes: 16 additions & 0 deletions tests/Fixtures/DataTrue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace JMS\Serializer\Tests\Fixtures;

class DataTrue
{
public true $data;

public function __construct(
true $data
) {
$this->data = $data;
}
}
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
49 changes: 49 additions & 0 deletions tests/Serializer/JsonSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use JMS\Serializer\SerializationContext;
use JMS\Serializer\Tests\Fixtures\Author;
use JMS\Serializer\Tests\Fixtures\AuthorList;
use JMS\Serializer\Tests\Fixtures\DataFalse;
use JMS\Serializer\Tests\Fixtures\DataTrue;
use JMS\Serializer\Tests\Fixtures\DiscriminatedAuthor;
use JMS\Serializer\Tests\Fixtures\DiscriminatedComment;
use JMS\Serializer\Tests\Fixtures\FirstClassMapCollection;
Expand All @@ -27,6 +29,7 @@
use JMS\Serializer\Visitor\Factory\JsonSerializationVisitorFactory;
use JMS\Serializer\Visitor\SerializationVisitorInterface;
use PHPUnit\Framework\Attributes\DataProvider;
use TypeError;

class JsonSerializationTest extends BaseSerializationTestCase
{
Expand Down Expand Up @@ -151,6 +154,8 @@ protected static function getContent($key)
$outputs['data_float'] = '{"data":1.236}';
$outputs['data_bool'] = '{"data":false}';
$outputs['data_string'] = '{"data":"foo"}';
$outputs['data_true'] = '{"data":true}';
$outputs['data_false'] = '{"data":false}';
$outputs['data_author'] = '{"data":{"full_name":"foo"}}';
$outputs['data_comment'] = '{"data":{"author":{"full_name":"foo"},"text":"bar"}}';
$outputs['data_discriminated_author'] = '{"data":{"full_name":"foo","objectType":"author"}}';
Expand Down Expand Up @@ -480,6 +485,50 @@ public function testSerializingUnionProperties()
self::assertEquals(static::getContent('data_string'), $serialized);
}

public function testTrueDataType()
{
if (PHP_VERSION_ID < 80200) {
$this->markTestSkipped('True type requires PHP 8.2');

return;
}

self::assertEquals(
static::getContent('data_true'),
$this->serialize(new DataTrue(true)),
);

self::assertEquals(
new DataTrue(true),
$this->deserialize(static::getContent('data_true'), DataTrue::class),
);

$this->expectException(TypeError::class);
$this->deserialize(static::getContent('data_false'), DataTrue::class);
}

public function testFalseDataType()
{
if (PHP_VERSION_ID < 80200) {
$this->markTestSkipped('False type requires PHP 8.2');

return;
}

self::assertEquals(
static::getContent('data_false'),
$this->serialize(new DataFalse(false)),
);

self::assertEquals(
new DataFalse(false),
$this->deserialize(static::getContent('data_false'), DataFalse::class),
);

$this->expectException(TypeError::class);
$this->deserialize(static::getContent('data_true'), DataFalse::class);
}

public function testDeserializingComplexDiscriminatedUnionProperties()
{
if (PHP_VERSION_ID < 80000) {
Expand Down
Loading