WebApi icon indicating copy to clipboard operation
WebApi copied to clipboard

Count with filter doesn't work in ODATA queries

Open LianwMS opened this issue 11 years ago • 15 comments

According to ODATA v4 Spec Protocol Part

Section 11.2.5.1 System Query Option $filter

http://docs.oasis-open.org/odata/odata/v4.0/errata02/os/complete/part1-protocol/odata-v4.0-errata02-os-part1-protocol-complete.html#_Toc406398301

The following query is supported in ODATA

http://host/service/Categories?$filter=Products/$count lt 10

However when I try this against a Web API 2.2 with OData4, I am getting the following error:

"code":"","message":"The query specified in the URI is not valid. The parent value for a property access of a property '$count' is not a single value. Property access can only be applied to a single value.","innererror":{ "message":"The parent value for a property access of a property '$count' is not a single value. Property access can only be applied to a single value.","type":"Microsoft.OData.Core.ODataException","stacktrace":" at Microsoft.OData.Core.UriParser.Parsers.EndPathBinder.BindEndPath....

Is this the expected behavior?

LianwMS avatar Jan 21 '15 02:01 LianwMS

I have the same issue.

AndriiKuznietsov avatar Jun 11 '15 10:06 AndriiKuznietsov

Same here.

rpawlaszek avatar Jun 18 '15 22:06 rpawlaszek

According to the OData 4 standard this should work. Any word on this?

hollis21 avatar Jul 17 '15 18:07 hollis21

Should work.. same here just not with filter but with orderby for my example:

http://localhost:60760/odata/CurrentlyAdmitted?$top=50&$orderby=admissionsLastYear/$count

getting:

{
  "error":{
    "code":"","message":"The query specified in the URI is not valid. The parent value for a property access of a property '$count' is not a single value. Property access can only be applied to a single value.","innererror":{
      "message":"The parent value for a property access of a property '$count' is not a single value. Property access can only be applied to a single value.","type":"Microsoft.OData.Core.ODataException","stacktrace":"   at Microsoft.OData.Core.UriParser.Parsers.EndPathBinder.BindEndPath(EndPathToken endPathToken)\r\n   at Microsoft.OData.Core.UriParser.Parsers.MetadataBinder.BindEndPath(EndPathToken endPathToken)\r\n   at Microsoft.OData.Core.UriParser.Parsers.MetadataBinder.Bind(QueryToken token)\r\n   at Microsoft.OData.Core.UriParser.Parsers.OrderByBinder.ProcessSingleOrderBy(BindingState state, OrderByClause thenBy, OrderByToken orderByToken)\r\n   at Microsoft.OData.Core.UriParser.Parsers.OrderByBinder.BindOrderBy(BindingState state, IEnumerable`1 orderByTokens)\r\n   at Microsoft.OData.Core.UriParser.ODataQueryOptionParser.ParseOrderByImplementation(String orderBy, ODataUriParserConfiguration configuration, IEdmType elementType, IEdmNavigationSource navigationSource)\r\n   at Microsoft.OData.Core.UriParser.ODataQueryOptionParser.ParseOrderBy()\r\n   at System.Web.OData.Query.OrderByQueryOption.get_OrderByClause()\r\n   at System.Web.OData.Query.Validators.OrderByQueryValidator.Validate(OrderByQueryOption orderByOption, ODataValidationSettings validationSettings)\r\n   at System.Web.OData.Query.OrderByQueryOption.Validate(ODataValidationSettings validationSettings)\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)"
    }
  }
}

mikila85 avatar Jul 20 '15 08:07 mikila85

It's issue in ODL parse.

xuzhg avatar Jan 07 '16 02:01 xuzhg

Any news on this, or a workaround besides a custom function? I really want this: GET http://somedomain.com.au/odata/Programs?$filter=Sessions/$count+lte+MaxSessions

andrensairr avatar Feb 17 '16 01:02 andrensairr

One more note, specification has a bug before, it is fixed in this link https://tools.oasis-open.org/version-control/browse/wsvn/odata/trunk/spec/ABNF/odata-abnf-construction-rules.txt?op=diff&rev=781

Which means orderby and filter based on complexColl/TypeCast/$count are supported too.

chinadragon0515 avatar Feb 25 '16 02:02 chinadragon0515

See odata lib commit: https://github.com/OData/odata.net/commit/1b58ec6cab8965e68b4a8639262583adbce04e22

xuzhg avatar Feb 25 '16 02:02 xuzhg

ODL V6.15 will support this. So, Web API OData v5.10 can add it.

xuzhg avatar May 19 '16 06:05 xuzhg

Related issue : https://github.com/OData/odata.net/issues/573

VikingsFan avatar May 19 '16 07:05 VikingsFan

@AndriiKuznietsov @rpawlaszek @hollis21 @mikila85 @andrensairr would you help to review my PR #738 ?

VikingsFan avatar May 20 '16 02:05 VikingsFan

Any progress on this issue?

GODBS avatar May 12 '17 18:05 GODBS

Looks like this is not yet fixed though a very popular request.

mikepizzo avatar May 30 '17 21:05 mikepizzo

I still see "message": "The method or operation is not implemented." for sample project. Is ODATA queries #194 fixed? https://services.odata.org/TripPinRESTierService/People?$orderby=Trips/$count

Lsiriki avatar Jan 02 '19 06:01 Lsiriki

This is still an issue for me with Odata V4 - https://host/service?$filter=RowChangedDatetime gt 2021-06-30T15:59:57.0000000Z/$count

When I send the above on Postman I get { "error": { "code": "", "message": "The query specified in the URI is not valid. The DateTimeOffset text '2021-06-30T15:59:57.0000000Z/$count' should be in format 'yyyy-mm-ddThh:mm:ss('.'s+)?(zzzzzz)?' and each field value is within valid range.", "details": [], "innererror": { "message": "The DateTimeOffset text '2021-06-30T15:59:57.0000000Z/$count' should be in format 'yyyy-mm-ddThh:mm:ss('.'s+)?(zzzzzz)?' and each field value is within valid range.", "type": "Microsoft.OData.ODataException", "stacktrace": " at Microsoft.OData.UriUtils.ConvertUriStringToDateTimeOffset(String text, DateTimeOffset& targetValue)\r\n at Microsoft.OData.UriParser.ExpressionLexer.TryParseDateTimeoffset(Int32 tokenPos)\r\n at Microsoft.OData.UriParser.ExpressionLexer.ParseFromDigit()\r\n at Microsoft.OData.UriParser.ExpressionLexer.NextTokenImplementation(Exception& error)\r\n at Microsoft.OData.UriParser.ExpressionLexer.NextToken()\r\n at Microsoft.OData.UriParser.UriQueryExpressionParser.ParseComparison()\r\n at Microsoft.OData.UriParser.UriQueryExpressionParser.ParseLogicalAnd()\r\n at Microsoft.OData.UriParser.UriQueryExpressionParser.ParseLogicalOr()\r\n at Microsoft.OData.UriParser.UriQueryExpressionParser.ParseExpression()\r\n at Microsoft.OData.UriParser.UriQueryExpressionParser.ParseExpressionText(String expressionText)\r\n at Microsoft.OData.UriParser.ODataQueryOptionParser.ParseFilterImplementation(String filter, ODataUriParserConfiguration configuration, ODataPathInfo odataPathInfo)\r\n at Microsoft.OData.UriParser.ODataQueryOptionParser.ParseFilter()\r\n at Microsoft.AspNet.OData.Query.FilterQueryOption.get_FilterClause()\r\n at Microsoft.AspNet.OData.Query.Validators.FilterQueryValidator.Validate(FilterQueryOption filterQueryOption, ODataValidationSettings settings)\r\n at Microsoft.AspNet.OData.Query.Validators.ODataQueryValidator.Validate(ODataQueryOptions options, ODataValidationSettings validationSettings)\r\n at Microsoft.AspNet.OData.EnableQueryAttribute.ValidateQuery(HttpRequest request, ODataQueryOptions queryOptions)\r\n at Microsoft.AspNet.OData.EnableQueryAttribute.<>c__DisplayClass1_0.<OnActionExecuted>b__1(ODataQueryContext queryContext)\r\n at Microsoft.AspNet.OData.EnableQueryAttribute.ExecuteQuery(Object responseValue, IQueryable singleResultCollection, IWebApiActionDescriptor actionDescriptor, Func2 modelFunction, IWebApiRequestMessage request, Func2 createQueryOptionFunction)\r\n at Microsoft.AspNet.OData.EnableQueryAttribute.OnActionExecuted(Object responseValue, IQueryable singleResultCollection, IWebApiActionDescriptor actionDescriptor, IWebApiRequestMessage request, Func2 modelFunction, Func2 createQueryOptionFunction, Action1 createResponseAction, Action3 createErrorAction)", "internalexception": { "message": "The string '2021-06-30T15:59:57.0000000Z/$count' is not a valid AllXsd value.", "type": "System.FormatException", "stacktrace": " at System.Xml.XmlConvert.ToDateTimeOffset(String s)\r\n at Microsoft.OData.PlatformHelper.ConvertStringToDateTimeOffset(String text)\r\n at Microsoft.OData.UriUtils.ConvertUriStringToDateTimeOffset(String text, DateTimeOffset& targetValue)" } } } }

lpmac avatar Jul 09 '21 11:07 lpmac