Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use default Symfony serializer service when available #276

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

magarzon
Copy link

Previous to this PR, the bundle created a new service using the same class as the Symfony Serializer, what, apart from redundant, makes problems with autowiring (two services with the same class).

With this PR, if the serializer service exists, it's used, tagging the custom normalizers.

If the serializer services doesn't exist, it's registered as before (more or less, instead from file, is registered using a Compiler Pass)

Tests are ok (updated one and adding another)

@stof
Copy link
Member

stof commented Feb 24, 2017

This means that normalizers created for this bundle are now included in the serializer used by your app (which could cause issues depending on the kind of objects you use) and that your own normalizers are now called when serializing the data in the bundle (which can totally break the features of the bundle depending of what your normalizers are, as your own normalizer could be used instead).
So I'm -1 on this.

If the issue is autowiring, the solution would be for Symfony to mark the main serializer services as the one used for autowiring, as it does already for the translator or the router for instance.

use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\Serializer\Serializer;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why adding such use statement ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A rest from some refactoring, probably

*/
class SerializerCompilerPass implements CompilerPassInterface
{
const SERIALIZER_SERVICE_ID = 'fos_js_routing.serializer';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should not add such constant. There is no reason to make this public (and private constants are not available in the targeted PHP versions)


$encoder = $container->register('fos_js_routing.encoder', JsonEncoder::class);
$encoder->setPublic(false);
$definition->addArgument(['json' => $encoder]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the composer.json requires php >=5.3.2, so the short array syntax is not OK.

];
$definition->addArgument($normalizers);

$encoder = $container->register('fos_js_routing.encoder', JsonEncoder::class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

::class is not OK either

@magarzon
Copy link
Author

magarzon commented Feb 24, 2017

@stof to have a normalizer that supports the normalization of RouteCollection or RoutesResponse it's weird, or you want to do the same this bundle do, so should be redundant. If you have a normalizer so generic that can be chosen instead of this bundle ones, you probably have a bigger problem (although you can play with priorities)

Anyway, probably adding a configuration parameter to select if you want the main serializer or the bundle one could be useful/valid?

@tacman
Copy link
Contributor

tacman commented Nov 28, 2023

As the bundle now requires the symfony serializer (and PHP 8), I think this PR can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants