Return an empty result set instead of 404 in case users are not available. (#5238)

A common response in collection endpoint with REST APIs is to return an empty result set when the requested params end up in an empty result. The reason for this is that the resource itself exists (the collection always exists), it just is empty.

See https://github.com/thepracticaldev/dev.to/pull/5192
This commit is contained in:
rhymes 2019-12-24 19:08:10 +01:00 committed by Ben Halpern
parent 9638e3da26
commit 2d6df70dea
2 changed files with 5 additions and 5 deletions

View file

@ -10,13 +10,14 @@ module Api
@users = User.where(username: usernames)
return
end
if params[:state] == "follow_suggestions"
@users = Suggester::Users::Recent.new(current_user).suggest
elsif params[:state] == "sidebar_suggestions"
given_tag = params[:tag]
@users = Suggester::Users::Sidebar.new(current_user, given_tag).suggest.sample(3)
else
error_not_found
@users = User.none
end
end

View file

@ -6,11 +6,10 @@ RSpec.describe "Api::V0::Users", type: :request do
end
describe "GET /api/users" do
it "returns not found status if state is not present" do
user = create(:user)
sign_in user
it "returns no users if state is not present" do
get api_users_path, params: {}
expect(response.status).to eq(404)
expect(response).to have_http_status(:success)
expect(json_response).to be_empty
end
end