-1

Can you help to fix the following error?

InvalidArgumentException: "fos_rest.serializer" must implement FOS\RestBundle\Serializer\Serializer (instance of "JMS\Serializer\Serializer" given).
in C:\wamp\www\SymfonyRestAPI\src\FOS\RestBundle\DependencyInjection\Compiler\SerializerConfigurationPass.php line 58
at SerializerConfigurationPass->process(object(ContainerBuilder)) in C:\wamp\www\SymfonyRestAPI\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Compiler\Compiler.php line 117
at Compiler->compile(object(ContainerBuilder)) in C:\wamp\www\SymfonyRestAPI\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\ContainerBuilder.php line 613
at ContainerBuilder->compile() in C:\wamp\www\SymfonyRestAPI\app\bootstrap.php.cache line 2502
at Kernel->initializeContainer() in C:\wamp\www\SymfonyRestAPI\app\bootstrap.php.cache line 2281
at Kernel->boot() in C:\wamp\www\SymfonyRestAPI\app\bootstrap.php.cache line 2312
at Kernel->handle(object(Request)) in C:\wamp\www\SymfonyRestAPI\web\app_dev.php line 28
Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
Ndb
  • 11
  • 4
  • This is too broad. But i think you must add the code of your config file. Which class is related to "fos_rest.serializer" – Brice Favre Mar 14 '16 at 09:16
  • I followed all the steps of symfony tutorial but always the same error, in appKernel.php file i added new FOS\RestBundle\FOSRestBundle(), new JMS\SerializerBundle\JMSSerializerBundle() – Ndb Mar 14 '16 at 09:21
  • in composer.json i added "friendsofsymfony/rest-bundle": "0.11.*", "jms/serializer-bundle": "0.12.x-dev" but same error again – Ndb Mar 14 '16 at 09:23
  • Please add the content of your comments in the answer. You should provide a [mcve]. – A.L Mar 14 '16 at 14:27
  • @A.L probably means: 'Please add the content of your comments in the *question*' – GitaarLAB Mar 31 '16 at 16:01
  • 1
    @GitaarLAB you're right, thanks for your comment. – A.L Mar 31 '16 at 16:04

1 Answers1

4

This exception has been introduced in FOSRestBundle 2.0 (see here) because of a serializer abstraction layer introduced in 2.0 too. It is normally automatically managed by the bundle which detects the JMS serializer.

Did you add the JMSSerializerBundle to your kernel as following?

// app/AppKernel.php
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new JMS\SerializerBundle\JMSSerializerBundle(),,
        );

        // ...
    }
}

This should be the problem here.

If it isn't, then maybe you tried to define directly the serializer FOSRestBundle uses, the following is a bad practice most of the time and should only be used if you know what you do:

// services.yml
services:
    fos_rest.serializer:
        // This won't work in case your class doesn't
        // implement FOS\RestBundle\Serializer\Serializer
        class: My\Serializer 

If my second proposition is your case, you custom serializer has to implement the interface FOS\RestBundle\Serializer\Serializer.

Guilhem N
  • 218
  • 1
  • 3
  • 9
  • 1
    How do you do that? Can you please explain it in your answer? – A.L Mar 31 '16 at 16:05
  • 1
    Sorry I mean this could be what makes this error happen, I'll improve my answer. – Guilhem N Mar 31 '16 at 16:07
  • https://github.com/FriendsOfSymfony/FOSRestBundle/commit/db66463430d048fbe38619df35490c2980c3e82c#diff-b4c2bb665099019e5209acdfe6371d2a – Mick Oct 17 '17 at 01:01