Skip to content

Commit 49e6d39

Browse files
committed
feature #52749 [Serializer] Add discriminator map to debug commmand output (jschaedl)
This PR was squashed before being merged into the 7.3 branch. Discussion ---------- [Serializer] Add discriminator map to debug commmand output | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | little addition | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Issues | - <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead --> | License | MIT The discriminator mapping was missing in the `debug:serializer` output. In case a `#[DiscriminatorMap]` is configured the output will now look like this: https://github.com/symfony/symfony/pull/52749/files#diff-d5830f2c54bb89f9819ca3c7c357a2f8df9319cf4d82f71b0704e693a2bfe9ceR106-R109 Commits ------- bb0b4079207 [Serializer] Add discriminator map to debug commmand output
2 parents 8064a26 + 757d63b commit 49e6d39

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
@@ -96,6 +96,9 @@ private function getAttributesData(ClassMetadataInterface $classMetadata): array
9696
{
9797
$data = [];
9898

99+
$mapping = $classMetadata->getClassDiscriminatorMapping();
100+
$typeProperty = $mapping?->getTypeProperty();
101+
99102
foreach ($classMetadata->getAttributesMetadata() as $attributeMetadata) {
100103
$data[$attributeMetadata->getName()] = [
101104
'groups' => $attributeMetadata->getGroups(),
@@ -106,6 +109,10 @@ private function getAttributesData(ClassMetadataInterface $classMetadata): array
106109
'normalizationContexts' => $attributeMetadata->getNormalizationContexts(),
107110
'denormalizationContexts' => $attributeMetadata->getDenormalizationContexts(),
108111
];
112+
113+
if ($mapping && $typeProperty === $attributeMetadata->getName()) {
114+
$data[$attributeMetadata->getName()]['discriminatorMap'] = $mapping->getTypesMapping();
115+
}
109116
}
110117

111118
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)