Skip to content

refactor: parse list pagination params in one Pagination concern - #21

Open
dex-the-ai wants to merge 1 commit into
mainfrom
architecture-audit/ruby-couchbase-orm-quickstart-2026w47
Open

dex-the-ai wants to merge 1 commit into
mainfrom
architecture-audit/ruby-couchbase-orm-quickstart-2026w47

Conversation

@dex-the-ai

@dex-the-ai dex-the-ai commented Nov 19, 2026 •

Copy link
Copy Markdown

Summary

Three list endpoints each parsed limit/offset themselves and handed the raw query-string values to N1QL query_fns, which interpolate them into LIMIT #{values[1]} OFFSET #{values[2]}. This PR moves that parsing into one Pagination concern, which owns the defaults and makes sure only non-negative Integers reach the models.

 app/controllers/
+├── concerns/pagination.rb   # pagination_params -> [limit, offset] (Integers, defaults 10/0)
 └── api/v1/
     ├── airlines_controller.rb  # index, to_airport: include Pagination
     └── airports_controller.rb  # direct_connections: include Pagination
 def to_airport
-  limit  = params[:limit]  || 10
-  offset = params[:offset] || 0
+  limit, offset = pagination_params   # ArgumentError -> existing 400 rescue
   Airline.to_airport(key: [destination_airport, limit, offset])

Swagger already documents both parameters as type: integer, so the API now enforces its own documented contract.

Evidence

Tested live against a local Couchbase 8.0.3 with travel-sample, running rails s -e test in ruby:3.4.1:

Request Before (main) After
airlines/list?country=France&limit=2 200, 2 airlines 200, 2 airlines (same)
airlines/list?...&limit=abc 500: 3080: Ambiguous reference to field 'abc', with the full statement echoed back 400 limit must be a non-negative integer
airlines/to-airport?...&limit=-1 200 [] 400
...&limit=2 UNION SELECT RAW 'pwned' 500 parsing_failure, with the statement echoed back 400

bundle exec rspec spec/requests/api/v1:

  • main: 27 examples, 0 failures
  • this branch: 31 examples, 0 failures. The 4 new specs cover a non-integer limit on list, a negative offset on to-airport, the default limit/offset on to-airport, and limit: '10 OFFSET 0' on direct-connections.
  • this branch's specs with main's controllers: 3 failures, all from the new validation specs
  • this branch with PR chore(security): review dependency vulnerabilities #20's Gemfile (Rails 8.1.4.1): 31 examples, 0 failures. CI=true rake rswag:specs:swaggerize: 63 examples, 0 failures.
  • rubocop app/controllers spec/requests/api/v1: 48 offenses before, 47 after. The new file has none.

⚠️ CI on main will probably fail on this PR for a reason that predates it. A fresh bundle install on the current Rails 7.1 Gemfile resolves json 3.1.0, and ActiveSupport 7.1 calls JSON.generate(..., quirks_mode: true), which raises ArgumentError: unknown keyword: quirks_mode on every JSON render. That is why the scheduled CI has been red since 2026-05-22. The local runs above against main used an uncommitted gem 'json', '< 3'. Open PR #20 (Rails 8.1) fixes this, and this branch passes on top of it.

GitHub has also auto-disabled the Continuous Integration workflow (disabled_inactivity), so only CodeQL runs on this PR (it passed). The rspec evidence above comes from local runs.

Merge Danger

Door: two-way. It's controller-only with no data or schema changes, so a revert is enough to undo it.

Blast Radius: small. The only change clients see is that limit/offset values that aren't non-negative integers now get a 400 with a clear message, where before they got a 500 or an empty list. Valid and omitted values behave exactly as before.

Follow-ups, not in this PR:

  • GET /api/v1/airports/list computes page/per_page/offset and then never uses them, so it returns every airport.
  • The routes_spec POST test isn't idempotent. A run that dies mid-way leaves route_post behind, and the next run gets a 409.
  • The airline JSON formatting is duplicated between index and to_airport.

🤖 Generated with Claude Code

The airlines list, airlines to-airport and airports direct-connections
actions each read params[:limit]/params[:offset] with their own defaults
and passed the raw strings to N1QL query_fns that interpolate them into
LIMIT/OFFSET. Non-integer values were parsed as N1QL expressions (500
with the statement echoed back), and negative values returned [].

Pagination#pagination_params now owns the defaults (10/0) and returns
non-negative Integers, raising ArgumentError (rendered as 400 by the
existing rescues) otherwise.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dex-the-ai
dex-the-ai requested a review from shyam-cb November 19, 2026 03:53

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant