From 319a0fe6702486919d61b7829a283456129a6ce9 Mon Sep 17 00:00:00 2001 From: "hubert.lenoir" Date: Fri, 1 Dec 2023 15:02:36 +0100 Subject: [PATCH] [Serializer] Remove TranslatableNormalizer service when the Translator is disabled --- .../DependencyInjection/FrameworkExtension.php | 2 +- .../php/serializer_without_translator.php | 14 ++++++++++++++ .../xml/serializer_without_translator.xml | 15 +++++++++++++++ .../yml/serializer_without_translator.yml | 10 ++++++++++ .../FrameworkExtensionTestCase.php | 6 ++++++ 5 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/serializer_without_translator.php create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/serializer_without_translator.xml create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/serializer_without_translator.yml diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 8a42bfb29ee20..4ff8d5de3d346 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -1938,7 +1938,7 @@ private function registerSerializerConfiguration(array $config, ContainerBuilder $container->removeDefinition('serializer.mapping.cache_class_metadata_factory'); } - if (!class_exists(Translator::class)) { + if (!$this->readConfigEnabled('translator', $container, $config)) { $container->removeDefinition('serializer.normalizer.translatable'); } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/serializer_without_translator.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/serializer_without_translator.php new file mode 100644 index 0000000000000..acf0130806b6b --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/serializer_without_translator.php @@ -0,0 +1,14 @@ +loadFromExtension('framework', [ + 'annotations' => false, + 'http_method_override' => false, + 'handle_all_throwables' => true, + 'php_errors' => ['log' => true], + 'serializer' => [ + 'enabled' => true, + ], + 'translator' => [ + 'enabled' => false, + ], +]); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/serializer_without_translator.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/serializer_without_translator.xml new file mode 100644 index 0000000000000..584937b0ac4d8 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/serializer_without_translator.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/serializer_without_translator.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/serializer_without_translator.yml new file mode 100644 index 0000000000000..33ee3f4b881c1 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/serializer_without_translator.yml @@ -0,0 +1,10 @@ +framework: + annotations: false + http_method_override: false + handle_all_throwables: true + php_errors: + log: true + serializer: + enabled: true + translator: + enabled: false diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTestCase.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTestCase.php index 70e0132926b28..7caaa77bf6f49 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTestCase.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTestCase.php @@ -1501,6 +1501,12 @@ public function testSerializerEnabled() $this->assertEquals($container->getDefinition('serializer.normalizer.object')->getArgument(6)['max_depth_handler'], new Reference('my.max.depth.handler')); } + public function testSerializerWithoutTranslator() + { + $container = $this->createContainerFromFile('serializer_without_translator'); + $this->assertFalse($container->hasDefinition('serializer.normalizer.translatable')); + } + public function testRegisterSerializerExtractor() { $container = $this->createContainerFromFile('full');