Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
ruudk wants to merge1 commit intosymfony:6.4
base:6.4
Choose a base branch
Loading
fromruudk:fix-60214
Open
Show file tree
Hide file tree
Changes fromall 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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;
}
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -20,6 +20,7 @@
'one' => DummyMessageNumberOne::class,
'two' => DummyMessageNumberTwo::class,
'three' => DummyMessageNumberThree::class,
'four' => DummyMessageNumberFour::class,
])]
interface DummyMessageInterface
{
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionssrc/Symfony/Component/Serializer/Tests/SerializerTest.php
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp