Jmsserializerbundle: Symfony 4 - Class 'jms_serializer.stopwatch_subscriber' not found

Created on 26 Oct 2017  Â·  15Comments  Â·  Source: schmittjoh/JMSSerializerBundle

In Symfony 4 I have occured a problem with JMSSerializerBundle in use with FOSRestBundle. My code looks like below:

$restresult = $this->getDoctrine()->getRepository('App:Entity')->findAll();
return View::create(['data' => $restresult], Response::HTTP_FOUND);

There is no problem when data is a pure array, but when there are even simple objects (f.ex. DateTime) that problem occurs.

DeletingJMSSerializerBundle from Kernel.php fixes the problem, because Symfony takes his native serializer when JMSSerializerBundle is not found.

Error:

"error": {
"code": 500,
"message": "Internal Server Error",
"exception": [
{
"message": "Warning: call_user_func() expects parameter 1 to be a valid callback, class 'jms_serializer.stopwatch_subscriber' not found",
"class": "ErrorException",
"trace": [
{
"namespace": "",
"short_class": "",
"class": "",
"type": "",
"function": "",
"file": "/var/www/someproject/vendor/jms/serializer/src/JMS/Serializer/EventDispatcher/EventDispatcher.php",
"line": 104,
"args": []
},
{
"namespace": "JMS\\Serializer\\EventDispatcher",
"short_class": "EventDispatcher",
"class": "JMS\\Serializer\\EventDispatcher\\EventDispatcher",
"type": "->",
"function": "dispatch",
"file": "/var/www/someproject/vendor/jms/serializer/src/JMS/Serializer/GraphNavigator.php",
"line": 188,
"args": [
[
"string",
"serializer.pre_serialize"
],
[
"string",
"App\\Entity\\SomeEntity"
],
[
"string",
"json"
],
[
"object",
"JMS\\Serializer\\EventDispatcher\\PreSerializeEvent"
]
]
},

Most helpful comment

Appears to still be broken in v2.2.0 @goetas, same with sf4-compat

All 15 comments

https://github.com/schmittjoh/JMSSerializerBundle/pull/605 is still open (and symfony 4 is not yer released), means that this bundle is not yet fully compatible with symfony 4

Hi, this is not a symfony bug, you have to change configuration of services which you can retrieve directly from the container - according to https://github.com/symfony/dependency-injection/commit/a043965c4a198c62999bae0e3cf11d207a03e2b2#diff-09eda9d53bdc62a97ad02badf08be429

“ deprecated "public-by-default" definitions and aliases, the new default will be "private" in 4.0 "

All services in symfony 4 are private, except explicit setup "public" flag on it

I found out, that this services have to be public (because LazyEventDispatcher)

<service id="jms_serializer.doctrine_proxy_subscriber" class="%jms_serializer.doctrine_proxy_subscriber.class%" public="true">
<service id="jms_serializer.stopwatch_subscriber" class="%jms_serializer.stopwatch_subscriber.class%" public="true">

and

<service id="jms_serializer.metadata_driver" alias="jms_serializer.metadata.chain_driver" public="true" />

because LazyLoadingDriver

Or you may change it and use lazy=true flag on service definition and remove Lazy* Services...

this will be "solved" by the bundle right after the symfony release

@goetas would it at least be possible to create a separate feature branch with this issue fixed? It would allow our team to move faster until S4 is released

@Rastusik sf4-compat branch tries to do that

@kgasienica which bundle version are you using?

@goetas great, thank you very much!

@goetas it was

"jms/serializer-bundle": "^2.0@dev",

@kgasienica there is a chance that your specific problem was solved with v2.2.0

After add those services in config.yml, there is still a problem with one service:
jms_serializer.metadata_driver
This one even if I put to run as public is not starting and is not on the services list.

Appears to still be broken in v2.2.0 @goetas, same with sf4-compat

@kgasienica What error are you getting ? I just override the services in my services.yml and it's working fine.

services:
    jms_serializer.doctrine_proxy_subscriber:
        class: JMS\Serializer\EventDispatcher\Subscriber\DoctrineProxySubscriber
        public: true
        tags:
            - { name: jms_serializer.event_subscriber }
        arguments:
            - true
            - false
    jms_serializer.stopwatch_subscriber:
        class: JMS\SerializerBundle\Serializer\StopwatchEventSubscriber
        public: true
        arguments:
            - "?debug.stopwatch"
    jms_serializer.datetime_handler:
        class: JMS\Serializer\Handler\DateHandler
        public: true
        tags:
            - { name: jms_serializer.subscribing_handler }
    jms_serializer.array_collection_handler:
        class: JMS\Serializer\Handler\ArrayCollectionHandler
        public: true
        tags:
            - { name: jms_serializer.subscribing_handler }

# You may need to make public other handlers

BTW services are private as default since 3.4 https://symfony.com/blog/new-in-symfony-3-4-services-are-private-by-default

@luishdez yes, if you override this services, it works fine, but we expect it without overrideing.

jms_serializer.metadata_driver was changed in sf4-compat branche, but, not doctrine_proxy_subscriber and stopwatch_subscriber

This two are still required to be public.

I create a pull request for it. https://github.com/schmittjoh/JMSSerializerBundle/pull/614

Was this page helpful?
0 / 5 - 0 ratings