diff --git a/src/Symfony/Bundle/DependencyInjection/Compiler/MetadataAwareNameConverterPass.php b/src/Symfony/Bundle/DependencyInjection/Compiler/MetadataAwareNameConverterPass.php index a1b93b067f..a611e305ec 100644 --- a/src/Symfony/Bundle/DependencyInjection/Compiler/MetadataAwareNameConverterPass.php +++ b/src/Symfony/Bundle/DependencyInjection/Compiler/MetadataAwareNameConverterPass.php @@ -39,14 +39,20 @@ public function process(ContainerBuilder $container): void } $definition = $container->getDefinition('serializer.name_converter.metadata_aware'); - $num = \count($definition->getArguments()); + $key = '$fallbackNameConverter'; + $arguments = $definition->getArguments(); + if (false === \array_key_exists($key, $arguments)) { + $key = 1; + } if ($container->hasAlias('api_platform.name_converter')) { $nameConverter = new Reference((string) $container->getAlias('api_platform.name_converter')); - if (1 === $num) { + + // old symfony versions + if (false === \array_key_exists($key, $arguments)) { $definition->addArgument($nameConverter); - } elseif (1 < $num && null === $definition->getArgument(1)) { - $definition->setArgument(1, $nameConverter); + } elseif (null === $definition->getArgument($key)) { + $definition->setArgument($key, $nameConverter); } } diff --git a/tests/Symfony/Bundle/DependencyInjection/Compiler/MetadataAwareNameConverterPassTest.php b/tests/Symfony/Bundle/DependencyInjection/Compiler/MetadataAwareNameConverterPassTest.php index 2ecfd99bc4..d069cc8b39 100644 --- a/tests/Symfony/Bundle/DependencyInjection/Compiler/MetadataAwareNameConverterPassTest.php +++ b/tests/Symfony/Bundle/DependencyInjection/Compiler/MetadataAwareNameConverterPassTest.php @@ -101,4 +101,23 @@ public function testProcessOnlyOneArg(): void $pass->process($containerBuilderProphecy->reveal()); } + + public function testProcessWithAbstractMetadataAware(): void + { + $pass = new MetadataAwareNameConverterPass(); + + $definition = $this->prophesize(Definition::class); + $definition->getArguments()->willReturn(['$metadataFactory' => [], '$fallbackNameConverter' => null])->shouldBeCalled(); + $definition->getArgument('$fallbackNameConverter')->willReturn(null)->shouldBeCalled(); + $definition->setArgument('$fallbackNameConverter', new Reference('app.name_converter'))->willReturn($definition)->shouldBeCalled(); + + $containerBuilderProphecy = $this->prophesize(ContainerBuilder::class); + $containerBuilderProphecy->hasDefinition('serializer.name_converter.metadata_aware')->willReturn(true)->shouldBeCalled(); + $containerBuilderProphecy->hasAlias('api_platform.name_converter')->shouldBeCalled()->willReturn(true); + $containerBuilderProphecy->getAlias('api_platform.name_converter')->shouldBeCalled()->willReturn(new Alias('app.name_converter')); + $containerBuilderProphecy->setAlias('api_platform.name_converter', 'serializer.name_converter.metadata_aware')->shouldBeCalled(); + $containerBuilderProphecy->getDefinition('serializer.name_converter.metadata_aware')->shouldBeCalled()->willReturn($definition); + + $pass->process($containerBuilderProphecy->reveal()); + } }