Skip to content

Filter parameters set to [] are discarded #278

@danbernier

Description

@danbernier

Observed behavior

When filtering, I'm seeing filter parameters get dropped if their value is an empty array.

Resource.where(field: nil) #=> a GET to `/resources?filter[field]`, great!
Resource.where(field: [])  #=> a GET to `/resources`, with no filter params

Expected behavior

I think empty array parameters should be passed through the same way as nil parameters are.

Motivating example

I'm fetching a list of IDs to then fetch from the API (fetching them from a recommender, from user's favorites, etc), and it could be empty; when I look up the resources by ID, if the list is empty, then the API returns me the 25 latest resources.

ids_to_query = calculate_list_of_ids
Resource.where(id: ids_to_query) # If it's empty, I get the 25 latest

We like the API's behavior, to return the latest 25 resources when there are no filter parameters, and we rely on it in other places, by specifying no filter criteria. But in this case, I want to filter by ID, and expect no results if there are no IDs.

Other notes

From my testing:

  • It's not about the field being named id; I also saw this when filtering by a fake field named foo
  • It's not strictly about the empty array; I saw the same with an empty hash. For some reason, nil, empty strings, and whitespace strings are passed through; it's only empty arrays and hashes that are dropped. (Honestly, I can't see a reason for filtering by a hash, I was just testing different kinds of empty values.)

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