Skip to content
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

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

Open
wants to merge 1 commit into
base: 0-10-stable
Choose a base branch
from

Conversation

bcaplan
Copy link

@bcaplan bcaplan commented Aug 2, 2017

Purpose

In the JSON:API adapter, this will prevent next link URLs from being generated for nonexistent pages if the page requested is out of bounds.

Changes

Now checking current_page against total_pages using >= instead of ==.

This also makes sure the prev link is a real page.

The self link is unmodified, it will return a link to the current page even if it doesn't exist. Seemed to best conform to the JSON:API spec.

Caveats

Related GitHub issues

Additional helpful information

@bcaplan bcaplan force-pushed the fix-next-links-for-out-of-bounds-pages branch from 87e47c7 to de6c30c Compare August 2, 2017 21:45
@bcaplan bcaplan force-pushed the fix-next-links-for-out-of-bounds-pages branch from a748e56 to 36b7960 Compare October 11, 2017 17:54
@bcaplan
Copy link
Author

bcaplan commented Oct 11, 2017

@bf4 Is there anything more I can do to help get this merged? I think tests were failing on the original branch I forked which may have prevented this from getting looked at. This is now fixed since I just pulled the upstream changes.

@bf4 bf4 closed this Nov 19, 2017
@bcaplan
Copy link
Author

bcaplan commented Nov 20, 2017

@bf4 Would you be willing to provide any info on why this was closed and not merged? This was my first attempt at contributing to open source software, so the information would be much appreciated.

@bf4
Copy link
Member

bf4 commented Nov 20, 2017

@bcaplan Not sure how I might have closed it

@bf4 bf4 reopened this Nov 20, 2017
@bcaplan
Copy link
Author

bcaplan commented Nov 20, 2017

@bf4 Haha, no worries. Thanks for reopening.

@bf4
Copy link
Member

bf4 commented Nov 26, 2017

This looks fine to me. Does the spec comment on this scenario? I don't recall. hashtag 'lazy'

@bcaplan
Copy link
Author

bcaplan commented Nov 26, 2017

@bf4 I named them test_out_of_bounds_page_pagination_links_using_{serializer here}, but there isn't a specific comment outlining the reason for the test. I didn't see any other specs with comments outlining expected behavior. Is that something you'd like me to add?

@bf4
Copy link
Member

bf4 commented Nov 26, 2017

I didn't see any other specs with comments outlining expected behavior

Sorry, I meant http://jsonapi.org/format/#fetching-pagination

Keys MUST either be omitted or have a null value to indicate that a particular link is unavailable.

So, nothing about out of bounds.

Copy link
Member

@bf4 bf4 left a comment

Choose a reason for hiding this comment

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

Looks good

url_for_page(collection.total_pages)
else
url_for_page(collection.current_page - FIRST_PAGE)
end
Copy link
Member

@bf4 bf4 Nov 26, 2017

Choose a reason for hiding this comment

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

or

-          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)

So, prev is the last page if current page is 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.

Done.

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.

@@ -174,6 +193,18 @@ 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
adapter = load_adapter(using_kaminari(4), mock_request)
Copy link
Member

Choose a reason for hiding this comment

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

might be good to replace 4 with out_of_bounds_page_number = 4 here and below

Copy link
Author

Choose a reason for hiding this comment

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

Done.

@bcaplan
Copy link
Author

bcaplan commented Nov 26, 2017

Ah, I see what you meant now. Yeah, it doesn't mention this specific circumstance. I decided when out of bounds: omitting the next link, providing the last page as the previous link, and providing a current link even though you are out of bounds seemed to best conform to my reading of the spirit of the spec.

@bcaplan bcaplan force-pushed the fix-next-links-for-out-of-bounds-pages branch from 36b7960 to c951acc Compare November 26, 2017 23:27
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.
@bcaplan bcaplan force-pushed the fix-next-links-for-out-of-bounds-pages branch from c951acc to b30704e Compare November 26, 2017 23:29
@jkburges
Copy link

It'd be good to see this merged. Is there anything I can do to help move things along?

@bcaplan
Copy link
Author

bcaplan commented Apr 5, 2021

This appears to have been addressed by this newer PR from December 2020: #2399

A little disappointed #2171 was never merged, but glad it's fixed nonetheless.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants