Empty search does not return descriptive message

The Front-End does not allow empty search and displays a helpful message explaining the search term is too short.

However, the API endpoint still allows empty search but the response is a JSON object with a NULL value and 200 status.

Shouldn’t the API respond with a descriptive error message and a 400 status?

Expected:

// 400 error
{
  "errors": [ "You supplied invalid parameters to the request: q" ],
  "error_type": "invalid_parameters"
}

Actual:

// 200 ok
{
  "grouped_search_result": null
}
2 Likes

The API should certainly can be improved here, but the query route allows for a lot of args:

https://github.com/discourse/discourse/blob/eed9561840120ea2c77c50fbc8fa61ba0a123b1e/app/controllers/search_controller.rb#L99-L114

We would need to be very careful here to check one we treat min_search_term_length as a requirement vs an optional thing.

2 Likes

Thanks @sam

As it stands:

  • Empty search is a dead spot in the API
  • Search endpoint accepts a search term but also metadata arguments (user, categories, tags, etc.)
  • Metadata arguments are not documented in the API
  • Front-end validates the search correctly, but back-end API validates it partially (allowing empty search to go through)
  • Response message should be descriptive in any case

I hope this overview helps.
IMO this a bug. I’d be surprised if this is the intended behavior.
Thanks anyway :v:

2 Likes