dotnet / aspnet-api-versioning

Provides a set of libraries which add service API versioning to ASP.NET Web API, OData with ASP.NET Web API, and ASP.NET Core.
MIT License
3.06k stars 703 forks source link

The error 'code' is not included in the error response when using AddErrorObjects() #1091

Open douggish opened 5 months ago

douggish commented 5 months ago

Is there an existing issue for this?

Describe the bug

When using Microsoft.AspNetCore.Mvc.Versioning 5.1.0, the error responses contained an error code (e.g. ApiVersionUnspecified). When upgrading to Asp.Versioning 8.1.0, the documentation states that it is possible to retain the previous "Error Object" format (which I would expect to include the code property). However, even when using builder.Services.AddErrorObjects().AddProblemDetails(), there is no code property in the error response.

I'm seeing something like this:

{
    "error": {
        "message": "Unspecified API version",
        "target": "Unspecified API version",
        "innerError": {
            "message": "An API version is required, but was not specified."
        }
    }
}

Expected Behavior

I expected the error response to match what is was in the older version (especially the code property):

{
  "error": {
    "code": "ApiVersionUnspecified",
    "message": "An API version is required, but was not specified.",
    "innerError": null
  }
}

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

8.0.204

Anything else?

I think this line of code is perhaps missing a not: https://github.com/dotnet/aspnet-api-versioning/blob/main/src/AspNetCore/WebApi/src/Asp.Versioning.Http/Routing/EndpointProblem.cs#L28

commonsensesoftware commented 5 months ago

Yeah, it looks like this is meant to be:

+if ( !string.IsNullOrEmpty( code ) )

😳 doh!

commonsensesoftware commented 5 months ago

I'll work on getting out a fix. CRs are also welcome. 😃