OData / WebApi

OData Web API: A server library built upon ODataLib and WebApi
https://docs.microsoft.com/odata
Other
855 stars 473 forks source link

in operator fails for datetimeoffset fields. #2373

Open ghost opened 3 years ago

ghost commented 3 years ago

Attempting to use in operator in a $filter clause on fields of type Edm.DateTimeOffset returns an error from the server.

Assemblies affected

Microsoft.AspNetCore.OData 8.0.0-preview2

Reproduce steps

See simple project setup at https://github.com/bascott/odata-example

Run the project, then navigate to https://localhost:5001/odata/WeatherForecasts?$filter=Date%20in%20(2020-12-09) or https://localhost:5001/odata/WeatherForecasts?$filter=Date%20in%20(2020-12-09T00:00:00-05:00)

Expected result

Result would be filtered on supplied date.

Actual result

Expanded error returned from server:

{
    "error": {
        "code": "",
        "message": "The query specified in the URI is not valid. Invalid JSON. The value '2020-12-09' is not a valid number.",
        "details": [],
        "innererror": {
            "message": "Invalid JSON. The value '2020-12-09' is not a valid number.",
            "type": "Microsoft.OData.ODataException",
            "stacktrace": "   at Microsoft.OData.Json.JsonReader.ParseNumberPrimitiveValue()\r\n   at Microsoft.OData.Json.JsonReader.ParseValue()\r\n   at Microsoft.OData.Json.JsonReader.Read()\r\n   at Microsoft.OData.Json.BufferingJsonReader.ReadInternal()\r\n   at Microsoft.OData.Json.BufferingJsonReader.ReadNextAndCheckForInStreamError()\r\n   at Microsoft.OData.Json.BufferingJsonReader.ReadInternal()\r\n   at Microsoft.OData.Json.BufferingJsonReader.Read()\r\n   at Microsoft.OData.JsonLight.ODataJsonLightPropertyAndValueDeserializer.ReadCollectionValue(IEdmCollectionTypeReference collectionValueTypeReference, String payloadTypeName, ODataTypeAnnotation typeAnnotation)\r\n   at Microsoft.OData.JsonLight.ODataJsonLightPropertyAndValueDeserializer.ReadNonEntityValueImplementation(String payloadTypeName, IEdmTypeReference expectedTypeReference, PropertyAndAnnotationCollector propertyAndAnnotationCollector, CollectionWithoutExpectedTypeValidator collectionValidator, Boolean validateNullValue, Boolean isTopLevelPropertyValue, Boolean insideResourceValue, String propertyName, Nullable`1 isDynamicProperty)\r\n   at Microsoft.OData.JsonLight.ODataJsonLightPropertyAndValueDeserializer.ReadNonEntityValue(String payloadTypeName, IEdmTypeReference expectedValueTypeReference, PropertyAndAnnotationCollector propertyAndAnnotationCollector, CollectionWithoutExpectedTypeValidator collectionValidator, Boolean validateNullValue, Boolean isTopLevelPropertyValue, Boolean insideResourceValue, String propertyName, Nullable`1 isDynamicProperty)\r\n   at Microsoft.OData.ODataUriConversionUtils.ConvertFromResourceOrCollectionValue(String value, IEdmModel model, IEdmTypeReference typeReference)\r\n   at Microsoft.OData.ODataUriConversionUtils.ConvertFromCollectionValue(String value, IEdmModel model, IEdmTypeReference typeReference)\r\n   at Microsoft.OData.UriParser.InBinder.GetCollectionOperandFromToken(QueryToken queryToken, IEdmTypeReference expectedType, IEdmModel model)\r\n   at Microsoft.OData.UriParser.InBinder.BindInOperator(InToken inToken, BindingState state)\r\n   at Microsoft.OData.UriParser.MetadataBinder.BindIn(InToken inToken)\r\n   at Microsoft.OData.UriParser.MetadataBinder.Bind(QueryToken token)\r\n   at Microsoft.OData.UriParser.FilterBinder.BindFilter(QueryToken filter)\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.AspNetCore.OData.Query.FilterQueryOption.get_FilterClause()\r\n   at Microsoft.AspNetCore.OData.Query.Validator.FilterQueryValidator.Validate(FilterQueryOption filterQueryOption, ODataValidationSettings settings)\r\n   at Microsoft.AspNetCore.OData.Query.FilterQueryOption.Validate(ODataValidationSettings validationSettings)\r\n   at Microsoft.AspNetCore.OData.Query.Validator.ODataQueryValidator.Validate(ODataQueryOptions options, ODataValidationSettings validationSettings)\r\n   at Microsoft.AspNetCore.OData.Query.ODataQueryOptions.Validate(ODataValidationSettings validationSettings)\r\n   at Microsoft.AspNetCore.OData.Query.EnableQueryAttribute.ValidateQuery(HttpRequest request, ODataQueryOptions queryOptions)\r\n   at Microsoft.AspNetCore.OData.Query.EnableQueryAttribute.CreateAndValidateQueryOptions(HttpRequest request, ODataQueryContext queryContext)\r\n   at Microsoft.AspNetCore.OData.Query.EnableQueryAttribute.ExecuteQuery(Object responseValue, IQueryable singleResultCollection, ControllerActionDescriptor actionDescriptor, HttpRequest request)\r\n   at Microsoft.AspNetCore.OData.Query.EnableQueryAttribute.OnActionExecuted(ActionExecutedContext actionExecutedContext, Object responseValue, IQueryable singleResultCollection, ControllerActionDescriptor actionDescriptor, HttpRequest request)"
        }
    }
}
xuzhg commented 3 years ago

@bascott From your repo, "Date" property is "DateTime" type, in this case:

1) Would you please use the "date" build-in method in the query. date(Date)%20in%20(2020-12-09) 2) Could you please "Date" CLR type to define the "Date" Property.

ghost commented 3 years ago

@xuzhg I attempted both and received the same error message.

xsmerek commented 3 years ago

Same problem for a property of type Edm.Date.