Skip to content

Commit

Permalink
Fixes #37587 - Reuse API parameter handling from base
Browse files Browse the repository at this point in the history
Foreman's API base controller provides a search_and_pagination parameter
group that has all the fields needed. Then it also provides some helpers
to parse these values properly.

This reuses those helpers. It also aligns on per_page=all handling
instead of a separate full_result=true. That parameter is now
deprecated.
  • Loading branch information
ekohl committed Aug 30, 2024
1 parent 29392cf commit f1815eb
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions app/controllers/katello/api/v2/api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,10 @@ class Api::V2::ApiController < ::Api::V2::BaseController
end

def_param_group :search do
param :search, String, :desc => N_("Search string")
param :page, :number, :desc => N_("Page number, starting at 1")
param :per_page, :number, :desc => N_("Number of results per page to return")
param :order, String, :desc => N_("Sort field and order, eg. 'id DESC'")
param :full_result, :bool, :desc => N_("Whether or not to show all results")
param :sort_by, String, :desc => N_("Field to sort the results on")
param :sort_order, String, :desc => N_("How to order the sorted results (e.g. ASC for ascending)")
param_group :search_and_pagination, ::Api::V2::BaseController
param :full_result, :bool, :desc => N_("DEPRECATED, use `per_page=all`"), deprecated: true
param :sort_by, String, :desc => N_("DEPRECATED, use `order`"), deprecated: true
param :sort_order, String, :desc => N_("DEPRECATED, use `order`"), deprecated: true
end

param :object_root, String, :desc => N_("root-node of single-resource responses (optional)")
Expand Down Expand Up @@ -61,12 +58,10 @@ def scoped_search(query, default_sort_by, default_sort_order, options = {})
resource = options[:resource_class] || resource_class
includes = options.fetch(:includes, [])
group = options.fetch(:group, nil)
params[:full_result] = true if options[:csv]
params[:per_page] = 'all' if options[:csv]
blank_query = resource.none

if params[:order]
(params[:sort_by], params[:sort_order]) = params[:order].split(' ')
else
unless params[:order]
params[:order] = "#{params[:sort_by]} #{params[:sort_order]}"
end

Expand Down Expand Up @@ -95,13 +90,11 @@ def scoped_search(query, default_sort_by, default_sort_order, options = {})
subtotal = dist_total
selectable = dist_total
end
if ::Foreman::Cast.to_bool(params[:full_result])
if params[:per_page] == 'all' || ::Foreman::Cast.to_bool(params[:full_result])
params[:per_page] = total
else
query = query.paginate(paginate_options)
end
page = params[:page] || 1
per_page = params[:per_page] || Setting[:entries_per_page]
query = (total.zero? || subtotal.zero?) ? blank_query : query

if options[:csv]
Expand All @@ -111,8 +104,8 @@ def scoped_search(query, default_sort_by, default_sort_order, options = {})
query: query,
subtotal: subtotal,
total: total,
page: page,
per_page: per_page,
page: metadata_page,
per_page: metadata_per_page,
selectable: selectable
)
end
Expand Down

0 comments on commit f1815eb

Please sign in to comment.