Skip to content

[Serializer] Fix TemplateType handling in AbstractObjectNormalizer #58033

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

Closed
wants to merge 1 commit into from
Closed
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 @@ -37,6 +37,7 @@
use Symfony\Component\TypeInfo\Type\CollectionType;
use Symfony\Component\TypeInfo\Type\IntersectionType;
use Symfony\Component\TypeInfo\Type\ObjectType;
use Symfony\Component\TypeInfo\Type\TemplateType;
use Symfony\Component\TypeInfo\Type\UnionType;
use Symfony\Component\TypeInfo\TypeIdentifier;

Expand Down Expand Up @@ -659,12 +660,17 @@ private function validateAndDenormalize(Type $type, string $currentClass, string
foreach ($types as $t) {
if (null === $data && $type->isNullable()) {
return null;
} elseif ($t instanceof TemplateType) {
$t = $t->getBound();
}

$collectionKeyType = $collectionValueType = null;
if ($t instanceof CollectionType) {
$collectionKeyType = $t->getCollectionKeyType();
$collectionValueType = $t->getCollectionValueType();
if ($collectionValueType instanceof TemplateType) {
$collectionValueType = $collectionValueType->getBound();
}
}

$t = $t->getBaseType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\PropertyInfo\Extractor\PhpStanExtractor;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
use Symfony\Component\PropertyInfo\Type as LegacyType;
Expand All @@ -37,6 +38,7 @@
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer;
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
Expand Down Expand Up @@ -1247,6 +1249,52 @@ protected function isAllowedAttribute($classOrObject, string $attribute, ?string
$this->assertInstanceOf(\ArrayObject::class, $actual->foo);
$this->assertSame(1, $actual->foo->count());
}

public function testTemplateTypeWhenAnObjectIsPassedToDenormalize()
{
$normalizer = new class (
classMetadataFactory: new ClassMetadataFactory(new AttributeLoader()),
propertyTypeExtractor: new PropertyInfoExtractor(typeExtractors: [new PhpStanExtractor(), new ReflectionExtractor()])
) extends AbstractObjectNormalizerDummy {
protected function isAllowedAttribute($classOrObject, string $attribute, ?string $format = null, array $context = []): bool
{
return true;
}
};
$serializer = new Serializer([$normalizer]);
$normalizer->setSerializer($serializer);

$denormalizedData = $normalizer->denormalize(['value' => new DummyGenericsValue()], DummyGenericsValueWrapper::class);

$this->assertInstanceOf(DummyGenericsValueWrapper::class, $denormalizedData);
$this->assertInstanceOf(DummyGenericsValue::class, $denormalizedData->value);

$this->assertSame('dummy', $denormalizedData->value->type);
}

public function testDenormalizeTemplateType()
{
$normalizer = new class (
classMetadataFactory: new ClassMetadataFactory(new AttributeLoader()),
propertyTypeExtractor: new PropertyInfoExtractor(typeExtractors: [new PhpStanExtractor(), new ReflectionExtractor()])
) extends AbstractObjectNormalizerDummy {
protected function isAllowedAttribute($classOrObject, string $attribute, ?string $format = null, array $context = []): bool
{
return true;
}
};
$serializer = new Serializer([new ArrayDenormalizer(), $normalizer]);
$normalizer->setSerializer($serializer);

$denormalizedData = $normalizer->denormalize(['value' => ['type' => 'dummy'], 'values' => [['type' => 'dummy']]], DummyGenericsValueWrapper::class);

$this->assertInstanceOf(DummyGenericsValueWrapper::class, $denormalizedData);
$this->assertInstanceOf(DummyGenericsValue::class, $denormalizedData->value);
$this->assertContainsOnlyInstancesOf(DummyGenericsValue::class, $denormalizedData->values);
$this->assertCount(1, $denormalizedData->values);
$this->assertSame('dummy', $denormalizedData->value->type);
$this->assertSame('dummy', $denormalizedData->values[0]->type);
}
}

class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
Expand Down Expand Up @@ -1753,3 +1801,31 @@ public function getSupportedTypes(?string $format): array
];
}
}

#[DiscriminatorMap('type', ['dummy' => DummyGenericsValue::class])]
abstract class AbstractDummyGenericsValue
{
public function __construct(
public string $type,
) {
}
}

class DummyGenericsValue extends AbstractDummyGenericsValue
{
public function __construct()
{
parent::__construct('dummy');
}
}

/**
* @template T of AbstractDummyGenericsValue
*/
class DummyGenericsValueWrapper
{
/** @var T */
public mixed $value;
/** @var T[] */
public array $values;
}
Loading