Skip to content

On OData error, no ProblemDetails is returned #1438

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
michaelmairegger opened this issue Mar 14, 2025 · 2 comments
Open

On OData error, no ProblemDetails is returned #1438

michaelmairegger opened this issue Mar 14, 2025 · 2 comments
Assignees
Labels
question Further information is requested

Comments

@michaelmairegger
Copy link

michaelmairegger commented Mar 14, 2025

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);
            }
        }
    }
}
@michaelmairegger michaelmairegger added the bug Something isn't working label Mar 14, 2025
@michaelmairegger michaelmairegger changed the title OData Query error returns not ProblemDetails On OData error, no ProblemDetails is returned Mar 14, 2025
@xuzhg xuzhg self-assigned this Mar 18, 2025
@xuzhg xuzhg added question Further information is requested and removed bug Something isn't working labels Mar 18, 2025
@xuzhg
Copy link
Member

xuzhg commented Mar 18, 2025

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.

@michaelmairegger
Copy link
Author

Thanks for clarification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants