core
core copied to clipboard
Specific OrderFilter removes generic OrderFilter
API Platform version(s) affected: 3.1.25
Description
According to the docs we should be able to enable a filter for all properties of a resource by setting #[ApiFilter(OrderFilter::class)] above the class name. And we still have to enable filters on nested properties explicitly.
The issue here is that setting such an explicit OrderFilter will remove the general OrderFilter setting.
How to reproduce
Example:
// This filter gets applied
#[ApiFilter(OrderFilter::class, properties: ['customer.name'])]
// This one will be ignored - but it should not be ignored
#[ApiFilter(OrderFilter::class)]
class Invoice
{
#[ORM\Column(type: 'string', length: 255, nullable: true)]
//We should have the option to order by this invoiceNumber but we don't if we add the specific filter for customer.name
private $invoiceNumber;
#[ORM\ManyToOne(targetEntity: Customer::class, inversedBy: 'invoices')]
// This one would also work, but still disables the global filter.
#[ApiFilter(OrderFilter::class, properties: ['customer.name'])]
private $customer;
}
I'm happy to provide further details if needed.
It's expected there is a name collision when filter services get declared on your second filter. The best thing for that kind of use cases is to specify filters as services manually. Or maybe we need to be able to fix the name collision at https://github.com/api-platform/core/blob/main/src/Symfony/Bundle/DependencyInjection/Compiler/AttributeFilterPass.php
@soyuka not sure why there would be a naming collision. The automated filter should be order[invoiceNumber] and order[customer] while the specific one would be order[customer.name] or am I mistaken here?
because we don't support multiple declaration like this for filters, you should use a service to solve the problem, also #5995 will help with that
@soyuka thank you for your reply. I read through the new docs and I'm not sure how we could add more than one orderfilter with the new system. We can add multiple properties but not one general order filder and an additional one for the specific properties. Could you give me a hint?
use a filter service (on the docs open the yaml code tab)