core icon indicating copy to clipboard operation
core copied to clipboard

Specific OrderFilter removes generic OrderFilter

Open Cruiser13 opened this issue 1 year ago • 2 comments

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.

Cruiser13 avatar Jan 16 '24 14:01 Cruiser13

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 avatar Jan 16 '24 16:01 soyuka

@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?

Cruiser13 avatar Jan 19 '24 11:01 Cruiser13

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 avatar Mar 15 '24 10:03 soyuka

@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?

Cruiser13 avatar Mar 15 '24 15:03 Cruiser13

use a filter service (on the docs open the yaml code tab)

soyuka avatar Mar 19 '24 09:03 soyuka