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

[Serializer] Type error in appDevDebugProjectContainer when using serializer #17676

Closed
jverdeyen opened this issue Feb 4, 2016 · 15 comments
Closed

Comments

@jverdeyen
Copy link
Contributor

Symfony 2.8.1
PHP 7.0.2.1 (debian)

I've enabled the Symonfy serializer.

I'm using the "friendsofsymfony/rest-bundle": "^1.7" with the following config:

fos_rest:
    param_fetcher_listener: force
    routing_loader:
        include_format:       false
    view:
        mime_types:
            json: ['application/json', 'application/json;version=1.0', 'application/json;version=1.1', 'application/json;version=1.2']
        view_response_listener: 'force'
        formats:
            xml:  true
            json: true
        templating_formats:
            html: true
    format_listener:
        rules:
            - { path: ^/, priorities: [ html, json, xml ], fallback_format: ~, prefer_extension: true }
        media_type:
            version_regex: '/(v|version)=(?P<version>[0-9\.]+)/'
    exception:
        codes:
            'Symfony\Component\Routing\Exception\ResourceNotFoundException': 404
            'Doctrine\ORM\OptimisticLockException': HTTP_CONFLICT
        messages:
            'Symfony\Component\Routing\Exception\ResourceNotFoundException': true
    allowed_methods_listener: true
    access_denied_listener:
        json: true
    body_listener: true

And this is a part of my controller action:

public function getNewsAction(Request $request)
    {
       ...
        $news = [generator news]..
       ...

        $view = $this->view($news);
        return $this->handleView($view, 200);
    }

I'm trying to debug the following error:

An Exception was thrown while handling: Type error: Argument 1 passed to 
FOSRestBundleSerializerSymfonySerializerAdapter_0000000052c9fc6b000000005a32441d7f491784
a700368952f1ec7813de5f79::__construct() must implement interface 
Symfony\Component\Serializer\SerializerInterface, instance of Closure given, called in 
/tmpfs/dev/cache/appDevDebugProjectContainer.php on line 2766

Even when using the JMSSerializer I get:

An Exception was thrown while handling: Type error: Argument 1 passed to 
FOSRestBundleSerializerJMSSerializerAdapter_000000003e79141b0000000013a926a47f491784a70
0368952f1ec7813de5f79::__construct() must implement interface JMS\Serializer\SerializerInterface, 
instance of Closure given, called in /tmpfs/dev/cache/appDevDebugProjectContainer.php on line 2789

This is the generated code:

/**
     * Gets the 'fos_rest.serializer' service.
     *
     * This service is shared.
     * This method always returns the same instance of the service.
     *
     * @param bool    $lazyLoad whether to try lazy-loading the service with a proxy
     *
     * @return \FOS\RestBundle\Serializer\JMSSerializerAdapter A FOS\RestBundle\Serializer\JMSSerializerAdapter instance.
     */
    public function getFosRest_SerializerService($lazyLoad = true)
    {
        if ($lazyLoad) {
            $container = $this;

            return $this->services['fos_rest.serializer'] = new FOSRestBundleSerializerJMSSerializerAdapter_000000003e79141b0000000013a926a47f491784a
700368952f1ec7813de5f79(
                function (&$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface $proxy) use ($container) {
                    $wrappedInstance = $container->getFosRest_SerializerService(false);

                    $proxy->setProxyInitializer(null);

                    return true;
                }
            );
        }

        return new \FOS\RestBundle\Serializer\JMSSerializerAdapter($this->get('jms_serializer'));
    }

Any idea where I should start looking for this problem?
I've also seen it in another project (unsolved).

@xabbuh
Copy link
Member

xabbuh commented Feb 4, 2016

Can you show the output of composer show -i friendsofsymfony/rest-bundle please?

@jverdeyen
Copy link
Contributor Author

root@525db9f02719:/var/www/app# composer show -i friendsofsymfony/rest-bundle
name     : friendsofsymfony/rest-bundle
descrip. : This Bundle provides various tools to rapidly develop RESTful API's with Symfony
keywords : rest
versions : * 1.8.x-dev
type     : symfony-bundle
license  : MIT License (MIT) (OSI approved) https://spdx.org/licenses/MIT.html#licenseText
source   : [git] https://github.com/FriendsOfSymfony/FOSRestBundle.git 95dbe97d682e7c3f50ee53947c348c76350a0153
dist     : [zip] https://api.github.com/repos/FriendsOfSymfony/FOSRestBundle/zipball/95dbe97d682e7c3f50ee53947c348c76350a0153 95dbe97d682e7c3f50ee53947c348c76350a0153
names    : friendsofsymfony/rest-bundle

autoload
psr-0
FOS\RestBundle => .

