Skip to content

With QueryParameter, it is not possible to set multiple filters for the same property #7493

@ttskch

Description

@ttskch

API Platform version(s) affected: 4.2.2

Description

As the document warns, ApiFilter is currently deprecated and using QueryParameter is recommended.

However, there are several bugs when applying filters using QueryParameter.

With QueryParameter, it is not possible to set multiple filters for the same property

For example, if we want to apply both NumericFilter and RangeFilter to the id property, we can achieve this with ApiFilter as follows:

#[ApiFilter(NumericFilter::class, properties: ['id'])]
#[ApiFilter(RangeFilter::class, properties: ['id'])]

This will output the following parameters in OpenAPI, and these parameters will actually function correctly.

On the other hand, when using QueryParameter, we need to write it like this: (The key must be the same as the actual property name.)

#[QueryParameter(key: 'id', filter: 'serviceId.for.numeric_filter')]
#[QueryParameter(key: 'id', filter: 'serviceId.for.range_filter')]

However, if we define multiple QueryParameters with the same key like this, the later definition overwrites the earlier one, so in this case, the NumericFilter will not work. The OpenAPI output will be as follows:

Note

Although writing it like this should be equivalent, currently there is a bug (#7492) where duplicate keys are only allowed when using the parameters argument, so in this case, the NumericFilter happens to work.

#[ApiResource(
    parameters: [
        new QueryParameter(key: 'id', filter: 'serviceId.for.numeric_filter'),
        new QueryParameter(key: 'id', filter: 'serviceId.for.range_filter'),
    ],
)]

However, this is not the intended behavior.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions