stas/jsonapi.rb

Example code producing division by 0 error

xhs345 opened this issue · 3 comments

Expected Behavior

No error when using the example code

Actual Behavior

Getting error FloatDomainError: Infinity when using example code for jsonapi_page_size

Steps to Reproduce the Problem

Include code

  def jsonapi_page_size(pagination_params)
    per_page = pagination_params[:size].to_f.to_i
    per_page = 30 if per_page > 30
    per_page
  end

and don't provide size in the params. Method jsonapi_pagination_params will return limit 0 and jsonapi_pagination_meta will divide by it's value of 0, causing the error.

Fixed it for me by updating the code to per_page = 30 if per_page > 30 || per_page === 0

Specifications

  • Version: 1.7.0
  • Ruby version: 2.5.5

Hey @xhs345, I've just ran into this issue.
If you are overriding page size with whatever comes in params, but there's no size in params, you are returning zero. You should fallback to whatever page size you want by default.
I'm not really sure if this is an issue, but surely would be nice for the library to handle it more nicely.

stas commented

Sorry folks, I'll take care of this in the next days! 🙈

Thanks a lot for reporting this!

stas commented

Thank you all, I updated the readme.