Skip to content

Self-hosted project management for devs and their clients: tickets, time tracking, docs and meetings, with an AI assistant that cites its sources. AGPL-3.0.

Read a blank page as unset and quote refused values only in part

Guido Boyke41ab9093acca
diff --git a/app/controllers/api/v1/people_controller.rb b/app/controllers/api/v1/people_controller.rb
index fcf5ba3..3593108 100644
--- a/app/controllers/api/v1/people_controller.rb
+++ b/app/controllers/api/v1/people_controller.rb
@@ -19,6 +19,9 @@ module Api
MAX_PER_PAGE = 100
# Far beyond any directory, so an absurd page number is refused rather than skipped to.
MAX_PAGE = 100_000
+ # How much of a refused value an error message quotes back: enough to recognise it, not
+ # a mirror for any amount of input.
+ QUOTED_LENGTH = 20
# A list parameter the endpoint cannot take (q, page, per_page).
class InvalidParameter < StandardError; end
@@ -76,7 +79,8 @@ module Api
end
# A whole number from 1 to max; absent or blank is the default. A refused value is quoted
- # back only when it is text: a list or a nested value would show Ruby's insides.
+ # back only when it is text, and only its start: a list or a nested value would show
+ # Ruby's insides.
def whole_number(name, default:, max:)
raw = params[name]
return default if raw.nil? || raw == ''
@@ -84,7 +88,7 @@ module Api
number = Integer(raw, 10) if raw.is_a?(String) && raw.match?(/\A\d+\z/)
return number if number&.between?(1, max)
- shown = raw.is_a?(String) ? " #{raw.inspect}" : ''
+ shown = raw.is_a?(String) ? " #{raw.truncate(QUOTED_LENGTH).inspect}" : ''
raise InvalidParameter, "invalid #{name}#{shown} (expected a whole number from 1 to #{max})"
end
end
diff --git a/spec/requests/api/v1/people_spec.rb b/spec/requests/api/v1/people_spec.rb
index e41c1cd..6874461 100644
--- a/spec/requests/api/v1/people_spec.rb
+++ b/spec/requests/api/v1/people_spec.rb
@@ -164,6 +164,23 @@ RSpec.describe 'API V1 people', :ci_guard, type: :request do
expect(body['meta']).to eq('page' => 2, 'per_page' => 1, 'total' => 3)
end
+ # A form or a client that sends the parameter empty means "not set".
+ it 'reads a blank page and per_page as the defaults' do
+ list(page: '', per_page: '')
+
+ expect(response).to have_http_status(:ok)
+ expect(body['meta']).to eq('page' => 1, 'per_page' => 50, 'total' => 3)
+ end
+
+ # The message names the refused value, but does not mirror any amount of input back.
+ it 'quotes a refused value only in part' do
+ list(per_page: '9' * 5000)
+
+ expect(response).to have_http_status(:bad_request)
+ expect(body['message']).to start_with('invalid per_page "999')
+ expect(body['message'].length).to be < 100
+ end
+
it 'answers a page past the end with no rows and the total' do
list(page: 2)