Skip to content

Prevent out of bounds pages from returning a next link. #2171

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

Open
wants to merge 1 commit into
base: 0-10-stable
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Features:
Fixes:

- [#2022](https://github.com/rails-api/active_model_serializers/pull/2022) Mutation of ActiveModelSerializers::Model now changes the attributes. Originally in [#1984](https://github.com/rails-api/active_model_serializers/pull/1984). (@bf4)
- [#2171](https://github.com/rails-api/active_model_serializers/pull/2171) Prevent the `next` link from getting populated in JSON:API when an out of bounds page is requested. (@bcaplan)
- [#2200](https://github.com/rails-api/active_model_serializers/pull/2200) Fix deserialization of polymorphic relationships. (@dennis95stumm)

Misc:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,14 @@ def last_page_url

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

page_number = collection.current_page > collection.total_pages ? collection.total_pages : collection.current_page - FIRST_PAGE

url_for_page(page_number)
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So no next page if the current page is the last page or out of bounds.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's correct. This way if you request out of bounds and rely on the next link, you don't paginate forever.

end

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

def out_of_bounds_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 @@ -120,6 +132,13 @@ def expected_response_with_last_page_pagination_links
end
end

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

def expected_response_with_empty_collection_pagination_links
{}.tap do |hash|
hash[:data] = []
Expand Down Expand Up @@ -174,6 +193,22 @@ def test_last_page_pagination_links_using_will_paginate
assert_equal expected_response_with_last_page_pagination_links, adapter.serializable_hash
end

def test_out_of_bounds_page_pagination_links_using_kaminari
out_of_bounds_page_number = 4

adapter = load_adapter(using_kaminari(out_of_bounds_page_number), mock_request)

assert_equal expected_response_with_out_of_bounds_page_pagination_links, adapter.serializable_hash
end

def test_out_of_bounds_page_pagination_links_using_will_paginate
out_of_bounds_page_number = 4

adapter = load_adapter(using_will_paginate(out_of_bounds_page_number), mock_request)

assert_equal expected_response_with_out_of_bounds_page_pagination_links, adapter.serializable_hash
end

def test_not_showing_pagination_links
adapter = load_adapter(@array, mock_request)

Expand Down