requires
doctrine/inflector ~1.0
php ^5.3.9|~7.0
psr/log ~1.0
symfony/framework-bundle ~2.3|~3.0
symfony/http-kernel ^2.3.24|~3.0
willdurand/jsonp-callback-validator ~1.0
willdurand/negotiation ~1.2

requires (dev)
jms/serializer ~0.13|~1.0
jms/serializer-bundle ~0.12|~1.0
phpoption/phpoption ~1.1.0
sensio/framework-extra-bundle ~2.0|~3.0
sllh/php-cs-fixer-styleci-bridge ^1.3
symfony/browser-kit ~2.3|~3.0
symfony/dependency-injection ~2.3|~3.0
symfony/form ~2.3|~3.0
symfony/phpunit-bridge ~2.7|~3.0
symfony/security ~2.3|~3.0
symfony/serializer ~2.3|~3.0
symfony/validator ~2.3|~3.0
symfony/yaml ~2.3|~3.0

suggests
jms/serializer-bundle Add support for advanced serialization capabilities, recommended, requires ~0.12||~1.0
sensio/framework-extra-bundle Add support for route annotations and the view response listener, requires ~3.0
symfony/serializer Add support for basic serialization capabilities and xml decoding, requires ~2.3
symfony/validator Add support for validation capabilities in the ParamFetcher, requires ~2.3

conflicts
jms/serializer <0.12
jms/serializer-bundle <0.11
symfony/validator >=2.5.0,<2.5.5

@jmcclell
Copy link

jmcclell commented Feb 4, 2016

I'm having the same issue with 3.0.

@jverdeyen
Copy link
Contributor Author

@jmcclell: Tested on 1.7.7 of the FOSRestBundle and this is working.

@jakzal
Copy link
Contributor

jakzal commented Feb 5, 2016

@jverdeyen isn't this an issue with the 1.8-dev version of FOSRestBundle?

@xabbuh
Copy link
Member

xabbuh commented Feb 5, 2016

The feature making use of the lazy services was introduced in the 1.8 dev branch, but it doesn't look that we do anything strange with the lazy services: FriendsOfSymfony/FOSRestBundle@b264edc#diff-6fb4ad4aa9f78da8ab275d84ae7ea143

I try to have a look at this this weekend and figure out if this is an issue in Symfony or in FOSRestBundle.

@jmcclell
Copy link

jmcclell commented Feb 5, 2016

https://github.com/jmcclell/symfony-testcase-17676

vagrant@testcase-vm:/vagrant/testcase$ vendor/bin/phpunit
PHPUnit 5.4-gc36aa9f by Sebastian Bergmann and contributors.

F                                                                   1 / 1 (100%)

Time: 920 ms, Memory: 22.00Mb

There was 1 failure:

1) Tests\AppBundle\Controller\DefaultControllerTest::testIndex
Failed asserting that 500 matches expected 200.

/vagrant/testcase/tests/AppBundle/Controller/DefaultControllerTest.php:13

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
[2016-02-05 14:43:02] request.CRITICAL:
Uncaught PHP Exception PHPUnit_Framework_Error:
 "Object of class Closure could not be converted to string"
at /vagrant/testcase/src/AppBundle/Service/MessageService.php line 9 {"exception":"[object] (PHPUnit_Framework_Error(code: 4096): Object of class Closure could not be converted to string
at /vagrant/testcase/src/AppBundle/Service/MessageService.php:9)"} []

Here's a basic test case. It actually isn't FOSRestBundle at all. In my case, because I've been using minimum-stability = dev while doing some upgrading and testing of new components, I had updated to the latest Doctrine Migrations which uses the 2.0.x branch of the Proxy package.

The bug appears to be with the Proxy package itself, or, perhaps, Symfony's usage of it. I'll be backing to stable versions and avoiding the 2.0.x branch, so that will fix things for me for now, but it looks like this will need addressed from one end or the other at some point.

@boast
Copy link

boast commented Feb 24, 2016

The proxy bridge seems to handle the lazy-proxy generation wrong (as now a Closure will be generated) thus the line here: https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/ProxyManager/LazyProxy/PhpDumper/ProxyDumper.php#L80

should be changed from:
$instantiation new $proxyClass
to
$instantiation $proxyClass::staticProxyConstructor
and everything should be working fine again. However, this requires the 2.0 branch of the ocramius/proxy-manager package. To be as compatible as possible, may check for a closure instance and choose the appropriate template after that.

@stof
Copy link
Member

stof commented Feb 24, 2016

@Ocramius could you have a look at this issue please ?

@stof
Copy link
Member

stof commented Feb 24, 2016

@boast on a side note, we don't officially support 2.0 versions of ProxyManager, so getting errors when using may totally be expected.

@boast
Copy link

boast commented Feb 24, 2016

I know that :) Maybe the BC-break was unintentional? As mentioned before,
doctrine/migrations allowed the version 2 and in symfonys composer.json,
the proxy-manager is only a dev-requirement.

