Skip to content

Commit

Permalink
[Serializer] Looking for DiscriminatorMap on interfaces when the curr…
Browse files Browse the repository at this point in the history
…ent object also extends from a class
  • Loading branch information
Caligone committed Sep 2, 2023
1 parent 701e2b8 commit 531a459
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
4 changes: 3 additions & 1 deletion Mapping/ClassDiscriminatorFromClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ private function resolveMappingForMappedObject($object)
{
$reflectionClass = new \ReflectionClass($object);
if ($parentClass = $reflectionClass->getParentClass()) {
return $this->getMappingForMappedObject($parentClass->getName());
if (null !== ($parentMapping = $this->getMappingForMappedObject($parentClass->getName()))) {
return $parentMapping;
}
}

foreach ($reflectionClass->getInterfaceNames() as $interfaceName) {
Expand Down
3 changes: 2 additions & 1 deletion Tests/Fixtures/DummyMessageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
/**
* @DiscriminatorMap(typeProperty="type", mapping={
* "one"="Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberOne",
* "two"="Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberTwo"
* "two"="Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberTwo",
* "three"="Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberThree"
* })
*
* @author Samuel Roze <samuel.roze@gmail.com>
Expand Down
19 changes: 19 additions & 0 deletions Tests/Fixtures/DummyMessageNumberThree.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Tests\Fixtures;

/**
* @author Samuel Roze <samuel.roze@gmail.com>
*/
class DummyMessageNumberThree extends \stdClass implements DummyMessageInterface
{
}
13 changes: 13 additions & 0 deletions Tests/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
use Symfony\Component\Serializer\Tests\Fixtures\DummyFirstChildQuux;
use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageInterface;
use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberOne;
use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberThree;
use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberTwo;
use Symfony\Component\Serializer\Tests\Fixtures\DummyObjectWithEnumConstructor;
use Symfony\Component\Serializer\Tests\Fixtures\DummyObjectWithEnumProperty;
Expand Down Expand Up @@ -488,6 +489,18 @@ public function testDeserializeAndSerializeNestedInterfacedObjectsWithTheClassMe
$this->assertEquals($example, $deserialized);
}

public function testDeserializeAndSerializeNestedAbstractAndInterfacedObjectsWithTheClassMetadataDiscriminator()
{
$example = new DummyMessageNumberThree();

$serializer = $this->serializerWithClassDiscriminator();

$serialized = $serializer->serialize($example, 'json');
$deserialized = $serializer->deserialize($serialized, DummyMessageInterface::class, 'json');

$this->assertEquals($example, $deserialized);
}

public function testExceptionWhenTypeIsNotKnownInDiscriminator()
{
try {
Expand Down

0 comments on commit 531a459

Please sign in to comment.