Commit db09fc36 authored by Jarka Kadlecová's avatar Jarka Kadlecová

Return only limited pagination headers for search API endpoints

parent 6f9193b9
...@@ -43,7 +43,7 @@ class SearchService ...@@ -43,7 +43,7 @@ class SearchService
end end
def search_objects def search_objects
@search_objects ||= search_results.objects(scope, params[:page], params[:without_counts]) @search_objects ||= search_results.objects(scope, params[:page])
end end
private private
......
...@@ -12,13 +12,16 @@ module API ...@@ -12,13 +12,16 @@ module API
private private
def add_pagination_headers(paginated_data) def add_pagination_headers(paginated_data)
header 'X-Total', paginated_data.total_count.to_s
header 'X-Total-Pages', total_pages(paginated_data).to_s
header 'X-Per-Page', paginated_data.limit_value.to_s header 'X-Per-Page', paginated_data.limit_value.to_s
header 'X-Page', paginated_data.current_page.to_s header 'X-Page', paginated_data.current_page.to_s
header 'X-Next-Page', paginated_data.next_page.to_s header 'X-Next-Page', paginated_data.next_page.to_s
header 'X-Prev-Page', paginated_data.prev_page.to_s header 'X-Prev-Page', paginated_data.prev_page.to_s
header 'Link', pagination_links(paginated_data) header 'Link', pagination_links(paginated_data)
return if data_without_counts?(paginated_data)
header 'X-Total', paginated_data.total_count.to_s
header 'X-Total-Pages', total_pages(paginated_data).to_s
end end
def pagination_links(paginated_data) def pagination_links(paginated_data)
...@@ -37,8 +40,10 @@ module API ...@@ -37,8 +40,10 @@ module API
request_params[:page] = 1 request_params[:page] = 1
links << %(<#{request_url}?#{request_params.to_query}>; rel="first") links << %(<#{request_url}?#{request_params.to_query}>; rel="first")
unless data_without_counts?(paginated_data)
request_params[:page] = total_pages(paginated_data) request_params[:page] = total_pages(paginated_data)
links << %(<#{request_url}?#{request_params.to_query}>; rel="last") links << %(<#{request_url}?#{request_params.to_query}>; rel="last")
end
links.join(', ') links.join(', ')
end end
...@@ -55,6 +60,10 @@ module API ...@@ -55,6 +60,10 @@ module API
relation relation
end end
def data_without_counts?(paginated_data)
paginated_data.is_a?(Kaminari::PaginatableWithoutCount)
end
end end
end end
end end
...@@ -24,8 +24,7 @@ module API ...@@ -24,8 +24,7 @@ module API
search: params[:search], search: params[:search],
snippets: snippets?, snippets: snippets?,
page: params[:page], page: params[:page],
per_page: params[:per_page], per_page: params[:per_page]
without_counts: false
}.merge(additional_params) }.merge(additional_params)
results = SearchService.new(current_user, search_params).search_objects results = SearchService.new(current_user, search_params).search_objects
......
...@@ -10,7 +10,7 @@ module Gitlab ...@@ -10,7 +10,7 @@ module Gitlab
@per_page = per_page @per_page = per_page
end end
def objects(scope, page = nil, without_counts = true) def objects(scope, page = nil)
case scope case scope
when 'notes' when 'notes'
notes.page(page).per(per_page) notes.page(page).per(per_page)
...@@ -21,7 +21,7 @@ module Gitlab ...@@ -21,7 +21,7 @@ module Gitlab
when 'commits' when 'commits'
Kaminari.paginate_array(commits).page(page).per(per_page) Kaminari.paginate_array(commits).page(page).per(per_page)
else else
super(scope, page, without_counts) super(scope, page, false)
end end
end end
......
...@@ -9,14 +9,14 @@ module Gitlab ...@@ -9,14 +9,14 @@ module Gitlab
@query = query @query = query
end end
def objects(scope, page = nil, without_counts = true) def objects(scope, page = nil)
case scope case scope
when 'snippet_titles' when 'snippet_titles'
snippet_titles.page(page).per(per_page) snippet_titles.page(page).per(per_page)
when 'snippet_blobs' when 'snippet_blobs'
snippet_blobs.page(page).per(per_page) snippet_blobs.page(page).per(per_page)
else else
super(scope, nil, without_counts) super(scope, nil, false)
end end
end end
......
...@@ -9,7 +9,7 @@ describe API::Search do ...@@ -9,7 +9,7 @@ describe API::Search do
shared_examples 'response is correct' do |schema:, size: 1| shared_examples 'response is correct' do |schema:, size: 1|
it { expect(response).to have_gitlab_http_status(200) } it { expect(response).to have_gitlab_http_status(200) }
it { expect(response).to match_response_schema(schema) } it { expect(response).to match_response_schema(schema) }
it { expect(response).to include_pagination_headers } it { expect(response).to include_limited_pagination_headers }
it { expect(json_response.size).to eq(size) } it { expect(json_response.size).to eq(size) }
end end
......
...@@ -3,3 +3,9 @@ RSpec::Matchers.define :include_pagination_headers do |expected| ...@@ -3,3 +3,9 @@ RSpec::Matchers.define :include_pagination_headers do |expected|
expect(actual.headers).to include('X-Total', 'X-Total-Pages', 'X-Per-Page', 'X-Page', 'X-Next-Page', 'X-Prev-Page', 'Link') expect(actual.headers).to include('X-Total', 'X-Total-Pages', 'X-Per-Page', 'X-Page', 'X-Next-Page', 'X-Prev-Page', 'Link')
end end
end end
RSpec::Matchers.define :include_limited_pagination_headers do |expected|
match do |actual|
expect(actual.headers).to include('X-Per-Page', 'X-Page', 'X-Next-Page', 'X-Prev-Page', 'Link')
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment