Skip to content

Commit

Permalink
Merge pull request #1254 from marein/allow-interfaces-for-typed-prope…
Browse files Browse the repository at this point in the history
…rties

Allow interfaces for typed properties
  • Loading branch information
goetas authored Nov 8, 2020
2 parents 0ed0b6a + 8c596c6 commit 0b740bc
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Metadata/Driver/TypedPropertiesDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ private function shouldTypeHint(ReflectionProperty $propertyReflection): bool
return true;
}

return class_exists($propertyReflection->getType()->getName());
return class_exists($propertyReflection->getType()->getName())
|| interface_exists($propertyReflection->getType()->getName());
}
}
9 changes: 9 additions & 0 deletions tests/Fixtures/TypedProperties/Car.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace JMS\Serializer\Tests\Fixtures\TypedProperties;

class Car implements Vehicle
{
}
1 change: 1 addition & 0 deletions tests/Fixtures/TypedProperties/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class User
{
public int $id;
public Role $role;
public Vehicle $vehicle;
public \DateTime $created;

/**
Expand Down
16 changes: 16 additions & 0 deletions tests/Fixtures/TypedProperties/Vehicle.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\TypedProperties;

use JMS\Serializer\Annotation as Serializer;

/**
* @Serializer\Discriminator(field = "type", map = {
* "car": "JMS\Serializer\Tests\Fixtures\TypedProperties\Car",
* })
*/
interface Vehicle
{
}
1 change: 1 addition & 0 deletions tests/Metadata/Driver/TypedPropertiesDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function testInferPropertiesFromTypes()
$expectedPropertyTypes = [
'id' => 'int',
'role' => 'JMS\Serializer\Tests\Fixtures\TypedProperties\Role',
'vehicle' => 'JMS\Serializer\Tests\Fixtures\TypedProperties\Vehicle',
'created' => 'DateTime',
'tags' => 'iterable',
];
Expand Down
5 changes: 5 additions & 0 deletions tests/Serializer/BaseSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,10 @@ public function testTypedProperties()
$this->markTestSkipped(sprintf('%s requires PHP 7.4', __METHOD__));
}

$builder = SerializerBuilder::create($this->handlerRegistry, $this->dispatcher);
$builder->includeInterfaceMetadata(true);
$this->serializer = $builder->build();

$user = new TypedProperties\User();
$user->id = 1;
$user->created = new \DateTime('2010-10-01 00:00:00');
Expand All @@ -1364,6 +1368,7 @@ public function testTypedProperties()
$role = new TypedProperties\Role();
$role->id = 5;
$user->role = $role;
$user->vehicle = new TypedProperties\Car();

$result = $this->serialize($user);

Expand Down
2 changes: 1 addition & 1 deletion tests/Serializer/JsonSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ protected function getContent($key)
$outputs['user_discriminator_array'] = '[{"entityName":"User"},{"entityName":"ExtendedUser"}]';
$outputs['user_discriminator'] = '{"entityName":"User"}';
$outputs['user_discriminator_extended'] = '{"entityName":"ExtendedUser"}';
$outputs['typed_props'] = '{"id":1,"role":{"id":5},"created":"2010-10-01T00:00:00+00:00","updated":"2011-10-01T00:00:00+00:00","tags":["a","b"]}';
$outputs['typed_props'] = '{"id":1,"role":{"id":5},"vehicle":{"type":"car"},"created":"2010-10-01T00:00:00+00:00","updated":"2011-10-01T00:00:00+00:00","tags":["a","b"]}';
}

if (!isset($outputs[$key])) {
Expand Down
3 changes: 3 additions & 0 deletions tests/Serializer/xml/typed_props.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
<role>
<id>5</id>
</role>
<vehicle>
<type><![CDATA[car]]></type>
</vehicle>
<created><![CDATA[2010-10-01T00:00:00+00:00]]></created>
<updated><![CDATA[2011-10-01T00:00:00+00:00]]></updated>
<tags>
Expand Down

0 comments on commit 0b740bc

Please sign in to comment.