Skip to content

Can't use symfony serializer when requesting the id "serializer" #583

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

Closed
kobelobster opened this issue Jun 9, 2017 · 16 comments
Closed

Comments

@kobelobster
Copy link

I want to use the symfony serializer and found this in the services.xml of this bundle

<service id="serializer" alias="jms_serializer.serializer" /><!-- Here for BC, may be disabled in the config -->

I saw, that you can disable it by setting enable_short_alias: false which I did in my config.yml, however, now, every service which needs the "serializer" gets this error

Unable to replace alias "fos_elastica.serializer" with actual definition "serializer". and You have requested a non-existent service "serializer".

When I debug through my code and am in the ContainerBuilder::getDefinition method, inspecting the $this->definitions is showing me, that there is no serializer registered. I deleted the cache of course, but nothin ghappened. Any idea what this could be? This is the serializer.xml of the FrameworkBundle of Symfony

<service id="serializer" class="Symfony\Component\Serializer\Serializer">
    <argument type="collection" />
    <argument type="collection" />
</service>
@kobelobster
Copy link
Author

If it helps: I'm using the version 1.2.1

@goetas
Copy link
Collaborator

goetas commented Jun 9, 2017

http://jmsyst.com/bundles/JMSSerializerBundle/master/configuration#extension-reference
put on your config.yml

jms_serializer:
    enable_short_alias: false

@goetas goetas closed this as completed Jun 9, 2017
@kobelobster
Copy link
Author

kobelobster commented Jun 9, 2017

@goetas: Please read the third paragraph of my issue. I already said

I saw, that you can disable it by setting enable_short_alias: false which I did in my config.yml, however, now, every service which needs the "serializer" gets this error

The exception is thrown BECAUSE of this setting.

@kobelobster
Copy link
Author

Yes, I did. If I understood the Configuration correctly, this

->arrayNode('serializer')
    ->treatNullLike(array())
    ->children()
        ->scalarNode('callback_class')->defaultValue('FOS\ElasticaBundle\Serializer\Callback')->end()
        ->scalarNode('serializer')->defaultValue('serializer')->end()
    ->end()
->end()

means, that if it's null I have an empty array, correct? Which means that it defaults to serializer, correct? But since serializer was jms_serializer before it worked, and now I have to change the config in

fos_elastica:
  serializer: ~

to

fos_elastica:
  serializer: jms_serializer.serializer

right?

@kobelobster
Copy link
Author

Update: If I change that to

fos_elastica:
  serializer:
    serializer: jms_serializer.serializer

it works again (so far). Thanks for your help, just misconfigured it apparently.

@kobelobster
Copy link
Author

Sorry @goetas , have to interrupt again. When I now call the service serializer, I get the following message

You have requested a non-existent service "serializer". Did you mean one of these: "fos_elastica.index.app.product.serializer.callback", "fos_rest.serializer.exception_wrapper_serialize_handler", "jms_serializer", "jms_serializer.array_collection_handler", "jms_serializer.constraint_violation_handler", "jms_serializer.datetime_handler", "jms_serializer.deserialization_context_factory", "jms_serializer.doctrine_proxy_subscriber", "jms_serializer.expression_evaluator", "jms_serializer.form_error_handler", "jms_serializer.handler_registry", "jms_serializer.json_deserialization_visitor", "jms_serializer.json_serialization_visitor", "jms_serializer.metadata_driver", "jms_serializer.metadata_factory", "jms_serializer.naming_strategy", "jms_serializer.object_constructor", "jms_serializer.php_collection_handler", "jms_serializer.serialization_context_factory", "jms_serializer.stopwatch_subscriber", "jms_serializer.templating.helper.serializer", "jms_serializer.unserialize_object_constructor", "jms_serializer.xml_deserialization_visitor", "jms_serializer.xml_serialization_visitor", "jms_serializer.yaml_serialization_visitor", "mapudo_core.serializer.builder_factory", "symfony.component.serializer.mapping.factory.class_metadata_factory"

It seems like removing the alias also removes the service? But that can't be, right?

