Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@
* @author Christian Flothmann <[email protected]>
*
* @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;
}
Expand Down
9 changes: 7 additions & 2 deletions DependencyInjection/Compiler/JMSHandlersPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
4 changes: 4 additions & 0 deletions DependencyInjection/FOSRestExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions Serializer/JMSHandlerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* @author Ener-Getick <[email protected]>
*
* @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
{
Expand Down Expand Up @@ -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));
}
}
4 changes: 4 additions & 0 deletions Serializer/JMSHandlerRegistryV2.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* @author Ener-Getick <[email protected]>
*
* @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
{
Expand Down Expand Up @@ -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));
}
}
19 changes: 19 additions & 0 deletions Tests/Functional/SerializerErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace FOS\RestBundle\Tests\Functional;

use JMS\Serializer\Handler\HandlerRegistry;
use Symfony\Component\ErrorHandler\ErrorRenderer\SerializerErrorRenderer;

/**
Expand All @@ -29,6 +30,7 @@ public static function tearDownAfterClass()
self::deleteTmpDir('FlattenExceptionNormalizerRfc7807Format');
self::deleteTmpDir('FormErrorHandler');
self::deleteTmpDir('FormErrorNormalizer');
self::deleteTmpDir('DisableJMSHandlers');
parent::tearDownAfterClass();
}

Expand Down Expand Up @@ -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'));
}
}
17 changes: 17 additions & 0 deletions Tests/Functional/app/DisableJMSHandlers/bundles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/*
* This file is part of the FOSRestBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* 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(),
];
7 changes: 7 additions & 0 deletions Tests/Functional/app/DisableJMSHandlers/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
imports:
- { resource: ../config/default.yml }
- { resource: ../config/exception_listener.yml }

fos_rest:
serializer:
enable_jms_registry: false