Skip to content

Commit b36da55

Browse files
authored
fix(core): Prevent duplicated pagination when a response returns an empty string as the next page token
1 parent 64130fa commit b36da55

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

google-apis-core/lib/google/apis/core/base_service.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def each
8282
end
8383
break if @max && item_count >= @max
8484
next_page_token = @last_result.send(@response_page_token_field)
85-
break if next_page_token.nil? || next_page_token == page_token
85+
break if next_page_token.to_s.empty? || next_page_token == page_token
8686
page_token = next_page_token
8787
end
8888
end

google-apis-core/spec/google/apis/core/service_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,10 +355,27 @@
355355

356356
let(:items) { service.fetch_all { |token| responses[token] } }
357357

358+
let(:responses2) do
359+
data = {}
360+
data[nil] = OpenStruct.new(
361+
next_page_token: 'p1', alt_page_token: 'p2', items: ['a', 'b', 'c'], alt_items: [1, 2, 3], singular: 'foo', hash_: { 'foo' => 1, 'bar' => 2 })
362+
data['p1'] = OpenStruct.new(
363+
next_page_token: 'p2', items: ['d', 'e', 'f'], alt_items: [4, 5, 6], singular: 'bar', hash_: nil)
364+
data['p2'] = OpenStruct.new(
365+
next_page_token: '', alt_page_token: nil, items: ['g', 'h', 'i'], alt_items: [7, 8, 9], singular: 'baz', hash_: { 'baz' => 3 })
366+
data
367+
end
368+
369+
let(:items2) { service.fetch_all { |token| responses2[token] } }
370+
358371
it 'should fetch pages until next page token is nil' do
359372
expect(items).to contain_exactly('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i')
360373
end
361374

375+
it 'should fetch pages until next page token is empty' do
376+
expect(items2).to contain_exactly('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i')
377+
end
378+
362379
it 'should stop on repeated page token' do
363380
responses['p2'].next_page_token = 'p2'
364381
expect(items).to contain_exactly('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i')

0 commit comments

Comments
 (0)