Skip to content

Commit 5b5d16d

Browse files
committed
Merge remote-tracking branch 'upstream/4.3'
2 parents 0fb1dc8 + be26bbe commit 5b5d16d

4 files changed

Lines changed: 60 additions & 1 deletion

File tree

src/Serializer/Mapping/Loader/PropertyMetadataLoader.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool
7171
if ($attribute instanceof DiscriminatorMap) {
7272
$classMetadata->setClassDiscriminatorMapping(new ClassDiscriminatorMapping(
7373
method_exists($attribute, 'getTypeProperty') ? $attribute->getTypeProperty() : $attribute->typeProperty,
74-
method_exists($attribute, 'getMapping') ? $attribute->getMapping() : $attribute->mapping
74+
method_exists($attribute, 'getMapping') ? $attribute->getMapping() : $attribute->mapping,
75+
method_exists($attribute, 'getDefaultType') ? $attribute->getDefaultType() : ($attribute->defaultType ?? null),
7576
));
7677
continue;
7778
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Serializer\Tests\Fixtures\Model;
15+
16+
use Symfony\Component\Serializer\Attribute\DiscriminatorMap;
17+
18+
#[DiscriminatorMap(typeProperty: 'discr', mapping: ['concrete' => ConcreteWithDiscriminator::class], defaultType: 'concrete')]
19+
abstract class AbstractWithDiscriminator
20+
{
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Serializer\Tests\Fixtures\Model;
15+
16+
class ConcreteWithDiscriminator extends AbstractWithDiscriminator
17+
{
18+
}

src/Serializer/Tests/Mapping/Loader/PropertyMetadataLoaderTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
1717
use ApiPlatform\Metadata\Property\PropertyNameCollection;
1818
use ApiPlatform\Serializer\Mapping\Loader\PropertyMetadataLoader;
19+
use ApiPlatform\Serializer\Tests\Fixtures\Model\AbstractWithDiscriminator;
1920
use ApiPlatform\Serializer\Tests\Fixtures\Model\HasRelation;
2021
use ApiPlatform\Serializer\Tests\Fixtures\Model\Relation;
2122
use PHPUnit\Framework\TestCase;
23+
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorMapping;
2224
use Symfony\Component\Serializer\Mapping\ClassMetadata;
2325

2426
final class PropertyMetadataLoaderTest extends TestCase
@@ -55,4 +57,21 @@ public function testCreateMappingForAClass(): void
5557
$this->assertArrayHasKey('name', $attributesMetadata);
5658
$this->assertEquals(['read'], $attributesMetadata['name']->getGroups());
5759
}
60+
61+
public function testForwardsDiscriminatorDefaultType(): void
62+
{
63+
if (!method_exists(ClassDiscriminatorMapping::class, 'getDefaultType')) { // @phpstan-ignore-line
64+
$this->markTestSkipped('ClassDiscriminatorMapping::getDefaultType() requires symfony/serializer 7.1+.');
65+
}
66+
67+
$coll = $this->createStub(PropertyNameCollectionFactoryInterface::class);
68+
$coll->method('create')->willReturn(new PropertyNameCollection([]));
69+
$loader = new PropertyMetadataLoader($coll);
70+
$classMetadata = new ClassMetadata(AbstractWithDiscriminator::class);
71+
$loader->loadClassMetadata($classMetadata);
72+
73+
$mapping = $classMetadata->getClassDiscriminatorMapping();
74+
$this->assertNotNull($mapping);
75+
$this->assertSame('concrete', $mapping->getDefaultType());
76+
}
5877
}

0 commit comments

Comments
 (0)