Sitelet https://github.com/googleapis/google-api-ruby-client/pull/28073/files
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions google-apis-core/lib/google/apis/core/base_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ def each
end
elsif items
# yield singular non-nil items (for genomics API)
item_count = item_count + 1
break if @max && item_count > @max
yield items
end
break if @max && item_count >= @max
Expand Down
18 changes: 18 additions & 0 deletions google-apis-core/spec/google/apis/core/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,24 @@
expect(service.fetch_all(max: 5) { |token| responses[token] } ).to contain_exactly('a', 'b', 'c', 'd', 'e')
end

it 'should allow limiting the number of singular items to fetch' do

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would a quick unit test for max: 0 be worth having here?

A zero max request is fringe for sure, but fetch_all(max: 0, items: :singular) appears to have yielded one item before this change and now yields none after it.

@torreypayne torreypayne Oct 2, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch

expect(service.fetch_all(max: 2, items: :singular) { |token| responses[token] } ).to contain_exactly('foo', 'bar')
end

it 'should stop fetching pages once the singular item limit is reached' do
expect do |b|
service.fetch_all(max: 2, items: :singular) do |token|
b.to_proc.call(token)
responses[token]
end.count
end.to yield_successive_args(nil, 'p1')
end

it 'should yield no items when max is zero' do
expect(service.fetch_all(max: 0) { |token| responses[token] }.to_a).to be_empty
expect(service.fetch_all(max: 0, items: :singular) { |token| responses[token] }.to_a).to be_empty
end

it 'should yield the next token' do
expect do |b|
service.fetch_all do |token|
Expand Down
Loading