Skip to content

Commit 757d63b

Browse files
jschaedlfabpot
authored andcommitted
[Serializer] Add discriminator map to debug commmand output
1 parent bb3100d commit 757d63b

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

Command/DebugCommand.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ private function getAttributesData(ClassMetadataInterface $classMetadata): array
9797
{
9898
$data = [];
9999

100+
$mapping = $classMetadata->getClassDiscriminatorMapping();
101+
$typeProperty = $mapping?->getTypeProperty();
102+
100103
foreach ($classMetadata->getAttributesMetadata() as $attributeMetadata) {
101104
$data[$attributeMetadata->getName()] = [
102105
'groups' => $attributeMetadata->getGroups(),
@@ -107,6 +110,10 @@ private function getAttributesData(ClassMetadataInterface $classMetadata): array
107110
'normalizationContexts' => $attributeMetadata->getNormalizationContexts(),
108111
'denormalizationContexts' => $attributeMetadata->getDenormalizationContexts(),
109112
];
113+
114+
if ($mapping && $typeProperty === $attributeMetadata->getName()) {
115+
$data[$attributeMetadata->getName()]['discriminatorMap'] = $mapping->getTypesMapping();
116+
}
110117
}
111118

112119
return $data;

Tests/Command/DebugCommandTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
1818
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
1919
use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader;
20+
use Symfony\Component\Serializer\Tests\Dummy\DummyClassWithDiscriminatorMap;
2021
use Symfony\Component\Serializer\Tests\Dummy\DummyClassOne;
2122

2223
/**
@@ -79,6 +80,41 @@ public function testOutputWithClassArgument()
7980
);
8081
}
8182

83+
public function testOutputWithDiscriminatorMapClass()
84+
{
85+
$command = new DebugCommand(new ClassMetadataFactory(new AttributeLoader()));
86+
87+
$tester = new CommandTester($command);
88+
$tester->execute(['class' => DummyClassWithDiscriminatorMap::class], ['decorated' => false]);
89+
90+
$this->assertSame(<<<TXT
91+
92+
Symfony\Component\Serializer\Tests\Dummy\DummyClassWithDiscriminatorMap
93+
-----------------------------------------------------------------------
94+
95+
+----------+------------------------------------------------------------------------+
96+
| Property | Options |
97+
+----------+------------------------------------------------------------------------+
98+
| type | [ |
99+
| | "groups" => [], |
100+
| | "maxDepth" => null, |
101+
| | "serializedName" => null, |
102+
| | "serializedPath" => null, |
103+
| | "ignore" => false, |
104+
| | "normalizationContexts" => [], |
105+
| | "denormalizationContexts" => [], |
106+
| | "discriminatorMap" => [ |
107+
| | "one" => "Symfony\Component\Serializer\Tests\Dummy\DummyClassOne", |
108+
| | "two" => "Symfony\Component\Serializer\Tests\Dummy\DummyClassTwo" |
109+
| | ] |
110+
| | ] |
111+
+----------+------------------------------------------------------------------------+
112+
113+
TXT,
114+
$tester->getDisplay(true),
115+
);
116+
}
117+
82118
public function testOutputWithInvalidClassArgument()
83119
{
84120
$serializer = $this->createMock(ClassMetadataFactoryInterface::class);

Tests/Dummy/DummyClassTwo.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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+
namespace Symfony\Component\Serializer\Tests\Dummy;
13+
14+
class DummyClassTwo
15+
{
16+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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+
namespace Symfony\Component\Serializer\Tests\Dummy;
13+
14+
use Symfony\Component\Serializer\Attribute\DiscriminatorMap;
15+
16+
#[DiscriminatorMap(typeProperty: 'type', mapping: [
17+
'one' => DummyClassOne::class,
18+
'two' => DummyClassTwo::class,
19+
])]
20+
class DummyClassWithDiscriminatorMap
21+
{
22+
public string $type;
23+
}

0 commit comments

Comments
 (0)