You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
LimitOffsetPagination not working over coreapi in v3.6.3 #5144
@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.
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.
@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.
called the url http://127.0.0.1:8000/v1/ with httpie
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/"
}
}
}
}
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.
@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.
@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.
@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!
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
@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.
What not documentation not says is that the pagination is deactivated by default because of PAGE_SIZE = None
in PageNumberPagination
defpaginate_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)
ifnotpage_size:
returnNone
...
@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.
@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.
Activity
matteius commentedon May 22, 2017
@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 L
imitOffsetPaginationworking 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
ListAnimalswhich is based on aModelSerializer, and relies on the settings defining the DEFAULT_PAGINATION_CLASS to berest_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)
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.
lovelydinosaur commentedon May 22, 2017
Thanks @matteius
khakulov commentedon May 22, 2017
@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/
http://127.0.0.1:8000/v1/with httpieThis ist wrong in djangorestframework==3.6.3, so:
http://127.0.0.1:8000/v1/againnow limit and offset are in schema description.
I will do a pull request with fixes later.
khakulov commentedon May 22, 2017
@matteius I created the pull request in your project https://github.com/brjadams/drf_sprint/pull/1 so you can test it youself.
If pagination class, include the schema generation
matteius commentedon May 22, 2017
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_CLASSshould 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.
xordoquy commentedon May 22, 2017
This commit is a merge commit and has no conflict, so it's likely not the commit you're looking for.
xordoquy commentedon May 22, 2017
Also note that the OP has pointed to #4998 which likely has the guilty commit
matteius commentedon May 22, 2017
@tomchristie Looks like this should be re-opened.
matteius commentedon May 22, 2017
@xordoquy That isn't the original commit however because they are changing
pagination.page_sizetogetattr(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 schemaget_pagination_fieldsit should not be contingent on the page_size variable, only if it has pagination.xordoquy commentedon May 22, 2017
@matteius this will just discard a bug for another one.
Proper fix would be:
page_size=None. #4998 was neededmatteius commentedon May 22, 2017
@xordoquy I disagree -- I think the original reporter of #4998 should be setting
'DEFAULT_PAGINATION_CLASS': Noneor should be setting aPAGE_SIZEattribute that isn't None. Otherwisepage_sizeis a valid argument and should be included.lovelydinosaur commentedon May 22, 2017
@matteius - Concur - I think we made the wrong assessment on #4998 in the first place.
The
page_sizeis valid, and should be present on the schema. As you say - if users then want to turnthat off fully, the right way is to set
'DEFAULT_PAGINATION_CLASS': NoneLet's roll that change back with #5161. Thanks!
khakulov commentedon May 22, 2017
For now we can check
api_settings.PAGE_SIZEinstead ofgetattr(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=NoneOK solution: change
getattr(pagination, 'page_size', None)toapi_settings.PAGE_SIZExordoquy commentedon May 22, 2017
@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.
khakulov commentedon May 22, 2017
What not documentation not says is that the pagination is deactivated by default because of
PAGE_SIZE = Nonein PageNumberPagination
xordoquy commentedon May 23, 2017
@khakulov one option would be to set the pagination to None by default though it should definitively go through the deprecation process.
matteius commentedon May 23, 2017
@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.
matteius commentedon May 23, 2017
@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_SIZEsetting 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.