Fosrestbundle: "fos_rest.serializer" must implement FOS\RestBundle\Serializer\Serializer (instance of "JMS\Serializer\Serializer" given)

Created on 14 Mar 2016  路  10Comments  路  Source: FriendsOfSymfony/FOSRestBundle

Hello,
after integration of FOSUserBundle and JMSSerializerBundle, i aded new FOS\RestBundle\FOSRestBundle(), new JMS\SerializerBundle\JMSSerializerBundle() in appKernel.php, but this error still appears

InvalidArgumentException: "fos_rest.serializer" must implement FOS\RestBundle\Serializer\Serializer (instance of "JMS\Serializer\Serializer" given).
in C:\wamp\www\RESTSymfonyAPI\src\FOS\RestBundle\DependencyInjection\Compiler\SerializerConfigurationPass.php line 57
at SerializerConfigurationPass->process(object(ContainerBuilder)) in C:\wamp\www\RESTSymfonyAPI\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Compiler\Compiler.php line 117
at Compiler->compile(object(ContainerBuilder)) in C:\wamp\www\RESTSymfonyAPI\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\ContainerBuilder.php line 613
at ContainerBuilder->compile() in C:\wamp\www\RESTSymfonyAPI\app\bootstrap.php.cache line 2502
at Kernel->initializeContainer() in C:\wamp\www\RESTSymfonyAPI\app\bootstrap.php.cache line 2281
at Kernel->boot() in C:\wamp\www\RESTSymfonyAPI\app\bootstrap.php.cache line 2312
at Kernel->handle(object(Request)) in C:\wamp\www\RESTSymfonyAPI\web\app_dev.php line 28

Most helpful comment

Well you can use the framework bundle tags instead of having your own serializer service:

# config.yml
framework:
    serializer: true
# services.yml
services:
    api.normalizer.competition:
        tags:
            - { name: serializer.normalizer }

    # ...

    api.encoder.json:
        tags:
            - { name: serializer.encoder }

All 10 comments

Which version of FOSRestBundle do you use? Did you try to use your own serializer? Can you please show your FOSRest related configuration?

my config.yml is as follow

imports:
    - { resource: parameters.yml }
    - { resource: security.yml }

framework:
    #esi:             ~
    #translator:      { fallback: "%locale%" }
    secret:          "%secret%"
    router:
        resource: "%kernel.root_dir%/config/routing.yml"
        strict_requirements: ~
    form:            ~
    csrf_protection: ~
    validation:      { enable_annotations: true }
    templating:
        engines: ['twig']
        #assets_version: SomeVersionScheme
    default_locale:  "%locale%"
    trusted_hosts:   ~
    trusted_proxies: ~
    session:
        # handler_id set to null will use default session handler from php.ini
        handler_id:  ~
    fragments:       ~
    http_method_override: true

# Twig Configuration
twig:
    debug:            "%kernel.debug%"
    strict_variables: "%kernel.debug%"

# Assetic Configuration
assetic:
    debug:          "%kernel.debug%"
    use_controller: false
    bundles:        [ ]
    #java: /usr/bin/java
    filters:
        cssrewrite: ~
        #closure:
        #    jar: "%kernel.root_dir%/Resources/java/compiler.jar"
        #yui_css:
        #    jar: "%kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar"

# Doctrine Configuration
doctrine:
    dbal:
        driver:   "%database_driver%"
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset:  UTF8
        # if using pdo_sqlite as your database driver, add the path in parameters.yml
        # e.g. database_path: "%kernel.root_dir%/data/data.db3"
        # path:     "%database_path%"

    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        auto_mapping: true

# Swiftmailer Configuration
swiftmailer:
    transport: "%mailer_transport%"
    host:      "%mailer_host%"
    username:  "%mailer_user%"
    password:  "%mailer_password%"
    spool:     { type: memory }

@NadaBen could you answer this questions too please?

Which version of FOSRestBundle do you use? Did you try to use your own serializer?

Also had this problem when upgrading to 2.0-BETA2, using my own custom serializer.

config.yml

