Skip to content

Commit

Permalink
Merge pull request #2319 from goetas/jms-decorator
Browse files Browse the repository at this point in the history
Use symfony decoration for jms serializer handler registry
  • Loading branch information
goetas authored Oct 14, 2021
2 parents f4c04f5 + aa38360 commit a2e5a93
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class HandlerRegistryDecorationPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
if (!$container->has('fos_rest.serializer.jms_handler_registry')) {
// skip if JMSSerializerBundle is not installed or if JMSSerializerBundle >= 4.0
if (!$container->has('fos_rest.serializer.jms_handler_registry') || $container->has('jms_serializer.handler_registry.service_locator')) {
return;
}

Expand Down
7 changes: 5 additions & 2 deletions DependencyInjection/Compiler/JMSHandlersPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ final class JMSHandlersPass implements CompilerPassInterface
public function process(ContainerBuilder $container): void
{
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));
// perform the aliasing only when jms-serializer-bundle < 4.0
if (!$container->has('jms_serializer.handler_registry.service_locator')) {
// 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
5 changes: 5 additions & 0 deletions Tests/Functional/DependencyInjectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use FOS\RestBundle\Serializer\JMSHandlerRegistryV2;
use FOS\RestBundle\Serializer\Normalizer\FormErrorHandler;
use JMS\Serializer\Visitor\SerializationVisitorInterface;
use JMS\SerializerBundle\Debug\TraceableHandlerRegistry;
use JMS\SerializerBundle\JMSSerializerBundle;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
Expand All @@ -33,6 +34,10 @@ public function testSerializerRelatedServicesAreNotRemovedWhenJmsSerializerBundl

$this->assertInstanceOf(FormErrorHandler::class, $container->get('test.jms_serializer.form_error_handler'));

if (class_exists(TraceableHandlerRegistry::class)) {
$this->markTestIncomplete('Starting from jms/serializer-bundle 4.0 the handler registry is not decorated anymore');
}

$this->assertInstanceOf(
interface_exists(SerializationVisitorInterface::class) ? JMSHandlerRegistryV2::class : JMSHandlerRegistry::class,
$container->get('test.jms_serializer.handler_registry')
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"symfony/browser-kit": "^4.4|^5.0",
"symfony/expression-language": "^4.4|^5.0",
"symfony/css-selector": "^4.4|^5.0",
"jms/serializer-bundle": "^2.4.3|^3.0.1",
"jms/serializer-bundle": "^2.4.3|^3.0.1|^4.0",
"jms/serializer": "^1.13|^2.0|^3.0",
"psr/http-message": "^1.0",
"friendsofphp/php-cs-fixer": "^3.0"
Expand Down

0 comments on commit a2e5a93

Please sign in to comment.