Early validation of query
Currently if you use EnableQueryAttribute on an ODataController action, there's no way (besides reflection) to perform early validation on the query, using the same settings supplied in the attribute.
All the information needed to validate a query is in the request url, and there's no reason to let the action hit the database if the request is going to fail because of an invalid query.
Example:
[EnableQuery(MaxExpansionDepth = 2, PageSize = 1000, AllowedQueryOptions = AllowedQueryOptions.Expand | AllowedQueryOptions.Filter)]
public IQueryable<Product> GetFeed()
{
return ProductRepository.GetProducts();
}
I would like two options to be available.
Option 1
Extra argument to EnableQueryAttribute that tells it to validate in OnActionExecuting instead of in OnActionExecuted.
[EnableQuery(EarlyValidation = true, MaxExpansionDepth = 2, PageSize = 1000, AllowedQueryOptions = AllowedQueryOptions.Expand | AllowedQueryOptions.Filter)]
public IQueryable<Product> GetFeed()
{
return ProductRepository.GetProducts();
}
This is fine, when all I want is to fail an invalid query before hitting the DB, and don't mind the action not being executed.
Option 2
Get access to the same ODataValidationSettings object that the EnableQueryAttribute uses for validation, from inside the action body. So that I can use that to validate manually.
[EnableQuery(MaxExpansionDepth = 2, PageSize = 1000, AllowedQueryOptions = AllowedQueryOptions.Expand | AllowedQueryOptions.Filter)]
public IQueryable<Product> GetFeed(ODataQueryOptions<Product> queryOptions)
{
// some essential code here
queryOptions.Validate(Request.ODataProperties().ValidationSettings)
return ProductRepository.GetProducts();
}
That way, it is possible to perform early validation, while ensuring that the action is always executed. And without having to create a ValidationSettings object identical to the one EnableQueryAttribute uses.
Note: Request.ODataProperties() is probably not the best for this feature. I just used it as an example.
With Option 2, you can new an ODataQuerySettings with the settings, validate, and then call ApplyTo.
public PageResult<Product> Get(ODataQueryOptions<Product> options)
{
ODataQuerySettings settings = new ODataQuerySettings()
{
PageSize = 5
};
IQueryable results = options.ApplyTo(_products.AsQueryable(), settings);
return new PageResult<Product>(
results as IEnumerable<Product>,
Request.GetNextPageLink(),
Request.GetInlineCount());
}
That approach means I have to supply each setting twice, once in the the EnableQuery attribute, and once when newing up an ODataQuerySettings object. That is not ideal, as it would lead to maintainance problems down the road, when some of the settings has to be changed.
Besides, I'm not interested in applying the OData queryoptions early, just in validating the request, and failing invalid requests, before hitting the DB. Assuming the following controller:
[EnableQuery(MaxExpansionDepth = 2, PageSize = 1000, AllowedQueryOptions = AllowedQueryOptions.Expand | AllowedQueryOptions.Filter)]
public IQueryable<Product> GetFeed()
{
return ProductRepository.GetProducts();
}
F.ex.: A request to http://example.com/odata/Produtcs?$orderby=Price is not valid, because $orderby is not an allowed query option. And since this information is available before hitting the DB, we might aswell fail the request, before putting any load on it.
@MikalJ Would you like to submit a pull request for the change? Thanks.
@MikalJ I'm very excited to tell you we have a new feature to handle this scenario in the 6.0.0, night package are ready and document is here, please have a try, any feedback is very appreciate.
@MikalJ we believe the issue to be fixed in WebApi 6.0. If it is not, please re-open the issue.