Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Serializer] Fallback looking for DiscriminatorMap on interfaces #47221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Serializer] Looking for DiscriminatorMap on interfaces when the curr…
…ent object also extends from a class
  • Loading branch information
Caligone committed Sep 2, 2023
commit bfc4bddc7b9358dfb77dfa76446380b40a219290
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
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
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>
Caligone marked this conversation as resolved.
Show resolved Hide resolved
*/
class DummyMessageNumberThree extends \stdClass implements DummyMessageInterface
{
}
13 changes: 13 additions & 0 deletions 13 src/Symfony/Component/Serializer/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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.