Skip to content

Commit

Permalink
Merge pull request #4387 from sul-dlss/4386-blank-ids
Browse files Browse the repository at this point in the history
Guard against blank ids for availability lookups. Fixes #4386
  • Loading branch information
jcoyne authored Aug 20, 2024
2 parents 20e389a + ac82c55 commit 908e08d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/controllers/availability_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ def redirect_bots
end

def redirect_no_ids
render json: [] unless params[:ids].present?
render json: [] unless Array(params[:ids]).compact_blank.present?
end
end
2 changes: 1 addition & 1 deletion app/services/live_lookup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class LiveLookup
delegate :as_json, :to_json, to: :records

def initialize(ids)
@ids = [ids].flatten.compact
@ids = Array(ids).compact_blank
end

# Uses the LiveLookup service specified by Settings.live_lookup_service
Expand Down
8 changes: 8 additions & 0 deletions spec/controllers/availability_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
end
end

describe "with a blank ID" do
it "should render a blank JSON array w/o making a live lookup request" do
expect(LiveLookup).not_to receive(:new)
get :index, params: { ids: [''] }
expect(response.body).to eq '[]'
end
end

describe "with IDs" do
let(:lookup) { double('new') }
let(:json) { [{ a: 'a', b: 'b' }] }
Expand Down

0 comments on commit 908e08d

Please sign in to comment.