Skip to content

LimitOffset pagination crashes Browseable API when limit=0 #4079

Description

@awbacker

We are using LimitOffsetPagination on all our endpoints, and noticed that sending limit=0 to the browsable api causes a crash. Getting json output (either from an xmlhttprequest or from format=json) does not crash.

Steps to reproduce

  1. Install LimitOffsetPagination as the default pagination
  2. Open a list (ours are ModelViewSet) in the browser
  3. Set the query string to ?limit=0&offset=20
  4. boom

Notes
We use a custom LimitOffset pagination, which just overrides the max and default limit
Using DRF 3.3.2

Expected behavior

Should show the browsable api for the endpoint, with the following data:

{
    "count":148,
    "next":"https://api.co/page/?limit=0&offset=20",
    "previous":"https://api.co/page/?limit=0&offset=20",
    "results":[]
}

Actual behavior

Exception Type: ZeroDivisionError
Exception Value:    
integer division or modulo by zero
Exception Location: .../venv/local/lib/python2.7/site-packages/rest_framework/pagination.py in _divide_with_ceil, line 41
Python Executable:  /usr/local/bin/uwsgi

.../venv/local/lib/python2.7/site-packages/rest_framework/pagination.py in get_html_context
    current = _divide_with_ceil(self.offset, self.limit) + 1 ...

    ▼ Local vars
    self      <mysite.LargePageination object at 0x7f614e677bd0>
    base_url  'https://api.co/page/?limit=0&offset=20'

.../venv/local/lib/python2.7/site-packages/rest_framework/pagination.py in _divide_with_ceil
    if a % b: ...

    ▼ Local vars
    a       20
    b       0

Activity

  1. anx-ckreuzberger commented on Apr 28, 2016

    @anx-ckreuzberger
    Contributor

    I am able to reproduce this behaviour. Also this is not really a crash, it just displays you an error message as you have Debug Mode set to True in your Django Project.

    Though I guess there is no harm done in catching this and just returning an empty result.

  2. lovelydinosaur commented on Apr 28, 2016

    @lovelydinosaur
    Member

    Thanks for confirming @anx-ckreuzberger!
    Agree - this ought to be resolved.

  3. awbacker commented on Apr 29, 2016

    @awbacker
    Author

    Thanks for confirming too. It's nice to know I'm not crazy (though I did try it on a fresh project). Not sure how it's not a crash, it's a div by 0. The stack is well protected enough that this sends back a 500, so I guess it's not a core dump ;) Well, no core dump, just an uncaught exception popping up in Sentry.

  4. added this to the milestone on May 10, 2016
  5. modified the milestones: , 3.4.0 Release on Jun 1, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions