Webapi: Exception while attempting to parse non-lowercase value in '$count' query option

Created on 21 Nov 2018  路  2Comments  路  Source: OData/WebApi

When making an OData request and passing the $count=true|false query option, query validation fails when the Boolean values are not 100% lowercase. This is inconsistent with C# Boolean parsing as well as with the default True|False values generated by bool.ToString() and bool.TrueString|FalseString.

Assemblies affected

Microsoft.AspNetCore.OData 7.1.0

Reproduce steps

  1. Create an API that supports the $count option
  2. Make a request specifying the $count value with an explicit boolean
    a. GET http://localhost/api/odata/productdetails?$count=true
    b. GET http://localhost/api/odata/productdetails?$count=True
    c. GET http://localhost/api/odata/productdetails?$count=TRUE

Expected result

All 3 requests should pass parsing and hit the controller with count set to true.

Actual result

Requests 2.a pass as expected.
Request 2.b, and 2.c fail with an exception

Microsoft.OData.ODataException: 'TRUE' is not a valid count option.
at Microsoft.OData.UriParser.ODataQueryOptionParser.ParseCount(String count)
at Microsoft.OData.UriParser.ODataQueryOptionParser.ParseCount()
at Microsoft.AspNet.OData.Query.CountQueryOption.get_Value()
...

Additional detail

This appears to have already been posted 2 years ago (!):
https://github.com/OData/odata.net/issues/718

I'll keep this in here and leave it up to you to decide which one is still considered valid. Notice that versions have since changed drastically (so I'd consider keeping this more recent one open).

I found this out while playing with the DataServiceContext .AddQueryOption method. This code generated the URL which failed on the server:

dataServiceQuery.AddQueryOption("$count", false);

For some context, I wanted to make sure that I had an explicit $count=false on my query since I'm handling the logic manually on the server using ODataQueryOptions<T>. AFAIK it is not possible to generate a $count=false using abstractions like IncludeTotalCount(), as it only adds the true version to the query and is not parameterized (I'll create another issue to discuss that particular point).

P2 bug

Most helpful comment

@julealgon these three cases should all be supported in case insensitive way.

All 2 comments

@julealgon these three cases should all be supported in case insensitive way.

Any update on this issue? I'm happy to put together a PR with a fix for this, but I'm not sure if the fix should be implemented in OData.WebApi or in the odata.net repository.

Odata.WebApi uses the core odata.net library to resolve the $count query option value (in the ODataQueryOptionParser class). It would be far easier to parse nonlowercase values there than attempt to deal with it in the OData.WebApi project.

Was this page helpful?
0 / 5 - 0 ratings