Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5333c6d

Browse files
committed
[Serializer] Looking for DiscriminatorMap on interfaces when the current object also extends from a class
1 parent 3cefdcb commit 5333c6d

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

src/Symfony/Component/Serializer/Mapping/ClassDiscriminatorFromClassMetadata.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ private function resolveMappingForMappedObject(object|string $object)
6969
{
7070
$reflectionClass = new \ReflectionClass($object);
7171
if ($parentClass = $reflectionClass->getParentClass()) {
72-
return $this->getMappingForMappedObject($parentClass->getName());
72+
if (null !== ($parentMapping = $this->getMappingForMappedObject($parentClass->getName()))) {
73+
return $parentMapping;
74+
}
7375
}
7476

7577
foreach ($reflectionClass->getInterfaceNames() as $interfaceName) {

src/Symfony/Component/Serializer/Tests/Fixtures/DummyMessageInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
/**
1717
* @DiscriminatorMap(typeProperty="type", mapping={
1818
* "one"="Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberOne",
19-
* "two"="Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberTwo"
19+
* "two"="Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberTwo",
20+
* "three"="Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberThree"
2021
* })
2122
*
2223
* @author Samuel Roze <[email protected]>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Fixtures;
13+
14+
use Symfony\Component\Serializer\Annotation\Groups;
15+
16+
/**
17+
* @author Samuel Roze <[email protected]>
18+
*/
19+
class DummyMessageNumberThree extends \stdClass implements DummyMessageInterface
20+
{
21+
}

src/Symfony/Component/Serializer/Tests/SerializerTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
use Symfony\Component\Serializer\Tests\Fixtures\DummyFirstChildQuux;
5959
use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageInterface;
6060
use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberOne;
61+
use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberThree;
6162
use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberTwo;
6263
use Symfony\Component\Serializer\Tests\Fixtures\DummyObjectWithEnumConstructor;
6364
use Symfony\Component\Serializer\Tests\Fixtures\DummyObjectWithEnumProperty;
@@ -489,6 +490,18 @@ public function testDeserializeAndSerializeNestedInterfacedObjectsWithTheClassMe
489490
$this->assertEquals($example, $deserialized);
490491
}
491492

493+
public function testDeserializeAndSerializeNestedAbstractAndInterfacedObjectsWithTheClassMetadataDiscriminator()
494+
{
495+
$example = new DummyMessageNumberThree();
496+
497+
$serializer = $this->serializerWithClassDiscriminator();
498+
499+
$serialized = $serializer->serialize($example, 'json');
500+
$deserialized = $serializer->deserialize($serialized, DummyMessageInterface::class, 'json');
501+
502+
$this->assertEquals($example, $deserialized);
503+
}
504+
492505
public function testExceptionWhenTypeIsNotKnownInDiscriminator()
493506
{
494507
try {

0 commit comments

Comments
 (0)