"collection.Take(1)" results in a bad URI: "Collection/$count?$top=1" but "collection.Take(1).ToArray()" results in a good URI: "Collection?$top=1"
======EDIT======: I was in fact calling .Count() afterward in the first example, however it is still generating an invalid URI ending with "Collection/$count?$top=1" which is not a valid OData URI, so the bug is that this should be caught and handled properly on the client side.
The following results in a badly generated query URI string: var result = odataClient.MyCollection.Take(1); // BOO! -> http://localhost/odata/MyCollection/$count?$top=1 // note the "/" added after the collection name, and then an option is added before the "?"
The following results in a good generated query URI string: var result = odataClient.MyCollection.Take(1).ToArray(); // YAY! -> http://localhost/odata/MyCollection?$top=1
The bad version of the URI results in the following response (not that it really matters as this is a client bug, but I do in fact have the count query option enabled). { "error":{ "code":"","message":"The query specified in the URI is not valid. Query option 'Count' is not allowed. To allow it, set the 'AllowedQueryOptions' property on EnableQueryAttribute or QueryValidationSettings.","innererror":{ "message":"Query option 'Count' is not allowed. To allow it, set the 'AllowedQueryOptions' property on EnableQueryAttribute or QueryValidationSettings.","type":"Microsoft.OData.Core.ODataException","stacktrace":" at System.Web.OData.Query.Validators.ODataQueryValidator.ValidateQueryOptionAllowed(AllowedQueryOptions queryOption, AllowedQueryOptions allowed)\r\n at System.Web.OData.Query.Validators.ODataQueryValidator.Validate(ODataQueryOptions options, ODataValidationSettings validationSettings)\r\n at System.Web.OData.Query.ODataQueryOptions.Validate(ODataValidationSettings validationSettings)\r\n at System.Web.OData.EnableQueryAttribute.ValidateQuery(HttpRequestMessage request, ODataQueryOptions queryOptions)\r\n at System.Web.OData.EnableQueryAttribute.ExecuteQuery(Object response, HttpRequestMessage request, HttpActionDescriptor actionDescriptor)\r\n at System.Web.OData.EnableQueryAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)" } } }
Assemblies affected
Using the library versions noted above along with a client generated from $metadata using the standard T4 template, simply run the query and observe the badly generated URI.
Expected result
If .ToArray() is needed after .Take(), then an intelligent error should be emitted and no badly formed URI generated. If it is not needed, then it should just work. This behavior and the bad URI generated are confusing.
Actual result
A badly formed URI is sent to the server.
@nickkin-msft,
var result = odataClient.MyCollection.Take(1); does not send a request to the server side. It only creates a query on client side. Then, once you call ToArray() to enumerate the query, the client will send the request to the server side.
So as .Count(). In this issue, you mentioned http://localhost/odata/MyCollection/$count?$top=1. I guess that your client code calls result.count() after odataClient.MyCollection.Take(1) which appends $count to the uri and send the request.
You are correct, we should provide a more reasonable error info about this.
Thanks! I was slightly mistaken about the repro, but the URI which the client generates is indeed invalid and agree that the client should throw an appropriate error if this type of query is not allowed.