diff --git a/lib/google/apis/core/base_service.rb b/lib/google/apis/core/base_service.rb index d4deaf63a..9065a389a 100644 --- a/lib/google/apis/core/base_service.rb +++ b/lib/google/apis/core/base_service.rb @@ -63,11 +63,12 @@ module Google item_count = 0 loop do @last_result = @fetch_proc.call(page_token) - for item in @last_result.send(@items_field) + items = @last_result.send(@items_field) + for item in items item_count = item_count + 1 break if @max && item_count > @max yield item - end + end unless items.nil? break if @max && item_count >= @max break if @last_result.next_page_token.nil? || @last_result.next_page_token == page_token page_token = @last_result.next_page_token diff --git a/spec/google/apis/core/service_spec.rb b/spec/google/apis/core/service_spec.rb index a8935e143..ae5fa6d44 100644 --- a/spec/google/apis/core/service_spec.rb +++ b/spec/google/apis/core/service_spec.rb @@ -265,6 +265,11 @@ EOF expect(items).to contain_exactly('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i') end + it 'should ignore nil collections' do + responses['p1'].items = nil + expect(items).to contain_exactly('a', 'b', 'c', 'g', 'h', 'i') + end + it 'should allow selecting another field for items' do expect(service.fetch_all(items: :alt_items) { |token| responses[token] } ).to contain_exactly(1, 2, 3, 4, 5, 6, 7, 8, 9) end @@ -307,7 +312,6 @@ EOF expect(count).to eq 6 end - end end end