I am using the following docs to create a custom filter https://api-platform.com/docs/core/filters#creating-custom-filters
It states to extend the AbstractFilter as a helper for creating the filter but on doing so I am receiving the following deprecation error.
User Deprecated: Passing an instance of "Symfony\Component\HttpFoundation\RequestStack" is deprecated since 2.2. Use "filters" context key instead.
What is the alternative at this stage?
It's weird... Do you use autowiring? Can you provide a minimal reproducer on GitHub?
Yeah, it is a bit odd. Here is a quick knock up to replicate the issue
https://github.com/townsymush/api-platform-deprecation-error
@townsymush You should try to update your DomainFilter to extend ApiPlatform\Core\Bridge\Doctrine\Orm\FilterAbstractContextAwareFilter instead of AbstractFilter if I remember/read correctly
@antograssiot unfortunately, although the filter still works after changing it to the AbstractContextAwareFilter I still get the deprecation notice. I also cleared the cache and restarted the server just to be sure.
https://github.com/townsymush/api-platform-deprecation-error/blob/master/config/services.yaml#L30
you're passing the request stack, you need to remove it as explained in the message then
Yes, I understand that, but if I do this my filter will not run.
https://github.com/api-platform/core/blob/master/src/Bridge/Doctrine/Orm/Filter/AbstractFilter.php#L66
So it's not clear on my alternative
Can't look deeper right now but from what i know aftrr upgrading several filters, https://github.com/api-platform/core/blob/master/src/Bridge/Doctrine/Orm/Filter/AbstractContextAwareFilter.php#L24 your filter should not be in a position to call the parent::apply() that you mentionned...This is why you need the new abstract filter that I mentionned earlier. Is your context set correctly ?
Yes, I understand that, but if I do this my filter will not run.
https://github.com/api-platform/core/blob/master/src/Bridge/Doctrine/Orm/Filter/AbstractFilter.php#L66
It is overridden in https://github.com/api-platform/core/blob/v2.2.5/src/Bridge/Doctrine/Orm/Filter/AbstractContextAwareFilter.php so you'll be fine.
Thanks both. I do not have any context set, which is why it is calling the parent::apply method. Where would I set the context for the filter?
Hey guys, so this issue seems related to using the @ApiResource(attributes={"filters"={RegexpFilter::class}}) Annotation. Do you think it should be removed from the documentation? Or a warning stating this is deprecated and state the preferred @ApiFilter annotation as the filters context isn't that clear on what the problem is.
I encountered the same issue while upgrading api platform to 2.2. To fix the issue I had to change my filter to extend AbstractContextAwareFilter rather than AbstractFilter.
And then declare the service like this:
'AppBundle\Filter\FullNameFilter':
arguments: [ '@doctrine' ]
tags: [ 'api_platform.filter' ]
Rather than:
'AppBundle\Filter\FullNameFilter':
arguments: [ '@doctrine', '@request_stack', '@?logger' ]
tags: [ 'api_platform.filter' ]
Closing, thanks @raziel057 for the answer.
Most helpful comment
@townsymush You should try to update your
DomainFilterto extend ApiPlatform\Core\Bridge\Doctrine\Orm\FilterAbstractContextAwareFilter instead ofAbstractFilterif I remember/read correctly