ODataQueryOptions and friends don't handle query options case correctly
The casing of OData query options ($filter, $orderby, $select, $expand, $top, $skip, and $count) is handled inconsistently by ODataQueryOptions. For example, if a request is sent with $Top in the query string instead of $top, the .Top property of ODataQueryOptions is initialized in a weird state where accessing its .Value property always throws InvalidOperationException. Similarly for .Filter; it always throws a NullReferenceException. Basically there is no way for an OData service to detect this case without catching these exceptions, which is very hacky.
A much better behavior would be to ignore such misspelled options and behave as if they do not exist.
Here is a full repro based on Microsoft.AspNet.OData 5.6.0:
using System;
using System.ComponentModel.DataAnnotations;
using System.Net.Http;
using System.Web.OData;
using System.Web.OData.Builder;
using System.Web.OData.Query;
using System.Web.OData.Routing;
using Microsoft.OData.Edm;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ODataMixedCaseRepro
{
[TestClass]
public class ODataQueryOptionsTests
{
private ODataQueryOptions _emptyQueryOptions;
private ODataQueryOptions _invalidCaseQueryOptions;
[TestInitialize]
public void TestInitialize()
{
var modelBuilder = new ODataConventionModelBuilder();
modelBuilder.EntitySet<TestEntityType>("entities");
IEdmModel model = modelBuilder.GetEdmModel();
const string BaseRequestUri = "http://localhost/entities";
_emptyQueryOptions = CreateQueryOptions(model, BaseRequestUri);
string requestUri =
String.Format(
"{0}?$Filter={1}&$Select={1}&$Expand={1}&$orderBy={1}&$Top={1}&$Skip={1}&$Count={1}",
BaseRequestUri,
"ignore me");
_invalidCaseQueryOptions = CreateQueryOptions(model, requestUri);
}
[TestMethod]
public void ExpectNoFilter()
{
Assert.IsNull(_emptyQueryOptions.Filter, "No query options");
Assert.IsNull(_invalidCaseQueryOptions.Filter, "Invalid case query options");
}
[TestMethod]
public void ExpectNoOrderBy()
{
Assert.IsNull(_emptyQueryOptions.OrderBy, "No query options");
Assert.IsNull(_invalidCaseQueryOptions.OrderBy, "Invalid case query options");
}
[TestMethod]
public void ExpectNoSelectExpand()
{
Assert.IsNull(_emptyQueryOptions.SelectExpand, "No query options");
Assert.IsNull(_invalidCaseQueryOptions.SelectExpand, "Invalid case query options");
}
[TestMethod]
public void ExpectNoTop()
{
Assert.IsNull(_emptyQueryOptions.Top, "No query options");
Assert.IsNull(_invalidCaseQueryOptions.Top, "Invalid case query options");
}
[TestMethod]
public void ExpectNoSkip()
{
Assert.IsNull(_emptyQueryOptions.Skip, "No query options");
Assert.IsNull(_invalidCaseQueryOptions.Skip, "Invalid case query options");
}
[TestMethod]
public void ExpectNoCount()
{
Assert.IsNull(_emptyQueryOptions.Count, "No query options");
Assert.IsNull(_invalidCaseQueryOptions.Count, "Invalid case query options");
}
private static ODataQueryOptions CreateQueryOptions(IEdmModel model, string requestUri)
{
var request = new HttpRequestMessage(HttpMethod.Get, requestUri);
var path = new ODataPath(new EntitySetPathSegment("entities"));
var context = new ODataQueryContext(model, typeof(TestEntityType), path);
var queryOptions = new ODataQueryOptions(context, request);
return queryOptions;
}
private class TestEntityType
{
[Key]
public string Id { get; set; }
}
}
}
Currently, you can use the case-insensitive to make it work. See here. So, close it.
That is a workaround, not a solution. In my case, I do not want to accept case-insensitive anything... I just want to be able to handle it correctly when a customer sends a request with incorrect case.
Please re-open this issue until the actual bug is fixed.