Skip to content

Commit b10e32c

Browse files
committed
Fix MetadataAwareNameConverterPass condition
1 parent 252caa2 commit b10e32c

File tree

3 files changed

+52
-13
lines changed

3 files changed

+52
-13
lines changed

src/Bridge/Elasticsearch/Serializer/NameConverter/InnerFieldsNameConverter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ public function __construct(?NameConverterInterface $decorated = null)
3636
/**
3737
* {@inheritdoc}
3838
*/
39-
public function normalize($propertyName, string $class = null, string $format = null, $context = [])
39+
public function normalize($propertyName, string $class = null, string $format = null, array $context = [])
4040
{
4141
return $this->convertInnerFields($propertyName, true, $class, $format, $context);
4242
}
4343

4444
/**
4545
* {@inheritdoc}
4646
*/
47-
public function denormalize($propertyName, string $class = null, string $format = null, $context = [])
47+
public function denormalize($propertyName, string $class = null, string $format = null, array $context = [])
4848
{
4949
return $this->convertInnerFields($propertyName, false, $class, $format, $context);
5050
}

src/Bridge/Symfony/Bundle/DependencyInjection/Compiler/MetadataAwareNameConverterPass.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,16 @@ final class MetadataAwareNameConverterPass implements CompilerPassInterface
3333
*/
3434
public function process(ContainerBuilder $container)
3535
{
36-
if (!$container->hasAlias('api_platform.name_converter') && $container->hasDefinition('serializer.name_converter.metadata_aware')) {
37-
$container->setAlias('api_platform.name_converter', 'serializer.name_converter.metadata_aware');
36+
if ($container->hasAlias('api_platform.name_converter') || !$container->hasDefinition('serializer.name_converter.metadata_aware')) {
37+
return;
3838
}
39+
40+
$definition = $container->getDefinition('serializer.name_converter.metadata_aware');
41+
42+
if (1 >= \count($definition->getArguments()) || null === $definition->getArgument(1)) {
43+
return;
44+
}
45+
46+
$container->setAlias('api_platform.name_converter', 'serializer.name_converter.metadata_aware');
3947
}
4048
}

tests/Bridge/Symfony/Bundle/DependencyInjection/Compiler/MetadataAwareNameConverterPassTest.php

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,33 @@
1717
use PHPUnit\Framework\TestCase;
1818
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1919
use Symfony\Component\DependencyInjection\ContainerBuilder;
20+
use Symfony\Component\DependencyInjection\Definition;
21+
use Symfony\Component\DependencyInjection\Reference;
2022

