Core: [2.4.3] Unused binding "$requestStack" error if a filter class overrides constructor of AbstractFilter

Created on 17 May 2019  路  10Comments  路  Source: api-platform/core

After updating to v2.4.3 I started getting Unused binding "$requestStack" in service "Extended\From\AbstractFilter\MyFilter" error.

Here is my config:

_defaults:
    autowire: true
    autoconfigure: true
    public: false

Extended\From\AbstractFilter\MyFilter:
    arguments:
        $managerRegistry: '@doctrine'
        $myParam: 'value'
    tags:
        - { name: 'api_platform.filter', id: 'my_filter' }

And the filter pseudocode:

<?php

namespace Extended\From\AbstractFilter;

use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\AbstractFilter;
use Doctrine\ORM\QueryBuilder;
use Psr\Log\LoggerInterface;
use Symfony\Bridge\Doctrine\ManagerRegistry;

class MyFilter extends AbstractFilter
{
    /** @var string */
    private $myParam;

    public function __construct(ManagerRegistry $managerRegistry, LoggerInterface $logger = null, string $myParam)
    {
        parent::__construct($managerRegistry, null, $logger);
        $this->myParam = $myParam;
    }

    public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null)
    {
         //handle
    }

    public function getDescription(string $resourceClass): array
    {
        return [];
    }
}

I was able to fix the error by switching off autowire and autoconfigure for the service:

Extended\From\AbstractFilter\MyFilter:
    autowire: false #was added
    autoconfigure: false #was added
    arguments:
        $managerRegistry: '@doctrine'
        $myParam: 'value'
        $logger: '@logger' #was added
    tags:
        - { name: 'api_platform.filter', id: 'my_filter' }

The further research showed me that the problem is in the overridden constructor.
If I remove the constructor then autowiring works with the original service definition.

Looks like this issue was introduced by https://github.com/api-platform/core/pull/2664

question

All 10 comments

It's linked to autowiring not able to use ?RequestStack? @meyerbaptiste any ideas?

To me this seems like a Symfony bug.

Experiencing the same. Workaround is to disable autowiring/autoconfigure and add a tag manually (tags: ['api_platform.filter'])

Hi everyone,
Any solution for symfony 4 ? I can't workaround the problem...

I specified 2 workarounds in the issue description: either disable autowire/autoconfigure of your custom filter or avoid overriding the constructor, replace it with setters, for example.

Yes I tried your workarounds but I use a custom doctrine filter that extends AbstractContextAwareFilter and somehow it still has that error :

Unused binding "$requestStack" in service "filter.my_custom_filter".

Here's my yml just in case:

filter.my_custom_filter: autowire: false autoconfigure: false parent: 'App\Filter\MyCustomFilter' arguments: [{ slug: ~ }] tags: [ 'api_platform.filter' ]

Filter example : https://api-platform.com/docs/core/filters/#creating-custom-doctrine-orm-filters

Thanks in advance for your help

@quentinphilbert Do you have a custom constructor? This problem should only happen if you have a custom constructor.

Anyway, this looks weird to me:

    parent: 'App\Filter\MyCustomFilter'

Are you sure that's what you intended?

Your filter configuration doesn't look correct: parent should be renamed to class and the arguments seems to be wrong (unless you are still overriding constructor in order to handle { slug: ~ } argument).
Try the example that I provided and clear the cache.

@quentinphilbert If you're following the docs, just use @ApiFilter annotation. You don't need to declare a service. (We should perhaps rewrite that section of the docs.)

Thanks guys it's all good now. I used :

App\Filter\MyFilter:
    arguments: ['@doctrine', '@?logger', { slug: ~ }]
    tags:
      - { name: 'api_platform.filter', id: 'filter.my_filter' }

And this constructor :

public function __construct(ManagerRegistry $managerRegistry, LoggerInterface $logger = null)
    {
        parent::__construct($managerRegistry, null, $logger);
    }

It works fine but the only problem I have now is that my filter doesn't show up in the responses of my GETs (it was in "hydra:search" and now there's nothing) but it's really minor. Thanks again !

Was this page helpful?
0 / 5 - 0 ratings