fos_rest:
    view:
        view_response_listener: true
    serializer:
        serialize_null: true
    service:
        serializer: api.serializer.default

services.yml

    api.serializer.default:
        class: 'FOS\RestBundle\Serializer\Serializer'
        arguments:
            0:
                - '@api.normalizer.competition'
                - '@api.normalizer.round'
                - '@api.normalizer.pick'
                - '@api.normalizer.group'
                - '@api.normalizer.position'
                - '@api.normalizer.prediction'
                - '@api.normalizer.taxonomy'
                - '@serializer.normalizer.object'
            1:
                - '@api.encoder.json'

Error:

  [InvalidArgumentException]
  "fos_rest.serializer" must implement FOS\RestBundle\Serializer\Serializer (instance of "FOS\RestBund
  le\Serializer\Serializer" given).

FOS\RestBundle\Serializer\Serializer is just an interface. You cannot use it as the class name in your DI configuration. Do you have your own custom serializer class and if so how does it look like? What exactly do you have to customise in the serialisation process?

@xabbuh: Ah I just realised this and was about to respond again! Teach me to read what I'm doing before commenting so quickly.

Basically all I want to be able to do is what I was doing in 1.7 which is specifying the Symfony normalizers it picks from, which I used to do like this:

    api.serializer.default:
        class: 'Symfony\Component\Serializer\Serializer'
        arguments:
            0:
                - '@api.normalizer.competition'
                - '@api.normalizer.round'
                - '@api.normalizer.pick'
                - '@api.normalizer.group'
                - '@api.normalizer.position'
                - '@api.normalizer.prediction'
                - '@api.normalizer.taxonomy'
                - '@serializer.normalizer.object'
            1:
                - '@api.encoder.json'

But with that configuration I get:

 [InvalidArgumentException]
  "fos_rest.serializer" must implement FOS\RestBundle\Serializer\Serializer (instance of "Symfony\Comp
  onent\Serializer\Serializer" given).

Now I understand that the Symfony Serializer isn't going to implement the FOS RestBundle Serializer interface, but presumably I can make a very simple class which extends the Symfony Serializer and implements the interface, unless there is a better way?

@araines We have the SymfonySerializerAdapter which is responsible to build the bridge between our interface and the Symfony Serializer component (there is a similar adapter for the JMSSerializer). I hope this helps you achieving what you are aiming for. But feel free to ask further questions if anything is not clear (enough).

Why don't you let the framework bundle configure symfony the serializer?
You would be able then to use the serializer tags to set normalizers and encoders and FOSRestBundle would configure it automatically.

@Ener-Getick that sounds like exactly what I'm trying to achieve, but I'm a little unclear about how exactly to achieve it. I've got it working for now like this:

config.yml

fos_rest:
    view:
        view_response_listener: true
    serializer:
        serialize_null: true
    service:
        serializer: api.serializer.bridge

services.yml

    api.serializer.bridge:
        class: 'FOS\RestBundle\Serializer\SymfonySerializerAdapter'
        arguments:
            - '@api.serializer.default'

    api.serializer.default:
        class: 'Symfony\Component\Serializer\Serializer'
        arguments:
            0:
                - '@api.normalizer.competition'
                - '@api.normalizer.round'
                - '@api.normalizer.pick'
                - '@api.normalizer.group'
                - '@api.normalizer.position'
                - '@api.normalizer.prediction'
                - '@api.normalizer.taxonomy'
                - '@serializer.normalizer.object'
            1:
                - '@api.encoder.json'

But it does feel like I'm jumping around a fair bit to do something which can probably be achieved in a more simple way.

Well you can use the framework bundle tags instead of having your own serializer service:

# config.yml
framework:
    serializer: true
# services.yml
services:
    api.normalizer.competition:
        tags:
            - { name: serializer.normalizer }

    # ...

    api.encoder.json:
        tags:
            - { name: serializer.encoder }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

matrix818181 picture matrix818181  路  7Comments

oliver13 picture oliver13  路  3Comments

MaksSlesarenko picture MaksSlesarenko  路  4Comments

maxbeckers picture maxbeckers  路  4Comments

pathmissing picture pathmissing  路  4Comments