Skip to content

Commit bfc4bdd

Browse files
committed
[Serializer] Looking for DiscriminatorMap on interfaces when the current object also extends from a class
1 parent fcb754a commit bfc4bdd

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ private function resolveMappingForMappedObject($object)
7878
{
7979
$reflectionClass = new \ReflectionClass($object);
8080
if ($parentClass = $reflectionClass->getParentClass()) {
81-
return $this->getMappingForMappedObject($parentClass->getName());
81+
if (null !== ($parentMapping = $this->getMappingForMappedObject($parentClass->getName()))) {
82+
return $parentMapping;
83+
}
8284
}
8385

8486
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 <samuel.roze@gmail.com>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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+
/**
15+
* @author Samuel Roze <samuel.roze@gmail.com>
16+
*/
17+
class DummyMessageNumberThree extends \stdClass implements DummyMessageInterface
18+
{
19+
}

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;
@@ -488,6 +489,18 @@ public function testDeserializeAndSerializeNestedInterfacedObjectsWithTheClassMe
488489
$this->assertEquals($example, $deserialized);
489490
}
490491

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy