Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b30704e

Browse files
committedNov 26, 2017
Prevent out of bounds pages from returning a next link.
Also makes sure the prev link is a real page. Self link is unmodified, it will return a link to the current page even if it doesn't exist. Seemed to best conform to JSON:API spec.
1 parent 4d7c245 commit b30704e

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Features:
1616
Fixes:
1717

1818
- [#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)
19+
- [#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)
1920
- [#2200](https://github.com/rails-api/active_model_serializers/pull/2200) Fix deserialization of polymorphic relationships. (@dennis95stumm)
2021

2122
Misc:

‎lib/active_model_serializers/adapter/json_api/pagination_links.rb

+5-2
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,14 @@ def last_page_url
5353

5454
def prev_page_url
5555
return nil if collection.current_page == FIRST_PAGE
56-
url_for_page(collection.current_page - FIRST_PAGE)
56+
57+
page_number = collection.current_page > collection.total_pages ? collection.total_pages : collection.current_page - FIRST_PAGE
58+
59+
url_for_page(page_number)
5760
end
5861

5962
def next_page_url
60-
return nil if collection.total_pages == 0 || collection.current_page == collection.total_pages
63+
return nil if collection.total_pages == 0 || collection.current_page >= collection.total_pages
6164
url_for_page(collection.next_page)
6265
end
6366

‎test/adapter/json_api/pagination_links_test.rb

+35
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,18 @@ def last_page_links
8888
}
8989
end
9090

91+
def out_of_bounds_page_links
92+
{
93+
links: {
94+
self: "#{URI}?page%5Bnumber%5D=4&page%5Bsize%5D=2",
95+
first: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
96+
prev: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=2",
97+
next: nil,
98+
last: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=2"
99+
}
100+
}
101+
end
102+
91103
def expected_response_when_unpaginatable
92104
data
93105
end
@@ -120,6 +132,13 @@ def expected_response_with_last_page_pagination_links
120132
end
121133
end
122134

135+
def expected_response_with_out_of_bounds_page_pagination_links
136+
{}.tap do |hash|
137+
hash[:data] = []
138+
hash.merge! out_of_bounds_page_links
139+
end
140+
end
141+
123142
def expected_response_with_empty_collection_pagination_links
124143
{}.tap do |hash|
125144
hash[:data] = []
@@ -174,6 +193,22 @@ def test_last_page_pagination_links_using_will_paginate
174193
assert_equal expected_response_with_last_page_pagination_links, adapter.serializable_hash
175194
end
176195

196+
def test_out_of_bounds_page_pagination_links_using_kaminari
197+
out_of_bounds_page_number = 4
198+
199+
adapter = load_adapter(using_kaminari(out_of_bounds_page_number), mock_request)
200+
201+
assert_equal expected_response_with_out_of_bounds_page_pagination_links, adapter.serializable_hash
202+
end
203+
204+
def test_out_of_bounds_page_pagination_links_using_will_paginate
205+
out_of_bounds_page_number = 4
206+
207+
adapter = load_adapter(using_will_paginate(out_of_bounds_page_number), mock_request)
208+
209+
assert_equal expected_response_with_out_of_bounds_page_pagination_links, adapter.serializable_hash
210+
end
211+
177212
def test_not_showing_pagination_links
178213
adapter = load_adapter(@array, mock_request)
179214

0 commit comments

Comments
 (0)
Please sign in to comment.