8000 bug #47221 [Serializer] Fallback looking for DiscriminatorMap on inte… · symfony/symfony@9271349 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9271349

Browse files
committed
bug #47221 [Serializer] Fallback looking for DiscriminatorMap on interfaces (Caligone)
This PR was merged into the 5.4 branch. Discussion ---------- [Serializer] Fallback looking for DiscriminatorMap on interfaces | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #47219 | License | MIT | Doc PR | No First time here Trying to properly fix #47219 but I'm not sure about the impact of this changes I am also not sure either if it should be a 6.2 or 6.1 based branch - [x] Globally validate the behavior - [x] Add tests Commits ------- bfc4bdd [Serializer] Looking for DiscriminatorMap on interfaces when the current object also extends from a class
2 parents fcb754a + bfc4bdd commit 9271349

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)
0