Skip to content

[Serializer] Add support for discriminator map in property normalizer #60511

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

Open
wants to merge 1 commit into
base: 6.4
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorResolverInterface;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
Expand Down Expand Up @@ -221,4 +222,28 @@ private function getReflectionProperty(string|object $classOrObject, string $att
}
}
}

protected function getAllowedAttributes(string|object $classOrObject, array $context, bool $attributesAsString = false): array|bool
{
if (false === $allowedAttributes = parent::getAllowedAttributes($classOrObject, $context, $attributesAsString)) {
return false;
}

if (null !== $this->classDiscriminatorResolver) {
$class = \is_object($classOrObject) ? $classOrObject::class : $classOrObject;
if (null !== $discriminatorMapping = $this->classDiscriminatorResolver->getMappingForMappedObject($classOrObject)) {
$allowedAttributes[] = $attributesAsString ? $discriminatorMapping->getTypeProperty() : new AttributeMetadata($discriminatorMapping->getTypeProperty());
}

if (null !== $discriminatorMapping = $this->classDiscriminatorResolver->getMappingForClass($class)) {
$attributes = [];
foreach ($discriminatorMapping->getTypesMapping() as $mappedClass) {
$attributes[] = parent::getAllowedAttributes($mappedClass, $context, $attributesAsString);
}
$allowedAttributes = array_merge($allowedAttributes, ...$attributes);
}
}

return $allowedAttributes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
'one' => DummyMessageNumberOne::class,
'two' => DummyMessageNumberTwo::class,
'three' => DummyMessageNumberThree::class,
'four' => DummyMessageNumberFour::class,
])]
interface DummyMessageInterface
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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;

use Symfony\Component\Serializer\Attribute\Ignore;
use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageInterface;

abstract class SomeAbstract {
#[Ignore]
public function getDescription()
{
return 'Hello, World!';
}
}

/**
* @author Samuel Roze <samuel.roze@gmail.com>
*/
class DummyMessageNumberFour extends SomeAbstract implements DummyMessageInterface
{
public function __construct(public $one)
{
}
}
24 changes: 24 additions & 0 deletions src/Symfony/Component/Serializer/Tests/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
use Symfony\Component\Serializer\Tests\Fixtures\DenormalizableDummy;
use Symfony\Component\Serializer\Tests\Fixtures\DummyFirstChildQuux;
use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageInterface;
use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberFour;
use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberOne;
use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberThree;
use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberTwo;
Expand Down Expand Up @@ -504,6 +505,29 @@ public function testDeserializeAndSerializeNestedAbstractAndInterfacedObjectsWit
$this->assertEquals($example, $deserialized);
}

public function testDeserializeAndSerializeConstructorAndIgnoreAndInterfacedObjectsWithTheClassMetadataDiscriminator()
{
$example = new DummyMessageNumberFour('Hello');

$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());

$serializer = new Serializer(
[
new PropertyNormalizer(
$classMetadataFactory,
null,
new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]),
new ClassDiscriminatorFromClassMetadata($classMetadataFactory),
),
], ['json' => new JsonEncoder()],
);

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

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

public function testExceptionWhenTypeIsNotKnownInDiscriminator()
{
try {
Expand Down