From 9dafbe47dcf70690fdbd13ef1ba3169bc5f267b5 Mon Sep 17 00:00:00 2001 From: Ted Johansson Date: Fri, 18 Oct 2024 14:37:39 +0800 Subject: [PATCH] FIX: Don't error out on nested categories index page param (#29273) We're expecting the page param to be something that neatly coerces into an integer. If we receive something like a nested parameter, this will blow up. (I'm sure there are other examples as well.) This commit falls back to a page value of 1 if the coercion fails. --- app/controllers/categories_controller.rb | 2 +- spec/requests/categories_controller_spec.rb | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb index 9c6d5279c0c..558fc168da6 100644 --- a/app/controllers/categories_controller.rb +++ b/app/controllers/categories_controller.rb @@ -620,7 +620,7 @@ class CategoriesController < ApplicationController include_topics: include_topics, include_subcategories: include_subcategories, tag: params[:tag], - page: params[:page].to_i, + page: params[:page].try(:to_i) || 1, } @category_list = CategoryList.new(guardian, category_options) diff --git a/spec/requests/categories_controller_spec.rb b/spec/requests/categories_controller_spec.rb index 3c29bf0dd29..d8fb02aac1a 100644 --- a/spec/requests/categories_controller_spec.rb +++ b/spec/requests/categories_controller_spec.rb @@ -425,6 +425,11 @@ RSpec.describe CategoriesController do expect(response.status).to eq(200) expect(response.parsed_body["category_list"]["categories"].count).to eq(0) end + + it "does not error out if page is a nested parameter" do + get "/categories.json?page[foo]=2" + expect(response.status).to eq(200) + end end end