ODataException when incorrect key datatype
Open
@xuzhg is already working on this.
Since Sep 14, 2021.
- Dominant language
- C#
- Stars
- 505
- Forks
- 186
- PR merge metrics
- No merged PRs in 30d
Description
When passing the incorrect type for a key, an exception is thrown in the KeySegmentTemplate causing a HTTP 500. I would expect this to cause a HTTP 400 response with a valid OData error returned (similar to what is done elsewhere).
To recreate start the ODataRoutingSample and open the url http://localhost:5000/Products('2'). This results in an unhandled exception:
Microsoft.OData.ODataException: The key value ('2') from request is not valid. The key value should be format of type 'Edm.Int32'.
---> Microsoft.OData.ODataException: Type verification failed. Expected type 'Edm.Int32' but received the value ''2''.
at Microsoft.OData.ODataUriConversionUtils.VerifyAndCoerceUriPrimitiveLiteral(Object primitiveValue, String literalValue, IEdmModel model, IEdmTypeReference expectedTypeReference)
at Microsoft.OData.ODataUriUtils.ConvertFromUriLiteral(String value, ODataVersion version, IEdmModel model, IEdmTypeReference typeReference)
at Microsoft.AspNetCore.OData.Routing.Template.KeySegmentTemplate.TryTranslate(ODataTemplateTranslateContext context) in /home/tas/src/AspNetCoreOData/src/Microsoft.AspNetCore.OData/Routing/Template/KeySegmentTemplate.cs:line 197
--- End of inner exception stack trace ---
at Microsoft.AspNetCore.OData.Routing.Template.KeySegmentTemplate.TryTranslate(ODataTemplateTranslateContext context) in /home/tas/src/AspNetCoreOData/src/Microsoft.AspNetCore.OData/Routing/Template/KeySegmentTemplate.cs:line 202
at Microsoft.AspNetCore.OData.Routing.Template.DefaultODataTemplateTranslator.Translate(ODataPathTemplate path, ODataTemplateTranslateContext context) in /home/tas/src/AspNetCoreOData/src/Microsoft.AspNetCore.OData/Routing/Template/DefaultODataTemplateTranslator.cs:line 33
at Microsoft.AspNetCore.OData.Routing.ODataRoutingMatcherPolicy.ApplyAsync(HttpContext httpContext, CandidateSet candidates) in /home/tas/src/AspNetCoreOData/src/Microsoft.AspNetCore.OData/Routing/ODataRoutingMatcherPolicy.cs:line 102
at Microsoft.AspNetCore.Routing.Matching.DfaMatcher.SelectEndpointWithPoliciesAsync(HttpContext httpContext, IEndpointSelectorPolicy[] policies, CandidateSet candidateSet)
at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.<Invoke>g__AwaitMatch|8_1(EndpointRoutingMiddleware middleware, HttpContext httpContext, Task matchTask)
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at Microsoft.AspNetCore.OData.Batch.ODataBatchMiddleware.Invoke(HttpContext context) in /home/tas/src/AspNetCoreOData/src/Microsoft.AspNetCore.OData/Batch/ODataBatchMiddleware.cs:line 70
at Microsoft.AspNetCore.OData.Query.ODataQueryRequestMiddleware.Invoke(HttpContext context) in /home/tas/src/AspNetCoreOData/src/Microsoft.AspNetCore.OData/Query/ODataQueryRequestMiddleware.cs:line 68
at Microsoft.AspNetCore.OData.Routing.ODataRouteDebugMiddleware.Invoke(HttpContext context) in /home/tas/src/AspNetCoreOData/src/Microsoft.AspNetCore.OData/Routing/ODataRouteDebugMiddleware.cs:line 79
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.