Skip to content
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
3 changes: 2 additions & 1 deletion src/Serializer/Mapping/Loader/PropertyMetadataLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool
if ($attribute instanceof DiscriminatorMap) {
$classMetadata->setClassDiscriminatorMapping(new ClassDiscriminatorMapping(
method_exists($attribute, 'getTypeProperty') ? $attribute->getTypeProperty() : $attribute->typeProperty,
method_exists($attribute, 'getMapping') ? $attribute->getMapping() : $attribute->mapping
method_exists($attribute, 'getMapping') ? $attribute->getMapping() : $attribute->mapping,
method_exists($attribute, 'getDefaultType') ? $attribute->getDefaultType() : ($attribute->defaultType ?? null),
));
continue;
}
Expand Down
21 changes: 21 additions & 0 deletions src/Serializer/Tests/Fixtures/Model/AbstractWithDiscriminator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Serializer\Tests\Fixtures\Model;

use Symfony\Component\Serializer\Attribute\DiscriminatorMap;

#[DiscriminatorMap(typeProperty: 'discr', mapping: ['concrete' => ConcreteWithDiscriminator::class], defaultType: 'concrete')]
abstract class AbstractWithDiscriminator
{
}
18 changes: 18 additions & 0 deletions src/Serializer/Tests/Fixtures/Model/ConcreteWithDiscriminator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Serializer\Tests\Fixtures\Model;

class ConcreteWithDiscriminator extends AbstractWithDiscriminator
{
}
19 changes: 19 additions & 0 deletions src/Serializer/Tests/Mapping/Loader/PropertyMetadataLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
use ApiPlatform\Metadata\Property\PropertyNameCollection;
use ApiPlatform\Serializer\Mapping\Loader\PropertyMetadataLoader;
use ApiPlatform\Serializer\Tests\Fixtures\Model\AbstractWithDiscriminator;
use ApiPlatform\Serializer\Tests\Fixtures\Model\HasRelation;
use ApiPlatform\Serializer\Tests\Fixtures\Model\Relation;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorMapping;
use Symfony\Component\Serializer\Mapping\ClassMetadata;

final class PropertyMetadataLoaderTest extends TestCase
Expand Down Expand Up @@ -55,4 +57,21 @@ public function testCreateMappingForAClass(): void
$this->assertArrayHasKey('name', $attributesMetadata);
$this->assertEquals(['read'], $attributesMetadata['name']->getGroups());
}

public function testForwardsDiscriminatorDefaultType(): void
{
if (!method_exists(ClassDiscriminatorMapping::class, 'getDefaultType')) { // @phpstan-ignore-line
$this->markTestSkipped('ClassDiscriminatorMapping::getDefaultType() requires symfony/serializer 7.1+.');
}

$coll = $this->createStub(PropertyNameCollectionFactoryInterface::class);
$coll->method('create')->willReturn(new PropertyNameCollection([]));
$loader = new PropertyMetadataLoader($coll);
$classMetadata = new ClassMetadata(AbstractWithDiscriminator::class);
$loader->loadClassMetadata($classMetadata);

$mapping = $classMetadata->getClassDiscriminatorMapping();
$this->assertNotNull($mapping);
$this->assertSame('concrete', $mapping->getDefaultType());
}
}
Loading