active_model_serializers icon indicating copy to clipboard operation
active_model_serializers copied to clipboard

Prevent out of bounds pages from returning a next link.

Open bcaplan opened this issue 7 years ago • 10 comments

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 avatar Aug 02 '17 21:08 bcaplan

@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.

bcaplan avatar Oct 11 '17 18:10 bcaplan

@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.

bcaplan avatar Nov 20 '17 21:11 bcaplan

@bcaplan Not sure how I might have closed it

bf4 avatar Nov 20 '17 21:11 bf4

@bf4 Haha, no worries. Thanks for reopening.

bcaplan avatar Nov 20 '17 22:11 bcaplan

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

bf4 avatar Nov 26 '17 17:11 bf4

@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?

bcaplan avatar Nov 26 '17 22:11 bcaplan

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.

bf4 avatar Nov 26 '17 23:11 bf4

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 avatar Nov 26 '17 23:11 bcaplan

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

jkburges avatar Sep 12 '18 05:09 jkburges

This appears to have been addressed by this newer PR from December 2020: https://github.com/rails-api/active_model_serializers/pull/2399

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

bcaplan avatar Apr 05 '21 23:04 bcaplan