Skip to content

Commit 6b86447

Browse files
authored
Merge pull request #1569 from gregtyler/support-value-types
Support value types in serialization
2 parents a093b21 + 9538cce commit 6b86447

File tree

8 files changed

+114
-0
lines changed

8 files changed

+114
-0
lines changed

src/GraphNavigator/DeserializationGraphNavigator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ public function accept($data, ?array $type = null)
128128

129129
case 'bool':
130130
case 'boolean':
131+
case 'false':
132+
case 'true':
131133
return $this->visitor->visitBoolean($data, $type);
132134

133135
case 'double':

src/GraphNavigator/SerializationGraphNavigator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ public function accept($data, ?array $type = null)
156156

157157
case 'bool':
158158
case 'boolean':
159+
case 'true':
160+
case 'false':
159161
return $this->visitor->visitBoolean((bool) $data, $type);
160162

161163
case 'double':

src/Metadata/Driver/TypedPropertiesDriver.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ private function getDefaultWhiteList(): array
7575
'float',
7676
'bool',
7777
'boolean',
78+
'true',
79+
'false',
7880
'string',
7981
'double',
8082
'iterable',

tests/Fixtures/DataFalse.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace JMS\Serializer\Tests\Fixtures;
6+
7+
class DataFalse
8+
{
9+
public false $data;
10+
11+
public function __construct(
12+
false $data
13+
) {
14+
$this->data = $data;
15+
}
16+
}

tests/Fixtures/DataTrue.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace JMS\Serializer\Tests\Fixtures;
6+
7+
class DataTrue
8+
{
9+
public true $data;
10+
11+
public function __construct(
12+
true $data
13+
) {
14+
$this->data = $data;
15+
}
16+
}

tests/Fixtures/TypedProperties/UnionTypedProperties.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class UnionTypedProperties
1010

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

13+
private string|false $valueTypedUnion;
14+
1315
public function __construct($data)
1416
{
1517
$this->data = $data;

tests/Metadata/Driver/UnionTypedPropertiesDriverTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,31 @@ public function testInferUnionTypesShouldResultInManyTypes()
5757
);
5858
}
5959

60+
public function testInferUnionTypesShouldIncludeValueTypes()
61+
{
62+
$m = $this->resolve(UnionTypedProperties::class);
63+
64+
self::assertEquals(
65+
[
66+
'name' => 'union',
67+
'params' =>
68+
[
69+
[
70+
[
71+
'name' => 'string',
72+
'params' => [],
73+
],
74+
[
75+
'name' => 'false',
76+
'params' => [],
77+
],
78+
],
79+
],
80+
],
81+
$m->propertyMetadata['valueTypedUnion']->type,
82+
);
83+
}
84+
6085
private function resolve(string $classToResolve): ClassMetadata
6186
{
6287
$namingStrategy = new IdenticalPropertyNamingStrategy();

tests/Serializer/JsonSerializationTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use JMS\Serializer\SerializationContext;
1616
use JMS\Serializer\Tests\Fixtures\Author;
1717
use JMS\Serializer\Tests\Fixtures\AuthorList;
18+
use JMS\Serializer\Tests\Fixtures\DataFalse;
19+
use JMS\Serializer\Tests\Fixtures\DataTrue;
1820
use JMS\Serializer\Tests\Fixtures\DiscriminatedAuthor;
1921
use JMS\Serializer\Tests\Fixtures\DiscriminatedComment;
2022
use JMS\Serializer\Tests\Fixtures\FirstClassMapCollection;
@@ -27,6 +29,7 @@
2729
use JMS\Serializer\Visitor\Factory\JsonSerializationVisitorFactory;
2830
use JMS\Serializer\Visitor\SerializationVisitorInterface;
2931
use PHPUnit\Framework\Attributes\DataProvider;
32+
use TypeError;
3033

3134
class JsonSerializationTest extends BaseSerializationTestCase
3235
{
@@ -151,6 +154,8 @@ protected static function getContent($key)
151154
$outputs['data_float'] = '{"data":1.236}';
152155
$outputs['data_bool'] = '{"data":false}';
153156
$outputs['data_string'] = '{"data":"foo"}';
157+
$outputs['data_true'] = '{"data":true}';
158+
$outputs['data_false'] = '{"data":false}';
154159
$outputs['data_author'] = '{"data":{"full_name":"foo"}}';
155160
$outputs['data_comment'] = '{"data":{"author":{"full_name":"foo"},"text":"bar"}}';
156161
$outputs['data_discriminated_author'] = '{"data":{"full_name":"foo","objectType":"author"}}';
@@ -480,6 +485,50 @@ public function testSerializingUnionProperties()
480485
self::assertEquals(static::getContent('data_string'), $serialized);
481486
}
482487

488+
public function testTrueDataType()
489+
{
490+
if (PHP_VERSION_ID < 80200) {
491+
$this->markTestSkipped('True type requires PHP 8.2');
492+
493+
return;
494+
}
495+
496+
self::assertEquals(
497+
static::getContent('data_true'),
498+
$this->serialize(new DataTrue(true)),
499+
);
500+
501+
self::assertEquals(
502+
new DataTrue(true),
503+
$this->deserialize(static::getContent('data_true'), DataTrue::class),
504+
);
505+
506+
$this->expectException(TypeError::class);
507+
$this->deserialize(static::getContent('data_false'), DataTrue::class);
508+
}
509+
510+
public function testFalseDataType()
511+
{
512+
if (PHP_VERSION_ID < 80200) {
513+
$this->markTestSkipped('False type requires PHP 8.2');
514+
515+
return;
516+
}
517+
518+
self::assertEquals(
519+
static::getContent('data_false'),
520+
$this->serialize(new DataFalse(false)),
521+
);
522+
523+
self::assertEquals(
524+
new DataFalse(false),
525+
$this->deserialize(static::getContent('data_false'), DataFalse::class),
526+
);
527+
528+
$this->expectException(TypeError::class);
529+
$this->deserialize(static::getContent('data_true'), DataFalse::class);
530+
}
531+
483532
public function testDeserializingComplexDiscriminatedUnionProperties()
484533
{
485534
if (PHP_VERSION_ID < 80000) {

0 commit comments

Comments
 (0)