diff --git a/doc/api/index.md b/doc/api/index.md index cf14a9f405b9b0dc19fed1a2de7717672448b634..26447a2223d65eb3eda1a448f061c15be94263b5 100644 --- a/doc/api/index.md +++ b/doc/api/index.md @@ -453,12 +453,14 @@ Keyset-pagination allows for more efficient retrieval of pages and - in contrast to offset-based pagination - runtime is independent of the size of the collection. -This method is controlled by the following parameters: - -| Parameter | Description | -|--------------| ------------| -| `pagination` | `keyset` (to enable keyset pagination). | -| `per_page` | Number of items to list per page (default: `20`, max: `100`). | +This method is controlled by the following parameters. `order_by` and `sort` are both mandatory. + +| Parameter | Required | Description | +|--------------| ------------ | --------- | +| `pagination` | yes | `keyset` (to enable keyset pagination). | +| `per_page` | no | Number of items to list per page (default: `20`, max: `100`). | +| `order_by` | yes | Column by which to order by. | +| `sort` | yes | Sort order (`asc` or `desc`) | In the following example, we list 50 [projects](projects.md) per page, ordered by `id` ascending. diff --git a/ee/spec/requests/api/audit_events_spec.rb b/ee/spec/requests/api/audit_events_spec.rb index 022fe2964c1cf794f878257c5d3ca88674ac0c9f..89e492d72da1092dc56f440bec377ea524ad99d9 100644 --- a/ee/spec/requests/api/audit_events_spec.rb +++ b/ee/spec/requests/api/audit_events_spec.rb @@ -162,7 +162,7 @@ let_it_be(:audit_event_2) { create(:group_audit_event, entity_id: group.id) } it 'paginates the records correctly' do - get api("/groups/#{group.id}/audit_events", current_user), params: { pagination: 'keyset', per_page: 1, order_by: 'id' } + get api("/groups/#{group.id}/audit_events", current_user), params: { pagination: 'keyset', per_page: 1 } expect(response).to have_gitlab_http_status(:ok) records = json_response diff --git a/lib/gitlab/pagination/keyset/cursor_based_request_context.rb b/lib/gitlab/pagination/keyset/cursor_based_request_context.rb index e06d7e48ca3de9894b91cde49707eeff89871ea8..41b908463457caa92032aea18d41b3b208025262 100644 --- a/lib/gitlab/pagination/keyset/cursor_based_request_context.rb +++ b/lib/gitlab/pagination/keyset/cursor_based_request_context.rb @@ -5,6 +5,8 @@ module Pagination module Keyset class CursorBasedRequestContext DEFAULT_SORT_DIRECTION = :desc + DEFAULT_SORT_COLUMN = :id + attr_reader :request_context delegate :params, to: :request_context @@ -28,7 +30,7 @@ def apply_headers(cursor_for_next_page) end def order_by - { params[:order_by].to_sym => params[:sort]&.to_sym || DEFAULT_SORT_DIRECTION } + { (params[:order_by]&.to_sym || DEFAULT_SORT_COLUMN) => (params[:sort]&.to_sym || DEFAULT_SORT_DIRECTION) } end end end