Pagination: Using $skip skips twice the specified number
- When using server-side pagination, "$skip" skips twice the data specified in the URL.*
Assemblies affected
*OData WebApi lib 6.1.0
Reproduce steps
- Enable server-side pagination by [EnableQuery(PageSize = 100)] for an endpoint for example ('foo')
- Send the URL ~/foo?$skip=5 (This skips 10 records instead of 5)
Expected result
It skips 10 records
Actual result
it should skip only 5 records
Additional detail
- $skip works fine if server side pagination is not enabled.
Can you show your controller codes? I am wondering maybe your method accepts a ODataQueryOption argument and decorates using [EnableQuery] attribute.
That is true. I am using > ODataQueryOption
[Authorize(Roles = "Admin, User")]
[EnableQuery(PageSize = 7200)]
public IQueryable<TestDTO> GetData(ODataQueryOptions opts)
{
var settings = new ODataValidationSettings
{
// Initialize settings as needed.
AllowedQueryOptions = AllowedQueryOptions.All
};
opts.Validate(settings);
//...
}
Is that wrong? If so, what is the alternative solution! Thanks in advance.
Basically, it's not recommended to use both at one method.
[EnableQuery] will apply the query options on data, no matter whether you do the query option at method body or not. So, if you apply $skip at your method body, [EnableQuery] will apply it again.
The final linq expression will have two .Skip(5) expression.
@xuzhg : Gotcha. So, what would be your recommendation? My method accepts ODataQueryOptions, as I want to check the filters being set by the URL and then apply some default filters. But I also don't want the records to be ever more than 7200 records. Is there another approach to this scenario.
We have a feature that I think, can meet your requirement. you can reference it at: http://odata.github.io/WebApi/#04-16-ignore-query-option
Closing as answered. Please re-open if you have further comments.
Yes. I was able to achieve the desired behavior with a few workarounds. But should not this be fixed! It does not make sense to apply Skip twice if we use [EnableQuery(PageSize=)] attribute and pass ODataQueryOptions opts. IMO, this is a bug.