Skip to content

Commit 8f38571

Browse files
authored
Merge pull request #2399 from hyli-inc/0-10-stable
Handle edge case where requested current_page > total_pages in JSONAPI
2 parents cc29721 + e2f48b5 commit 8f38571

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Features:
88

99
Fixes:
1010

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

1315
### [v0.10.12 (2020-12-10)](https://github.com/rails-api/active_model_serializers/compare/v0.10.11...v0.10.12)

lib/active_model_serializers/adapter/json_api/pagination_links.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,15 @@ def last_page_url
5555

5656
def prev_page_url
5757
return nil if collection.current_page == FIRST_PAGE
58+
if collection.current_page > collection.total_pages
59+
return url_for_page(collection.total_pages)
60+
end
5861
url_for_page(collection.current_page - FIRST_PAGE)
5962
end
6063

6164
def next_page_url
62-
return nil if collection.total_pages == 0 || collection.current_page == collection.total_pages
65+
return nil if collection.total_pages == 0 ||
66+
collection.current_page >= collection.total_pages
6367
url_for_page(collection.next_page)
6468
end
6569

test/adapter/json_api/pagination_links_test.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ def last_page_links
9090
}
9191
end
9292

93+
def greater_than_last_page_links
94+
{
95+
links: {
96+
self: "#{URI}?page%5Bnumber%5D=4&page%5Bsize%5D=2",
97+
first: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
98+
prev: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=2",
99+
next: nil,
100+
last: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=2"
101+
}
102+
}
103+
end
104+
93105
def expected_response_when_unpaginatable
94106
data
95107
end
@@ -122,6 +134,13 @@ def expected_response_with_last_page_pagination_links
122134
end
123135
end
124136

137+
def expected_response_with_greater_than_last_page_pagination_links
138+
{}.tap do |hash|
139+
hash[:data] = []
140+
hash.merge! greater_than_last_page_links
141+
end
142+
end
143+
125144
def expected_response_with_empty_collection_pagination_links
126145
{}.tap do |hash|
127146
hash[:data] = []
@@ -141,6 +160,18 @@ def test_pagination_links_using_will_paginate
141160
assert_equal expected_response_with_pagination_links, adapter.serializable_hash
142161
end
143162

163+
def test_pagination_links_invalid_current_page_using_kaminari
164+
adapter = load_adapter(using_kaminari(4), mock_request)
165+
166+
assert_equal expected_response_with_greater_than_last_page_pagination_links, adapter.serializable_hash
167+
end
168+
169+
def test_pagination_links_invalid_current_page_using_will_paginate
170+
adapter = load_adapter(using_will_paginate(4), mock_request)
171+
172+
assert_equal expected_response_with_greater_than_last_page_pagination_links, adapter.serializable_hash
173+
end
174+
144175
def test_pagination_links_with_additional_params
145176
adapter = load_adapter(using_will_paginate, mock_request(test: 'test'))
146177

0 commit comments

Comments
 (0)