@goetas
Copy link
Collaborator

goetas commented Jun 9, 2017

Which serializer do you need?

  • if you need the symfony serializer, looks not enabled or installed.
  • if you need the jms serializer:
    • if you are using the bundle 2.x, then the instance name is jms_serializer (just serializer is not available and is reserved for symfony).
    • if you are using the bundle 1.x, then:
      • if enable_short_alias: false, then the instance name is jms_serializer (just serializer is not available).
      • if enable_short_alias: true, then the instance name is jms_serializer or serializer, and both will refer to the jms serializer (this setup is not recommended since it conflicts with the symfony serializer).

@kobelobster
Copy link
Author

I want to use both. We use the JMS-Serializer for our API but I know wanted to normalize an object (use the basic serializer functionality), because the JMSSerializer in our version doesn't have the method normalize.

Isn't it possible to use both at the same time?

@goetas
Copy link
Collaborator

goetas commented Jun 9, 2017

as said:

the symfony serializer, looks not enabled or installed

you have to figure out why the symfony serializer is not available....

regarding the jms serializer:

  • if you are using the 2.x branch of the bundle, no actions are required.
  • if you are using the 1.x, then put enable_short_alias: false and refer everywhere to the jms serializer using jms_serializer

@kobelobster
Copy link
Author

kobelobster commented Jun 9, 2017

@goetas

you have to figure out why the symfony serializer is not available....

Because I made a mistake. I forgot to enable it in the framework

framework:
    # ...
    serializer: { enable_annotations: true }

I replaced all occurences of serializer with jms_serializer and configured the enable_short_alias, but no result.

framework:
    #esi:             ~
    serializer: { enabled: true }

jms_serializer:
    enable_short_alias: false

fos_elastica:
  serializer:
    serializer: jms_serializer.serializer // same error with jms_serializer

This is how it looks now, and this is the error

A circular reference has been detected (configured limit: 1).

I'm currently digging to find out where this comes from.

@kobelobster
Copy link
Author

This looked more like a configuration problem, however. I just resetted my changes and have redone them. Now it works. I will report back if I find anything.

@kobelobster
Copy link
Author

kobelobster commented Jun 9, 2017

@goetas Yeah, I think I found the problem and seems like it has to do with the FOSRestBundle, not with this one.

if ($container->has('jms_serializer.serializer')) {
    $container->setAlias('fos_rest.serializer', 'jms_serializer.serializer');
} else {
    $container->removeDefinition('fos_rest.serializer.exception_wrapper_serialize_handler');
}
if ($container->has('serializer')) {
    $container->setAlias('fos_rest.serializer', 'serializer');
} else {
    $container->removeDefinition('fos_rest.serializer.exception_wrapper_normalizer');
}

That's the problem. The FOSRestBundle uses the serializer instead of the jms_serializer just because both are defined

http://symfony.com/doc/current/bundles/FOSRestBundle/1-setting_up_the_bundle.html#c-enable-a-serializer

This documentation sounds like this is how it should be done, and there is no way around that unless I define fos_rest.serializer myself.

@goetas
Copy link
Collaborator

goetas commented Jun 9, 2017

Yeah, I think I found the problem and seems like it has to do with the FOSRestBundle, not with this one.

I doubt

the symfony serializer, looks not enabled or installed
you have to figure out why the symfony serializer is not available....

Maybe you forgot this:

The serializer service is not available by default. To turn it on, activate it in your configuration
http://symfony.com/doc/current/serializer.html#activating-the-serializer

@kobelobster
Copy link
Author

@goetas Yeah, look at tthis post here: #583 (comment)

I forgot it.

Almost everything works now. The JMS Serializer is doing what it should do, no errors at all, BUT here's the BUT. When I access the serializer via $this->get('serializer'); it still returns the jms_serializer. Last problem to fix prorbably, then I'm good to go.

@kobelobster
Copy link
Author

Ok, final answer. It seems to work now :D. Don't know what I changed between now and the previous comments, but I'm good to go now, finally> Thanks for your time.

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

No branches or pull requests

2 participants