FIX: Default to first page when page params is an array.

This commit is contained in:
Guo Xiang Tan 2016-02-25 11:32:16 +08:00
parent 15ce3b2f49
commit e8de80de98
3 changed files with 13 additions and 7 deletions

View File

@ -49,6 +49,10 @@ class TopicsController < ApplicationController
# existing installs.
return wordpress if params[:best].present?
# work around people somehow sending in arrays,
# arrays are not supported
params[:page] = params[:page].to_i rescue 1
opts = params.slice(:username_filters, :filter, :page, :post_number, :show_deleted)
username_filters = opts[:username_filters]
@ -65,7 +69,7 @@ class TopicsController < ApplicationController
raise Discourse::NotFound
end
page = params[:page].to_i
page = params[:page]
if (page < 0) || ((page - 1) * @topic_view.chunk_size > @topic_view.topic.highest_post_number)
raise Discourse::NotFound
end
@ -529,7 +533,7 @@ class TopicsController < ApplicationController
url << "/#{post_number}" if post_number.to_i > 0
url << ".json" if request.format.json?
page = params[:page].to_i
page = params[:page]
url << "?page=#{page}" if page != 0
redirect_to url, status: 301

View File

@ -43,10 +43,7 @@ class TopicView
self.instance_variable_set("@#{key}".to_sym, value)
end
# work around people somehow sending in arrays,
# arrays are not supported
@page = @page.to_i rescue 1
@page = 1 if @page.zero?
@page = 1 if (!@page || @page.zero?)
@chunk_size = options[:slow_platform] ? TopicView.slow_chunk_size : TopicView.chunk_size
@limit ||= @chunk_size
@ -183,7 +180,7 @@ class TopicView
return filter_posts_by_ids(opts[:post_ids]) if opts[:post_ids].present?
return filter_best(opts[:best], opts) if opts[:best].present?
filter_posts_paged(opts[:page].to_i)
filter_posts_paged(@page)
end
def primary_group_names

View File

@ -563,6 +563,11 @@ describe TopicsController do
expect(response).to redirect_to(topic.relative_url + "/42?page=123")
end
it 'does not accept page params as an array' do
xhr :get, :show, id: topic.slug, post_number: 42, page: [2]
expect(response).to redirect_to("#{topic.relative_url}/42?page=1")
end
it 'returns 404 when an invalid slug is given and no id' do
xhr :get, :show, id: 'nope-nope'
expect(response.status).to eq(404)