AspNetCoreOData
AspNetCoreOData copied to clipboard
On OData error, no ProblemDetails is returned
Assemblies affected 9.2.0
Describe the bug
If the query is wrong e.g. https://localhost:5001/odata/myset?$filter=invalid the error returned is some custom format, and not in the ProblemDetails format that Micrsoft suggests.
Request/Response
Execute an invalid query e.g. https://localhost:5001/odata/myset?$filter=invalid
{
"error": {
"code": "",
"message": "The query specified in the URI is not valid. Could not find a property named 'invalid' on type 'REDACTED.",
"details": [],
"innererror": {
"message": "Could not find a property named 'invalid' on type 'REDACTED'.",
"type": "Microsoft.OData.ODataException",
"stacktrace": " at Microsoft.OData.UriParser.EndPathBinder.GeneratePropertyAccessQueryForOpenType(EndPathToken endPathToken, SingleValueNode parentNode)\r\n at Microsoft.OData.UriParser.EndPathBinder.BindEndPath(EndPathToken endPathToken)\r\n at Microsoft.OData.UriParser.MetadataBinder.BindEndPath(EndPathToken endPathToken)\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.OnActionExecuting(ActionExecutingContext actionExecutingContext)"
}
}
}
Expected behavior I excpected the error to be of type ProblemDetails
{
"type": "https://tools.ietf.org/html/rfc9110#section-15.5.1",
"title": "The query specified in the URI is not valid. Could not find a property named 'invalid' on type 'REDACTED'.",
"status": 400,
"Errors": {
"Error": [
"Could not find a property named 'invalid' on type 'REDACTED."
]
}
}
Workaround Use the following EnableQueryWithProblemDetailsAttribute instead of the EnableQueryAttribute:
public class EnableQueryWithProblemDetailsAttribute : EnableQueryAttribute
{
public override void OnActionExecuting(ActionExecutingContext actionExecutingContext)
{
base.OnActionExecuting(actionExecutingContext);
if (actionExecutingContext.Result is BadRequestObjectResult bror)
{
if(bror.Value is SerializableError error)
{
var problemDetails = new ProblemDetails
{
Type = "https://tools.ietf.org/html/rfc9110#section-15.5.1"
};
if (error.TryGetValue("Message", out var message))
{
problemDetails.Title = message.ToString();
}
if (error.TryGetValue("ExceptionMessage", out var messageType))
{
var dictionary = new Dictionary<string, string?[]> { { "Error", [messageType.ToString()] } };
problemDetails.Extensions.Add("errors", dictionary);
}
actionExecutingContext.Result = new BadRequestObjectResult(problemDetails);
}
}
}
}
The error generated from OData lib is compatible to OData spec.
ProblemDetails is defined in ASP.NET Core.
By default, OData converts SerializableError to 'ODataError' and serialize it using OData format.
Thanks for clarification.