Skip to content

[BUG FIX] Handles edge case where requested current_page > total_pages. #2399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Features:

Fixes:

- [#2399](https://github.com/rails-api/active_model_serializers/pull/2399) Handles edge case where requested current_page > total_pages (@f3z0)

Misc:

### [v0.10.12 (2020-12-10)](https://github.com/rails-api/active_model_serializers/compare/v0.10.11...v0.10.12)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,15 @@ def last_page_url

def prev_page_url
return nil if collection.current_page == FIRST_PAGE
if collection.current_page > collection.total_pages
return url_for_page(collection.total_pages)
end
url_for_page(collection.current_page - FIRST_PAGE)
end

def next_page_url
return nil if collection.total_pages == 0 || collection.current_page == collection.total_pages
return nil if collection.total_pages == 0 ||
collection.current_page >= collection.total_pages
url_for_page(collection.next_page)
end

Expand Down
31 changes: 31 additions & 0 deletions test/adapter/json_api/pagination_links_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ def last_page_links
}
end

def greater_than_last_page_links
{
links: {
self: "#{URI}?page%5Bnumber%5D=4&page%5Bsize%5D=2",
first: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
prev: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=2",
next: nil,
last: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=2"
}
}
end

def expected_response_when_unpaginatable
data
end
Expand Down Expand Up @@ -122,6 +134,13 @@ def expected_response_with_last_page_pagination_links
end
end

def expected_response_with_greater_than_last_page_pagination_links
{}.tap do |hash|
hash[:data] = []
hash.merge! greater_than_last_page_links
end
end

def expected_response_with_empty_collection_pagination_links
{}.tap do |hash|
hash[:data] = []
Expand All @@ -141,6 +160,18 @@ def test_pagination_links_using_will_paginate
assert_equal expected_response_with_pagination_links, adapter.serializable_hash
end

def test_pagination_links_invalid_current_page_using_kaminari
adapter = load_adapter(using_kaminari(4), mock_request)

assert_equal expected_response_with_greater_than_last_page_pagination_links, adapter.serializable_hash
end

def test_pagination_links_invalid_current_page_using_will_paginate
adapter = load_adapter(using_will_paginate(4), mock_request)

assert_equal expected_response_with_greater_than_last_page_pagination_links, adapter.serializable_hash
end

def test_pagination_links_with_additional_params
adapter = load_adapter(using_will_paginate, mock_request(test: 'test'))

Expand Down