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

Skip to content

[TypeInfo] Return bound type as base type in TemplateType #58259

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 18, 2024
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 @@ -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;
}
3 changes: 1 addition & 2 deletions src/Symfony/Component/TypeInfo/Type/TemplateType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\Component\TypeInfo\Type;

use Symfony\Component\TypeInfo\Exception\LogicException;
use Symfony\Component\TypeInfo\Type;
use Symfony\Component\TypeInfo\TypeIdentifier;

Expand All @@ -33,7 +32,7 @@ public function __construct(

public function getBaseType(): BuiltinType|ObjectType
{
throw new LogicException(sprintf('Cannot get base type on "%s" template type.', $this));
return $this->bound->getBaseType();
}

public function isA(TypeIdentifier|string $subject): bool
Expand Down
Loading