From 2d6df70deaf2b80e351854f00dc611ec432d17eb Mon Sep 17 00:00:00 2001 From: rhymes Date: Tue, 24 Dec 2019 19:08:10 +0100 Subject: [PATCH] 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 --- app/controllers/api/v0/users_controller.rb | 3 ++- spec/requests/api/v0/users_spec.rb | 7 +++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/v0/users_controller.rb b/app/controllers/api/v0/users_controller.rb index cf12ec69a..2c6406f14 100644 --- a/app/controllers/api/v0/users_controller.rb +++ b/app/controllers/api/v0/users_controller.rb @@ -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 diff --git a/spec/requests/api/v0/users_spec.rb b/spec/requests/api/v0/users_spec.rb index a4c22854f..8971e1f10 100644 --- a/spec/requests/api/v0/users_spec.rb +++ b/spec/requests/api/v0/users_spec.rb @@ -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