WebAPI OData $Skip on custom IQueryable double applied
While reading through the Github issues list I came across this post: OData PageResult method ignoring count parameter when using EnableQuery attribute #159. What appears to be the problem is the combination of EnableQuery Attribute and the parameterized Get constructor taking the ODataQueryOptions. Using both means that you will implement the constructor query options, applying the query expressions, then the framework will apply what filters it can on direction from the applied attribute; therefore double applying things like skip, top and orderby.
Since I wanted to handle the skip myself but I still wanted the other features of the EnableQueryAttribute I had a look and noticed it was virtual. I created a derived class and then attempted to override the ApplyQuery methods. Unfortunately the ODataQueryOptions only had private sets on its properties so I ninja-ed in a little reflection. (It feels dirty but hey..)
public override IQueryable ApplyQuery(IQueryable queryable, ODataQueryOptions queryOptions)
{
var skipOption = new SkipQueryOption("0", queryOptions.Context);
typeof(ODataQueryOptions).GetProperty("Skip").SetValue(queryOptions, skipOption, null);
return base.ApplyQuery(queryable, queryOptions);
}
With the skip option now being 0 it doesn't apply it when constructing the response and no more "double skip blues".