On Wed, 24 Feb 2016 at 13:08 Christophe Coevoet notifications@github.com
wrote:

@boast https://github.com/boast on a side note, we don't officially
support 2.0 versions of ProxyManager, so getting errors when using may
totally be expected.


Reply to this email directly or view it on GitHub
#17676 (comment).

@Ocramius
Copy link
Contributor

Yes, the BC break was intentional here, although I am not sure if I missed it in the upgrade docs. Needs either a context switch (check for PM version) or relying on ProxyManager's factories (which encapsulate this behaviour)

@Ocramius
Copy link
Contributor

@Ocramius
Copy link
Contributor

Yes, I think the quickest fix here would be to add a conditional around

to either use new $proxyClassName or $proxyClassName::staticProxyConstructor(). Since no reflection of the proxy is available there, the quickest solution would be to check for the ProxyManager existence of the StaticProxyConstructor method generator.

@Ocramius
Copy link
Contributor

I provided a fix via #17919

Ocramius added a commit to Ocramius/symfony that referenced this issue Feb 28, 2016
Ocramius added a commit to Ocramius/symfony that referenced this issue Feb 28, 2016
Ocramius added a commit to Ocramius/symfony that referenced this issue Feb 28, 2016
Ocramius added a commit to Ocramius/symfony that referenced this issue Feb 28, 2016
fabpot added a commit that referenced this issue Feb 28, 2016
This PR was merged into the 2.3 branch.

Discussion
----------

Fix - #17676 (backport #17919 to 2.3)

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #17676
| License       | MIT
| Doc PR        |

This is a backport of #17919

Commits
-------

0c6400a  #17676 - making the proxy instantiation compatible with ProxyManager 2.x by detecting proxy features
fabpot added a commit that referenced this issue Feb 28, 2016
* 2.3:
  #17676 - making the proxy instantiation compatible with ProxyManager 2.x by detecting proxy features
  Fix bug when using an private aliased factory service
  ChoiceFormField of type "select" could be "disabled"
  Update contributing docs
  [Console] Fix escaping of trailing backslashes
  Fix constraint validator alias being required
  [ci] clone with depth=1 to kill push-forced PRs
  Add check on If-Range header
fabpot added a commit that referenced this issue Feb 28, 2016
* 2.7:
  #17676 - making the proxy instantiation compatible with ProxyManager 2.x by detecting proxy features
  Fix bug when using an private aliased factory service
  ChoiceFormField of type "select" could be "disabled"
  Update contributing docs
  [Console] Fix escaping of trailing backslashes
  Fix constraint validator alias being required
  [ci] clone with depth=1 to kill push-forced PRs
  Add check on If-Range header
fabpot added a commit that referenced this issue Feb 28, 2016
* 2.8:
  #17676 - making the proxy instantiation compatible with ProxyManager 2.x by detecting proxy features
  Fix bug when using an private aliased factory service
  ChoiceFormField of type "select" could be "disabled"
  Update contributing docs
  [Console] Fix escaping of trailing backslashes
  Fix constraint validator alias being required
  [ci] clone with depth=1 to kill push-forced PRs
  Add check on If-Range header
fabpot pushed a commit that referenced this issue Feb 28, 2016
fabpot added a commit that referenced this issue Feb 28, 2016
…oxyManager 2.x by detecting proxy features (Ocramius)

This PR was submitted for the master branch but it was merged into the 3.0 branch instead (closes #17919).

Discussion
----------

#17676 - making the proxy instantiation compatible with ProxyManager 2.x by detecting proxy features

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #17676
| License       | MIT
| Doc PR        |

Note: tests don't pass because the test asset being used does not respect the return type hints (PHP7) required for ProxyManager v2. Not sure if I should just hack around it, and use the PHP7 hints.

Commits
-------

a8f1a10 #17676 - making the proxy instantiation compatible with ProxyManager 2.x by detecting proxy features
@fabpot fabpot closed this as completed Feb 28, 2016
fabpot added a commit that referenced this issue Feb 28, 2016
* 3.0:
  #17676 - making the proxy instantiation compatible with ProxyManager 2.x by detecting proxy features
  #17676 - making the proxy instantiation compatible with ProxyManager 2.x by detecting proxy features
  Fix bug when using an private aliased factory service
  [Form] fix tests added by #17798 by removing `choices_as_values`
  [Form] fix FQCN in tests added by #17798
  [DependencyInjection] Remove unused parameter of private property
  bug #17798 [Form] allow `choice_label` option to be `false`
  [Form] fix tests added by #17760 with FQCN
  ChoiceFormField of type "select" could be "disabled"
  Update contributing docs
  [Console] Fix escaping of trailing backslashes
  Fix constraint validator alias being required
  [DependencyInjection] Simplified code in AutowirePass
  [ci] clone with depth=1 to kill push-forced PRs
  Add check on If-Range header
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants