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

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

Merged
merged 1 commit into from
Jun 19, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorFromClassMetadata;
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorResolverInterface;
Expand Down Expand Up @@ -765,6 +766,30 @@ protected function createChildContext(array $parentContext, string $attribute, ?
return $context;
}

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;
}

/**
* Builds the cache key for the attributes cache.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,30 +164,6 @@ protected function setAttributeValue(object $object, string $attribute, mixed $v
}
}

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;
}

protected function isAllowedAttribute($classOrObject, string $attribute, ?string $format = null, array $context = [])
{
if (!parent::isAllowedAttribute($classOrObject, $attribute, $format, $context)) {
Expand Down
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,29 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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;

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

class DummyMessageNumberFour extends SomeAbstract implements DummyMessageInterface
{
public function __construct(public $one)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,16 @@
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerAwareInterface;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\AbstractDummy;
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\AbstractDummyFirstChild;
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\AbstractDummySecondChild;
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\DummySecondChildQuux;
use Symfony\Component\Serializer\Tests\Fixtures\DummyString;
use Symfony\Component\Serializer\Tests\Fixtures\DummyWithNotNormalizable;
Expand Down Expand Up @@ -1087,6 +1090,25 @@ public static function provideBooleanTypesData()
[['foo' => false], TruePropertyDummy::class],
];
}

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

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

$normalizer = new PropertyNormalizer(
$classMetadataFactory,
null,
new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]),
new ClassDiscriminatorFromClassMetadata($classMetadataFactory),
);

$serialized = $normalizer->normalize($example, 'json');
$deserialized = $normalizer->denormalize($serialized, DummyMessageInterface::class, 'json');

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

class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
Expand Down
Loading