WebApi icon indicating copy to clipboard operation
WebApi copied to clipboard

Pagination: Using $skip skips twice the specified number

Open mrtyormaa opened this issue 9 years ago • 7 comments

  • 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.

mrtyormaa avatar Nov 13 '17 17:11 mrtyormaa

Can you show your controller codes? I am wondering maybe your method accepts a ODataQueryOption argument and decorates using [EnableQuery] attribute.

xuzhg avatar Nov 13 '17 17:11 xuzhg

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.

mrtyormaa avatar Nov 13 '17 18:11 mrtyormaa

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 avatar Nov 13 '17 18:11 xuzhg

@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.

mrtyormaa avatar Nov 13 '17 18:11 mrtyormaa

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

xuzhg avatar Nov 13 '17 18:11 xuzhg

Closing as answered. Please re-open if you have further comments.

AlanWong-MS avatar Nov 15 '17 19:11 AlanWong-MS

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.

mrtyormaa avatar Nov 15 '17 21:11 mrtyormaa