Merge pull request #7260 from opf/fix/30103/magical-max-size-limit

[30103] Remove the internal setting with 500 max entries
pull/7264/head
ulferts 6 years ago committed by GitHub
commit 433ef3ff0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      app/models/setting.rb
  2. 2
      app/services/api/v3/work_package_collection_from_query_service.rb
  3. 9
      lib/api/decorators/offset_paginated_collection.rb
  4. 28
      lib/api/utilities/page_size_helper.rb
  5. 2
      lib/api/v3/groups/groups_api.rb
  6. 2
      lib/api/v3/news/news_api.rb
  7. 2
      lib/api/v3/principals/principals_api.rb
  8. 2
      lib/api/v3/time_entries/time_entries_api.rb
  9. 2
      lib/api/v3/users/users_api.rb
  10. 2
      lib/api/v3/work_packages/watchers_api.rb
  11. 2
      modules/documents/lib/api/v3/documents/documents_api.rb
  12. 2
      modules/grids/app/controllers/api/v3/grids/grids_api.rb
  13. 56
      spec/lib/api/utilities/page_size_helper.rb

@ -225,7 +225,11 @@ class Setting < ActiveRecord::Base
# Helper that returns an array based on per_page_options setting
def self.per_page_options_array
per_page_options.split(%r{[\s,]}).map(&:to_i).select { |n| n > 0 }.sort
per_page_options
.split(%r{[\s,]})
.map(&:to_i)
.select(&:positive?)
.sort
end
def self.clear_cache(key = cache_key)

@ -30,7 +30,7 @@ module API
module V3
class WorkPackageCollectionFromQueryService
include Utilities::PathHelper
include ::API::Utilities::ParamsHelper
include ::API::Utilities::PageSizeHelper
def initialize(query, user)
self.query = query

@ -30,20 +30,17 @@
module API
module Decorators
class OffsetPaginatedCollection < ::API::Decorators::Collection
include ::API::Utilities::PageSizeHelper
def self.per_page_default(relation)
relation.base_class.per_page
end
def self.per_page_maximum
Setting.api_max_page_size.to_i
end
def initialize(models, self_link, query: {}, page: nil, per_page: nil, current_user:)
@self_link_base = self_link
@query = query
@page = page || 1
@per_page = [per_page || self.class.per_page_default(models),
self.class.per_page_maximum].min
@per_page = [per_page || self.class.per_page_default(models), maximum_page_size].min
full_self_link = make_page_link(page: @page, page_size: @per_page)
paged = paged_models(models)

@ -30,16 +30,38 @@
module API
module Utilities
module ParamsHelper
module PageSizeHelper
# Set a default max size to ensure backwards compatibility
# with the previous private setting `maximum_page_size`.
# The actual value is taken from
# max(Setting.per_page_options)
DEFAULT_API_MAX_SIZE ||= 500
##
# Determine set page_size from string
def resolve_page_size(string)
resolved_value = to_i_or_nil(string)
# a page size of 0 is a magic number for the maximum page size value
if resolved_value == 0 || resolved_value.to_i > Setting.api_max_page_size.to_i
resolved_value = Setting.api_max_page_size.to_i
if resolved_value == 0 || resolved_value.to_i > maximum_page_size
resolved_value = maximum_page_size
end
resolved_value
end
##
# Get the maximum allowed page size from
# the largest option of per_page size,
# or the magic fallback value 500.
def maximum_page_size
[
DEFAULT_API_MAX_SIZE,
Setting.per_page_options_array.max
].max
end
private
def to_i_or_nil(string)
string ? string.to_i : nil
end

@ -30,7 +30,7 @@ module API
module V3
module Groups
class GroupsAPI < ::API::OpenProjectAPI
helpers ::API::Utilities::ParamsHelper
helpers ::API::Utilities::PageSizeHelper
resources :groups do
params do

@ -30,7 +30,7 @@ module API
module V3
module News
class NewsAPI < ::API::OpenProjectAPI
helpers ::API::Utilities::ParamsHelper
helpers ::API::Utilities::PageSizeHelper
resources :news do
get do

@ -30,7 +30,7 @@ module API
module V3
module Principals
class PrincipalsAPI < ::API::OpenProjectAPI
helpers ::API::Utilities::ParamsHelper
helpers ::API::Utilities::PageSizeHelper
resource :principals do
get do

@ -30,7 +30,7 @@ module API
module V3
module TimeEntries
class TimeEntriesAPI < ::API::OpenProjectAPI
helpers ::API::Utilities::ParamsHelper
helpers ::API::Utilities::PageSizeHelper
resources :time_entries do
get do

@ -33,7 +33,7 @@ module API
module V3
module Users
class UsersAPI < ::API::OpenProjectAPI
helpers ::API::Utilities::ParamsHelper
helpers ::API::Utilities::PageSizeHelper
helpers do
def user_transition(allowed)

@ -32,7 +32,7 @@ module API
module V3
module WorkPackages
class WatchersAPI < ::API::OpenProjectAPI
helpers ::API::Utilities::ParamsHelper
helpers ::API::Utilities::PageSizeHelper
get '/available_watchers' do
authorize(:add_work_package_watchers, context: @work_package.project)

@ -33,7 +33,7 @@ module API
module V3
module Documents
class DocumentsAPI < ::API::OpenProjectAPI
helpers ::API::Utilities::ParamsHelper
helpers ::API::Utilities::PageSizeHelper
resources :documents do
get do

@ -32,7 +32,7 @@ module API
class GridsAPI < ::API::OpenProjectAPI
resources :grids do
helpers do
include API::Utilities::ParamsHelper
include API::Utilities::PageSizeHelper
end
get do

@ -0,0 +1,56 @@
#-- copyright
# OpenProject is a project management system.
# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2017 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See docs/COPYRIGHT.rdoc for more details.
#++
require 'spec_helper'
describe ::API::Utilities::PageSizeHelper do
let(:clazz) do
Class.new do
include ::API::Utilities::PageSizeHelper
end
end
let(:subject) { clazz.new }
describe '#maximum_page_size' do
context 'when small values in per_page_options',
with_settings: { per_page_options: '20,100' } do
it 'uses the magical number 500' do
expect(subject.maximum_page_size).to eq(500)
end
end
context 'when larger values in per_page_options',
with_settings: { per_page_options: '20,100,1000' } do
it 'uses that value' do
expect(subject.maximum_page_size).to eq(1000)
end
end
end
end
Loading…
Cancel
Save