From fbb53e5e35ca0ec3de26ddc7de7ea4d1dda5c20b Mon Sep 17 00:00:00 2001 From: Antoine Bluchet Date: Thu, 10 Oct 2024 12:00:18 +0200 Subject: [PATCH] fix(symfony): metadata aware name converter has 0 arguments by default (#6711) Since https://github.com/symfony/symfony/commit/ce228d3a7a6448e5f214033a0cd2710177085f29#diff-f53370286252d2326a377eafd4bba05112518e0c8bf3aedda670fd76a254e9fa the serializer.name_converter.metadata_aware is extending an abstract service. Therefore there are no arguments by default. We relied on the number of arguments to inject API Platform's configured name converter. --- .../MetadataAwareNameConverterPass.php | 14 ++++++++++---- .../MetadataAwareNameConverterPassTest.php | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Bundle/DependencyInjection/Compiler/MetadataAwareNameConverterPass.php b/src/Symfony/Bundle/DependencyInjection/Compiler/MetadataAwareNameConverterPass.php index a1b93b067f9..a611e305ec0 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 2ecfd99bc40..d069cc8b39a 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()); + } }