diff --git a/CHANGELOG.md b/CHANGELOG.md index 891aa5a15..8b13d025f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,12 @@ CHANGELOG ========= +3.1.0 +----- + +* The JMS Handlers `JMSHandlerRegistry` and `JMSHandlerRegistryV2` behaviors are deprecated. If you depend on the inheritance of JMS handlers that they provided, a deprecation will be thrown. + Set the option `fos_rest.serializer.enable_jms_registry` to `false` to disable these custom registries. + 3.0.3 ----- diff --git a/DependencyInjection/Compiler/HandlerRegistryDecorationPass.php b/DependencyInjection/Compiler/HandlerRegistryDecorationPass.php index 432e41438..ae5715147 100644 --- a/DependencyInjection/Compiler/HandlerRegistryDecorationPass.php +++ b/DependencyInjection/Compiler/HandlerRegistryDecorationPass.php @@ -28,11 +28,14 @@ * @author Christian Flothmann * * @internal + * + * @deprecated since FOSRestBundle 3.1. */ class HandlerRegistryDecorationPass implements CompilerPassInterface { public function process(ContainerBuilder $container): void { + // If handlers are disabled, this alias is not created by JMSHandlersPass if (!$container->has('fos_rest.serializer.jms_handler_registry')) { return; } diff --git a/DependencyInjection/Compiler/JMSHandlersPass.php b/DependencyInjection/Compiler/JMSHandlersPass.php index 77bf8eafb..d9fc0cf32 100644 --- a/DependencyInjection/Compiler/JMSHandlersPass.php +++ b/DependencyInjection/Compiler/JMSHandlersPass.php @@ -26,9 +26,14 @@ final class JMSHandlersPass implements CompilerPassInterface { public function process(ContainerBuilder $container): void { + $enableCustomRegistry = $container->hasParameter('fos_rest.serializer.enable_jms_registry'); + $container->getParameterBag()->remove('fos_rest.serializer.enable_jms_registry'); + if ($container->has('jms_serializer.handler_registry')) { - // the public alias prevents the handler registry definition from being removed - $container->setAlias('fos_rest.serializer.jms_handler_registry', new Alias('jms_serializer.handler_registry', true)); + if ($enableCustomRegistry) { + // the public alias prevents the handler registry definition from being removed + $container->setAlias('fos_rest.serializer.jms_handler_registry', new Alias('jms_serializer.handler_registry', true)); + } return; } diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index aede2cf57..aa82db987 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -110,6 +110,7 @@ public function getConfigTreeBuilder(): TreeBuilder ->prototype('scalar')->end() ->end() ->booleanNode('serialize_null')->defaultFalse()->end() + ->booleanNode('enable_jms_registry')->defaultTrue()->end() ->end() ->end() ->arrayNode('zone') diff --git a/DependencyInjection/FOSRestExtension.php b/DependencyInjection/FOSRestExtension.php index 52c4594e2..0eced89c7 100644 --- a/DependencyInjection/FOSRestExtension.php +++ b/DependencyInjection/FOSRestExtension.php @@ -362,6 +362,10 @@ private function loadSerializer(array $config, ContainerBuilder $container): voi $options['serializeNullStrategy'] = $config['serializer']['serialize_null']; $viewHandler->addArgument($options); + + if ($config['serializer']['enable_jms_registry']) { + $container->setParameter('fos_rest.serializer.enable_jms_registry', true); + } } private function loadZoneMatcherListener(array $config, XmlFileLoader $loader, ContainerBuilder $container): void diff --git a/Serializer/JMSHandlerRegistry.php b/Serializer/JMSHandlerRegistry.php index e11044daa..dc76bbd95 100644 --- a/Serializer/JMSHandlerRegistry.php +++ b/Serializer/JMSHandlerRegistry.php @@ -20,6 +20,8 @@ * @author Ener-Getick * * @internal do not depend on this class directly + * + * @deprecated since FOSRestBundle 3.1, use the option `fos_rest.serializer.disable_jms_handlers` to avoid relying on it. */ class JMSHandlerRegistry implements HandlerRegistryInterface { @@ -56,6 +58,8 @@ public function getHandler($direction, $typeName, $format) if (null !== $handler) { return $handler; } + + @trigger_error(sprintf('Relying on the custom registry %s to inherit the JMS handler of type `%s` is deprecated since FOSRestBundle 3.1. It will be removed in version 4.0. Set the option `fos_rest.serializer.enable_jms_registry` to `false` to disable it.', __CLASS__, $typeName), E_USER_DEPRECATED); } while ($typeName = get_parent_class($typeName)); } } diff --git a/Serializer/JMSHandlerRegistryV2.php b/Serializer/JMSHandlerRegistryV2.php index 6ee179e6e..62548a2da 100644 --- a/Serializer/JMSHandlerRegistryV2.php +++ b/Serializer/JMSHandlerRegistryV2.php @@ -20,6 +20,8 @@ * @author Ener-Getick * * @internal do not depend on this class directly + * + * @deprecated since FOSRestBundle 3.1, use the option `fos_rest.serializer.disable_custom_jms_registry` to avoid relying on it. */ final class JMSHandlerRegistryV2 implements HandlerRegistryInterface { @@ -56,6 +58,8 @@ public function getHandler(int $direction, string $typeName, string $format) if (null !== $handler) { return $handler; } + + @trigger_error(sprintf('Relying on the custom registry %s to inherit the JMS handler of type `%s` is deprecated since FOSRestBundle 3.1. It will be removed in version 4.0. Set the option `fos_rest.serializer.enable_jms_registry` to `false` to disable it.', __CLASS__, $typeName), E_USER_DEPRECATED); } while ($typeName = get_parent_class($typeName)); } } diff --git a/Tests/Functional/SerializerErrorTest.php b/Tests/Functional/SerializerErrorTest.php index ead2a6095..cba884a72 100644 --- a/Tests/Functional/SerializerErrorTest.php +++ b/Tests/Functional/SerializerErrorTest.php @@ -11,6 +11,7 @@ namespace FOS\RestBundle\Tests\Functional; +use JMS\Serializer\Handler\HandlerRegistry; use Symfony\Component\ErrorHandler\ErrorRenderer\SerializerErrorRenderer; /** @@ -29,6 +30,7 @@ public static function tearDownAfterClass() self::deleteTmpDir('FlattenExceptionNormalizerRfc7807Format'); self::deleteTmpDir('FormErrorHandler'); self::deleteTmpDir('FormErrorNormalizer'); + self::deleteTmpDir('DisableJMSHandlers'); parent::tearDownAfterClass(); } @@ -273,4 +275,21 @@ public function serializeInvalidFormXmlProvider() ['FormErrorHandler', $expectedJMSContent], ]; } + + public function testJMSHandlers() + { + // Test case importing the JMS Serializer bundle + self::bootKernel(['test_case' => 'FormErrorHandler', 'debug' => false]); + + $this->assertTrue(self::$container->has('fos_rest.serializer.jms_handler_registry')); + $this->assertNotInstanceOf(HandlerRegistry::class, self::$container->get('jms_serializer.handler_registry')); + } + + public function testDisabledJMSRegistry() + { + self::bootKernel(['test_case' => 'DisableJMSHandlers', 'debug' => false]); + + $this->assertFalse(self::$container->has('fos_rest.serializer.jms_handler_registry')); + $this->assertInstanceOf(HandlerRegistry::class, self::$container->get('jms_serializer.handler_registry')); + } } diff --git a/Tests/Functional/app/DisableJMSHandlers/bundles.php b/Tests/Functional/app/DisableJMSHandlers/bundles.php new file mode 100644 index 000000000..17a91ff56 --- /dev/null +++ b/Tests/Functional/app/DisableJMSHandlers/bundles.php @@ -0,0 +1,17 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +return [ + new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(), + new \FOS\RestBundle\FOSRestBundle(), + new \JMS\SerializerBundle\JMSSerializerBundle(), + new \FOS\RestBundle\Tests\Functional\Bundle\TestBundle\TestBundle(), +]; diff --git a/Tests/Functional/app/DisableJMSHandlers/config.yml b/Tests/Functional/app/DisableJMSHandlers/config.yml new file mode 100644 index 000000000..9e8d8d4a1 --- /dev/null +++ b/Tests/Functional/app/DisableJMSHandlers/config.yml @@ -0,0 +1,7 @@ +imports: + - { resource: ../config/default.yml } + - { resource: ../config/exception_listener.yml } + +fos_rest: + serializer: + enable_jms_registry: false