Skip to content
代码片段 群组 项目
提交 4b56a5d2 编辑于 作者: Vasilii Iakliushin's avatar Vasilii Iakliushin
浏览文件

Fix invalid X-Next-Page header for the first page

Contributes to https://gitlab.com/gitlab-org/gitlab/-/issues/471566

**Problem**

Code responsible for the pagination of the first page always assumes
that the second page exists. As a result, it sets a X-Next-Page even if
there is no second page.

**Solution**

Fix the calculation of `total_pages` and `next_page` values.

Changelog: fixed
上级 1be5677c
No related branches found
No related tags found
无相关合并请求
...@@ -70,10 +70,12 @@ def paginate_first_page_via_gitaly(finder) ...@@ -70,10 +70,12 @@ def paginate_first_page_via_gitaly(finder)
finder.execute(gitaly_pagination: true).tap do |records| finder.execute(gitaly_pagination: true).tap do |records|
total = finder.total total = finder.total
per_page = params[:per_page].presence || Kaminari.config.default_per_page per_page = params[:per_page].presence || Kaminari.config.default_per_page
total_pages = (total / per_page.to_f).ceil
next_page = total_pages > 1 ? 2 : nil
Gitlab::Pagination::OffsetHeaderBuilder.new( Gitlab::Pagination::OffsetHeaderBuilder.new(
request_context: request_context, per_page: per_page, page: 1, next_page: 2, request_context: request_context, per_page: per_page, page: 1, next_page: next_page,
total: total, total_pages: (total / per_page) + 1 total: total, total_pages: total_pages
).execute ).execute
end end
end end
......
...@@ -70,6 +70,11 @@ ...@@ -70,6 +70,11 @@
context 'when first page is requested' do context 'when first page is requested' do
let(:branches) { [branch1, branch2, branch3] } let(:branches) { [branch1, branch2, branch3] }
before do
allow(BranchesFinder).to receive(:===).with(finder).and_return(true)
allow(finder).to receive(:total).and_return(branches.size)
end
it 'keyset pagination is used with offset headers' do it 'keyset pagination is used with offset headers' do
allow(request_context).to receive(:request).and_return(fake_request) allow(request_context).to receive(:request).and_return(fake_request)
allow(project.repository).to receive(:branch_count).and_return(branches.size) allow(project.repository).to receive(:branch_count).and_return(branches.size)
...@@ -85,6 +90,26 @@ ...@@ -85,6 +90,26 @@
pager.paginate(finder) pager.paginate(finder)
end end
context 'when second page does not exist' do
let(:base_query) { { per_page: 3 } }
it 'does not set an invalid X-Next-Page header' do
allow(request_context).to receive(:request).and_return(fake_request)
allow(project.repository).to receive(:branch_count).and_return(branches.size)
expect(finder).to receive(:execute).and_return(branches)
expect(request_context).to receive(:header).with('X-Per-Page', '3')
expect(request_context).to receive(:header).with('X-Page', '1')
expect(request_context).to receive(:header).with('X-Next-Page', '')
expect(request_context).to receive(:header).with('X-Prev-Page', '')
expect(request_context).to receive(:header).with('Link', kind_of(String))
expect(request_context).to receive(:header).with('X-Total', '3')
expect(request_context).to receive(:header).with('X-Total-Pages', '1')
pager.paginate(finder)
end
end
end end
context 'when second page is requested' do context 'when second page is requested' do
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册