Skip to content

LimitOffsetPagination not working over coreapi in v3.6.3 #5144

Closed

Description

@khakulov

Steps to reproduce

following coreapi call to my book resource with LimitOffsetPagination

client.action(document, ['book', 'list'], params={
    'offset': 0,
    'limit': 20,
})

Expected behavior

in v3.6.2 works well:

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

Actual behavior

in v3.6.3 it causes an exception:

ParameterError: {'offset': 'Unknown parameter.', 'limit': 'Unknown parameter.'}

related to pr #4998 which introduced this breaking change (bug).

Activity

  1. matteius commented on May 22, 2017

    @matteius
    Contributor

    @khakulov Could you please describe an example application that causes this issue for you on version 3.6.3? I am at the PyCon sprints and have setup this test project and was able to show LimitOffsetPagination working in both the versions you describe.

    In my example I am using the testpage app to create an API that is using a ListAPI view named ListAnimals which is based on a ModelSerializer, and relies on the settings defining the DEFAULT_PAGINATION_CLASS to be rest_framework.pagination.LimitOffsetPagination. There is a migration that adds 20 default dog Animals to use in a live example.

    For this example I made requests to:

    The first one showed the JSONRenderer working fine and the second showed BrowsableAPIRenderer working fine.

    Expected Output (the uuids will vary)

    // http://127.0.0.1:8000/v1/animals/?offset=0&limit=5&format=json
    
    {
      "count": 20,
      "next": "http://127.0.0.1:8000/v1/animals/?format=json&limit=5&offset=5",
      "previous": null,
      "results": [
        {
          "id": 1,
          "family": "Dog",
          "email_address": "[email protected]",
          "name": "Sparky 0",
          "secret_key": "869fa4d40aab465c92b5f2131e2a9e08"
        },
        {
          "id": 2,
          "family": "Dog",
          "email_address": "[email protected]",
          "name": "Sparky 1",
          "secret_key": "828f0d12aec6486d8e918e8b6da4caec"
        },
        {
          "id": 3,
          "family": "Dog",
          "email_address": "[email protected]",
          "name": "Sparky 2",
          "secret_key": "64960950957e4ad18354c45978427a9c"
        },
        {
          "id": 4,
          "family": "Dog",
          "email_address": "[email protected]",
          "name": "Sparky 3",
          "secret_key": "4998ba4002eb4bf3b080819cd5758d30"
        },
        {
          "id": 5,
          "family": "Dog",
          "email_address": "[email protected]",
          "name": "Sparky 4",
          "secret_key": "8c83a15f688b46b28818e592737e0a5b"
        }
      ]
    }
    

    Based on your bug report I would need more information to reproduce the bug you are claiming exists.
    Specifically helpful would be the classes from the View on down and the URL definition of the view that reproduces this problem.

  2. lovelydinosaur commented on May 22, 2017

    @lovelydinosaur
    Member

    Thanks @matteius

  3. khakulov commented on May 22, 2017

    @khakulov
    Author

    @matteius @tomchristie This bug is according to coreapi or coreschema. The view works, i can retrieve tha data. But LimitOffsetPagination does't delivering the required schema description.

    I forked your Example Project. And i put into your project coreapi support according to documentation: http://www.django-rest-framework.org/api-guide/schemas/

    1. Edit your urls.py, putted 3 Lines of code
    from django.conf.urls import url
    from django.views.decorators.cache import cache_control
    from rest_framework.schemas import get_schema_view  # coreapi support
    
    from .views import ListAnimals
    
    schema_view = get_schema_view(title='Animals API')  # coreapi support
    
    cached = cache_control(private=True, maxage=5 * 60 * 1)
    
    urlpatterns = [
        url('^$', schema_view),  # coreapi support
        url(r'^animals/$',
            ListAnimals.as_view(), name="list_animals"),
    ]
    1. installed coreapi
    pip install coreapi
    1. run the server
    python manage.py runserver
    
    1. called the url http://127.0.0.1:8000/v1/ with httpie
    khakulov@NETSYNO # http http://127.0.0.1:8000/v1/
    HTTP/1.0 200 OK
    Allow: GET, HEAD, OPTIONS
    Content-Length: 3391
    Content-Type: application/coreapi+json
    Date: Mon, 22 May 2017 21:53:51 GMT
    Server: WSGIServer/0.2 CPython/3.6.1
    Vary: Accept, Cookie
    X-Frame-Options: SAMEORIGIN
    
    {
        "_meta": {
            "title": "Animals API",
            "url": "http://127.0.0.1:8000/v1/"
        },
        "_type": "document",
        ...
        "v1": {
            "animals": {
                "list": {
                    "_type": "link",
                    "action": "get",
                    "description": "List Animals in the system with pagination.",
                    "url": "/v1/animals/"
                }
            }
        }
    }
    

    This ist wrong in djangorestframework==3.6.3, so:

    1. i installed djangorestframework==3.6.2
    pip install djangorestframework==3.6.2
    
    1. call http://127.0.0.1:8000/v1/ again
    khakulov@NETSYNO # http http://127.0.0.1:8000/v1/
    HTTP/1.0 200 OK
    Allow: GET, HEAD, OPTIONS
    Content-Length: 3391
    Content-Type: application/coreapi+json
    Date: Mon, 22 May 2017 21:53:51 GMT
    Server: WSGIServer/0.2 CPython/3.6.1
    Vary: Accept, Cookie
    X-Frame-Options: SAMEORIGIN
    
    {
        "_meta": {
            "title": "Animals API",
            "url": "http://127.0.0.1:8000/v1/"
        },
        "_type": "document",
        ...
        "v1": {
            "animals": {
                "list": {
                    "_type": "link",
                    "action": "get",
                    "description": "List Animals in the system with pagination.",
                    "fields": [
                        {
                            "location": "query",
                            "name": "limit",
                            "schema": {
                                "_type": "integer",
                                "description": "Number of results to return per page.",
                                "title": "Limit"
                            }
                        },
                        {
                            "location": "query",
                            "name": "offset",
                            "schema": {
                                "_type": "integer",
                                "description": "The initial index from which to return the results.",
                                "title": "Offset"
                            }
                        }
                    ],
                    "url": "/v1/animals/"
                }
            }
        }
    }
    

    now limit and offset are in schema description.

    I will do a pull request with fixes later.

  4. khakulov commented on May 22, 2017

    @khakulov
    Author
  5. matteius commented on May 22, 2017

    @matteius
    Contributor

    I have determined that the issue originated from this commit: 6ad0be4#diff-eae91da078f88bdb425b240be0ee5487

    The commit does not explain why page_size was added as a check, but we assume it had to do with checking for non-zero page sizes. We think this is incorrect because if you have a pagination class then the parameters are to be included in the schema otherwise the DEFAULT_PAGINATION_CLASS should be set to None.

    I have reverted this change in my PR and shown that all tests still pass so no new test was ever added to explain why this functionality changed.

  6. xordoquy commented on May 22, 2017

    @xordoquy
    Contributor

    This commit is a merge commit and has no conflict, so it's likely not the commit you're looking for.

  7. xordoquy commented on May 22, 2017

    @xordoquy
    Contributor

    Also note that the OP has pointed to #4998 which likely has the guilty commit

  8. matteius commented on May 22, 2017

    @matteius
    Contributor

    @tomchristie Looks like this should be re-opened.

  9. matteius commented on May 22, 2017

    @matteius
    Contributor

    @xordoquy That isn't the original commit however because they are changing pagination.page_size to getattr(pagination, 'page_size', None) but we see that in v3.6.2 it didn't check for page_size at all. My change is to go back to those days since for the pagination schema get_pagination_fields it should not be contingent on the page_size variable, only if it has pagination.

  10. xordoquy commented on May 22, 2017

    @xordoquy
    Contributor

    @matteius this will just discard a bug for another one.
    Proper fix would be:

  11. matteius commented on May 22, 2017

    @matteius
    Contributor

    @xordoquy I disagree -- I think the original reporter of #4998 should be setting 'DEFAULT_PAGINATION_CLASS': None or should be setting a PAGE_SIZE attribute that isn't None. Otherwise page_size is a valid argument and should be included.

  12. lovelydinosaur commented on May 22, 2017

    @lovelydinosaur
    Member

    @matteius - Concur - I think we made the wrong assessment on #4998 in the first place.
    The page_size is valid, and should be present on the schema. As you say - if users then want to turn
    that off fully, the right way is to set 'DEFAULT_PAGINATION_CLASS': None
    Let's roll that change back with #5161. Thanks!

  13. khakulov commented on May 22, 2017

    @khakulov
    Author

    For now we can check api_settings.PAGE_SIZE instead of getattr(pagination, 'page_size', None). Because every build in Pagination classes checks page_size for None.

    But it would not work with custom Pagination classes without using api_settings.PAGE_SIZE.

    Best solution: Set default settings DEFAULT_PAGINATION_CLASS=None
    OK solution: change getattr(pagination, 'page_size', None) to api_settings.PAGE_SIZE

  14. xordoquy commented on May 22, 2017

    @xordoquy
    Contributor

    @matteius fair enough. The documentation says the way to deactivate pagination is to set the class to None so let's go that way.
    I think we're missing a point but as far as the tests are still running without changes I'd consider we're good.

  15. khakulov commented on May 22, 2017

    @khakulov
    Author

    What not documentation not says is that the pagination is deactivated by default because of PAGE_SIZE = None

    in PageNumberPagination

        def paginate_queryset(self, queryset, request, view=None):
            """
            Paginate a queryset if required, either returning a
            page object, or `None` if pagination is not configured for this view.
            """
            page_size = self.get_page_size(request)
            if not page_size:
                return None
           ...
  16. xordoquy commented on May 23, 2017

    @xordoquy
    Contributor

    @khakulov one option would be to set the pagination to None by default though it should definitively go through the deprecation process.

  17. matteius commented on May 23, 2017

    @matteius
    Contributor

    @khakulov I believe that you have the side-effect of pagination being disabled by default except that you can pass it in as a query parameter and then the result set will be paginated. The Default behavior was that Pagination was enabled but that page size had to be specified by the client in order to be activated unless the project made it the default behavior by specifying PAGE_SIZE.

    To your point, to upgrade to whatever release will include this change is also breaking change as to say you could upgrade both the setting and the version at one time. The issue to revert the behavior is that the code change was already mutated once since this change due to an issue it caused, and this ticket also identifying the expected behavior of the schema changed.

    I think we can learn a few things from this and the most important is more tests. We have a lot of implicit functionality that may mutate and the test still pass. I will re-visit this and cut additional PRs for these but I encourage you to help me preserve the expected functionality with additional test cases. The other more immediate thing I will do is PR a documentation update to reflect this behavior. I hope this helps.

  18. matteius commented on May 23, 2017

    @matteius
    Contributor

    @khakulov I was working on the documentation changes and was re-testing my theory and had to admit that not having the page_size set results in no pagination.

    Edit: After sleeping on it this initial idea it felt weird because it tried to infer on the Pagination class if the result would be paginated. It kind of feels weird that the pagination class itself defaults to not being paginated with the PAGE_SIZE setting being None. That makes it complicated to know when to include pagination parameters in the schema if it has an effect, so the current code always includes the pagination parameters regardless if the result set is fully paginated.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions