-
-
Notifications
You must be signed in to change notification settings - Fork 586
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
Union types deserialisation #1504
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JMS\Serializer\Handler; | ||
|
||
use JMS\Serializer\Context; | ||
use JMS\Serializer\DeserializationContext; | ||
use JMS\Serializer\Exception\RuntimeException; | ||
use JMS\Serializer\GraphNavigatorInterface; | ||
use JMS\Serializer\SerializationContext; | ||
use JMS\Serializer\Visitor\DeserializationVisitorInterface; | ||
use JMS\Serializer\Visitor\SerializationVisitorInterface; | ||
|
||
final class UnionHandler implements SubscribingHandlerInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribingMethods() | ||
{ | ||
return [ | ||
[ | ||
'type' => 'union', | ||
'direction' => GraphNavigatorInterface::DIRECTION_DESERIALIZATION, | ||
'format' => 'json', | ||
'method' => 'deserializeUnion', | ||
], | ||
[ | ||
'type' => 'union', | ||
'direction' => GraphNavigatorInterface::DIRECTION_DESERIALIZATION, | ||
'format' => 'xml', | ||
'method' => 'deserializeUnion', | ||
], | ||
[ | ||
'type' => 'union', | ||
'format' => 'json', | ||
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, | ||
'method' => 'serializeUnion', | ||
], | ||
[ | ||
'type' => 'union', | ||
'format' => 'xml', | ||
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, | ||
'method' => 'serializeUnion', | ||
], | ||
]; | ||
} | ||
|
||
public function serializeUnion( | ||
SerializationVisitorInterface $visitor, | ||
$data, | ||
array $type, | ||
SerializationContext $context | ||
) { | ||
return $this->matchSimpleType($data, $type, $context); | ||
} | ||
|
||
/** | ||
* @param int|string|\SimpleXMLElement $data | ||
* @param array $type | ||
*/ | ||
public function deserializeUnion(DeserializationVisitorInterface $visitor, $data, array $type, DeserializationContext $context) | ||
{ | ||
if ($data instanceof \SimpleXMLElement) { | ||
throw new RuntimeException('XML deserialisation into union types is not supported yet.'); | ||
} | ||
|
||
return $this->matchSimpleType($data, $type, $context); | ||
} | ||
|
||
private function matchSimpleType($data, array $type, Context $context) | ||
{ | ||
$dataType = gettype($data); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i guess this works for json only... in XML all the data are always strings... so I do not see the point of this handler for xml |
||
$alternativeName = null; | ||
switch ($dataType) { | ||
case 'boolean': | ||
$alternativeName = 'bool'; | ||
break; | ||
case 'integer': | ||
$alternativeName = 'int'; | ||
break; | ||
case 'double': | ||
$alternativeName = 'float'; | ||
break; | ||
case 'array': | ||
case 'string': | ||
break; | ||
default: | ||
throw new RuntimeException(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't here be a better message? or be a map (static property in the class) with alternative names for types? static $aliases = [
'boolean' => 'bool',
'integer' => 'int',
//...
]; |
||
} | ||
|
||
foreach ($type['params'] as $possibleType) { | ||
if ($possibleType['name'] === $dataType || $possibleType['name'] === $alternativeName) { | ||
return $context->getNavigator()->accept($data, $possibleType); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
use JMS\Serializer\EventDispatcher\ObjectEvent; | ||
use JMS\Serializer\Exception\RuntimeException; | ||
use JMS\Serializer\GraphNavigatorInterface; | ||
use JMS\Serializer\Metadata\Driver\TypedPropertiesDriver; | ||
use JMS\Serializer\SerializationContext; | ||
use JMS\Serializer\Tests\Fixtures\Author; | ||
use JMS\Serializer\Tests\Fixtures\AuthorList; | ||
|
@@ -18,6 +19,7 @@ | |
use JMS\Serializer\Tests\Fixtures\ObjectWithInlineArray; | ||
use JMS\Serializer\Tests\Fixtures\ObjectWithObjectProperty; | ||
use JMS\Serializer\Tests\Fixtures\Tag; | ||
use JMS\Serializer\Tests\Fixtures\TypedProperties\UnionTypedProperties; | ||
use JMS\Serializer\Visitor\Factory\JsonSerializationVisitorFactory; | ||
use JMS\Serializer\Visitor\SerializationVisitorInterface; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
|
@@ -423,6 +425,18 @@ public static function getTypeHintedArraysAndStdClass() | |
]; | ||
} | ||
|
||
public function testDeserializingUnionProperties() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this seems doing the test with primitives... what about union of objects? is that supported? |
||
{ | ||
if (PHP_VERSION_ID < 80000) { | ||
$this->markTestSkipped(sprintf('%s requires PHP 8.0', TypedPropertiesDriver::class)); | ||
|
||
return; | ||
} | ||
|
||
$object = new UnionTypedProperties(10000); | ||
self::assertEquals($object, $this->deserialize(static::getContent('data_integer'), UnionTypedProperties::class)); | ||
} | ||
|
||
/** | ||
* @dataProvider getTypeHintedArraysAndStdClass | ||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can this be shortened as in
serializer/src/Handler/ArrayCollectionHandler.php
Line 51 in 4478166