2123
/**
2224
* @author Antoine Bluchet <[email protected]>
2325
*/
2426
class MetadataAwareNameConverterPassTest extends TestCase
2527
{
28+
public function testConstruct()
29+
{
30+
$this->assertInstanceOf(CompilerPassInterface::class, new MetadataAwareNameConverterPass());
31+
}
32+
2633
public function testProcess()
2734
{
2835
$pass = new MetadataAwareNameConverterPass();
29-
$this->assertInstanceOf(CompilerPassInterface::class, $pass);
36+
37+
$arguments = [new Reference('serializer.mapping.class_metadata_factory'), new Reference('app.name_converter')];
38+
39+
$definition = $this->prophesize(Definition::class);
40+
$definition->getArguments()->willReturn($arguments)->shouldBeCalled();
41+
$definition->getArgument(1)->willReturn($arguments[1])->shouldBeCalled();
3042

3143
$containerBuilderProphecy = $this->prophesize(ContainerBuilder::class);
32-
$containerBuilderProphecy->hasAlias('api_platform.name_converter')->shouldBeCalled()->willReturn(false);
33-
$containerBuilderProphecy->hasDefinition('serializer.name_converter.metadata_aware')->shouldBeCalled()->willReturn(true);
44+
$containerBuilderProphecy->hasAlias('api_platform.name_converter')->willReturn(false)->shouldBeCalled();
45+
$containerBuilderProphecy->hasDefinition('serializer.name_converter.metadata_aware')->willReturn(true)->shouldBeCalled();
46+
$containerBuilderProphecy->getDefinition('serializer.name_converter.metadata_aware')->willReturn($definition)->shouldBeCalled();
3447
$containerBuilderProphecy->setAlias('api_platform.name_converter', 'serializer.name_converter.metadata_aware')->shouldBeCalled();
3548

3649
$pass->process($containerBuilderProphecy->reveal());
@@ -39,11 +52,11 @@ public function testProcess()
3952
public function testProcessWithNameConverter()
4053
{
4154
$pass = new MetadataAwareNameConverterPass();
42-
$this->assertInstanceOf(CompilerPassInterface::class, $pass);
4355

4456
$containerBuilderProphecy = $this->prophesize(ContainerBuilder::class);
45-
$containerBuilderProphecy->hasAlias('api_platform.name_converter')->shouldBeCalled()->willReturn(true);
46-
$containerBuilderProphecy->hasDefinition('serializer.name_converter.metadata_aware')->shouldNotBeCalled()->willReturn(true);
57+
$containerBuilderProphecy->hasAlias('api_platform.name_converter')->willReturn(true)->shouldBeCalled();
58+
$containerBuilderProphecy->hasDefinition('serializer.name_converter.metadata_aware')->shouldNotBeCalled();
59+
$containerBuilderProphecy->getDefinition('serializer.name_converter.metadata_aware')->shouldNotBeCalled();
4760
$containerBuilderProphecy->setAlias('api_platform.name_converter', 'serializer.name_converter.metadata_aware')->shouldNotBeCalled();
4861

4962
$pass->process($containerBuilderProphecy->reveal());
@@ -52,11 +65,29 @@ public function testProcessWithNameConverter()
5265
public function testProcessWithoutMetadataAwareDefinition()
5366
{
5467
$pass = new MetadataAwareNameConverterPass();
55-
$this->assertInstanceOf(CompilerPassInterface::class, $pass);
5668

5769
$containerBuilderProphecy = $this->prophesize(ContainerBuilder::class);
58-
$containerBuilderProphecy->hasAlias('api_platform.name_converter')->shouldBeCalled()->willReturn(false);
59-
$containerBuilderProphecy->hasDefinition('serializer.name_converter.metadata_aware')->shouldBeCalled()->willReturn(false);
70+
$containerBuilderProphecy->hasAlias('api_platform.name_converter')->willReturn(false)->shouldBeCalled();
71+
$containerBuilderProphecy->hasDefinition('serializer.name_converter.metadata_aware')->willReturn(false)->shouldBeCalled();
72+
$containerBuilderProphecy->setAlias('api_platform.name_converter', 'serializer.name_converter.metadata_aware')->shouldNotBeCalled();
73+
74+
$pass->process($containerBuilderProphecy->reveal());
75+
}
76+
77+
public function testProcessWithMetadataAwareDefinitionSecondArgumentNull()
78+
{
79+
$pass = new MetadataAwareNameConverterPass();
80+
81+
$arguments = [new Reference('serializer.mapping.class_metadata_factory'), null];
82+
83+
$definition = $this->prophesize(Definition::class);
84+
$definition->getArguments()->willReturn($arguments)->shouldBeCalled();
85+
$definition->getArgument(1)->willReturn($arguments[1])->shouldBeCalled();
86+
87+
$containerBuilderProphecy = $this->prophesize(ContainerBuilder::class);
88+
$containerBuilderProphecy->hasAlias('api_platform.name_converter')->willReturn(false)->shouldBeCalled();
89+
$containerBuilderProphecy->hasDefinition('serializer.name_converter.metadata_aware')->willReturn(true)->shouldBeCalled();
90+
$containerBuilderProphecy->getDefinition('serializer.name_converter.metadata_aware')->willReturn($definition)->shouldBeCalled();
6091
$containerBuilderProphecy->setAlias('api_platform.name_converter', 'serializer.name_converter.metadata_aware')->shouldNotBeCalled();
6192

6293
$pass->process($containerBuilderProphecy->reveal());

0 commit comments

Comments
 (0)