The HttpResponseMessage.ReasonPhrase is not displayed in swagger-ui.
As best as I can tell, this is still an issue using 5.5.3 from Nuget. This is related to Issue #623 but it seems that issue was closed after a work-around was suggested. Unfortunately, the root problem was never addressed (it was suggested to be reopened but never was, hency why I'm filing an additional issue - sorry if you consider this a duplicate and want the other one reopened instead). FYI, in Issue #623 we see that the OP had originally made this request to the Swagger team but they pushed the user to the Swashbuckle team. However, I'm not sure if the spec and/or Swagger UI supports this ReasonPhrase info - I totally defer to ddd and others more familiar with this than I.
Here is some additional information for reproducing this.
I return a 401 like this:
throw new HttpResponseException(new HttpResponseMessage { ReasonPhrase = "Invalid API Key", StatusCode = HttpStatusCode.Unauthorized });
If I view the response with Fiddler or other, I see: 401 Invalid API Key. So far, so good - the server-side stuff is working perfectly!
However, when I use Swagger UI to test the service, I do see 401 for the status code but nowhere is the Invalid API Key text displayed.
I expect to be able to view the text assigned to the ReasonPhrase somewhere within Swagger UI. I'm not familiar enough with Swagger UI or the spec to know exactly where it belongs.
I too am having this exact problem, except with a 400 instead of a 401. I can get Chrome and Postman to show the reasonphrase by default, but not Swagger UI. It seems to be missing a way to interpret ReasonPhrase from HttpResponseException as the default messaging.
Is there some sort of configuration option that will allow for interpretation of exception responses, so we can access the reason phrase to display?
So if anyone else has this issue, my way to get around it is just to modify the return around the function in the authentication class that continues to the API endpoints. This only works if you are implementing some sort of authentication gateway, like IdentityBasicAuthentication in WebAPI.
This is a simplification of what I'm doing:
Class IdentityBasicAuthentication
Function ExecuteAutorizationFilterAsync
...
Dim objAPIResults As Task(Of HttpResponseMessage) = continuation()
Return ProcessResults(objAPIResults)
Within ProcessResults, I check to see if the request is coming from SwaggerUI, then modify the response.
Function ProcessResults()
If IsSwaggerRequest() Then
Dim strError As String = objOriginalResponse.ReasonPhrase
objOriginalResponse.Content = New StringContent(strError)
Throw New Http.HttpResponseException(objOriginalResponse)
End If
...
@rudyscoggins this is a suitable workaround and I came up with a very similar solution using C# and a standard ActionFilterAttribute that I want to share.
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public class SwaggerReasonPhraseFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(HttpActionExecutedContext context)
{
if (context.Response.Content == null && context.Request.IsSwagger())
{
context.Response.Content = new StringContent(context.Response.ReasonPhrase);
}
}
}
I added an extension method to the HttpRequestMessage that just checks the Referrer for the text "swagger" which in most cases will determine if the request came from swaggerUI.
I'm curious what code you have in method IsSwaggerRequest() and if you have a more robust way of determining if this is a swagger request.
Hopefully this is something that can be added into SwaggerUI/Swashbuckle in the future so we don't need to have these workarounds
@ahoefling could you send IsSwagger Extension Method?
Most helpful comment
@rudyscoggins this is a suitable workaround and I came up with a very similar solution using C# and a standard ActionFilterAttribute that I want to share.
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)] public class SwaggerReasonPhraseFilterAttribute : ActionFilterAttribute { public override void OnActionExecuted(HttpActionExecutedContext context) { if (context.Response.Content == null && context.Request.IsSwagger()) { context.Response.Content = new StringContent(context.Response.ReasonPhrase); } } }I added an extension method to the
HttpRequestMessagethat just checks theReferrerfor the text "swagger" which in most cases will determine if the request came from swaggerUI.I'm curious what code you have in method
IsSwaggerRequest()and if you have a more robust way of determining if this is a swagger request.Hopefully this is something that can be added into SwaggerUI/Swashbuckle in the future so we don't need to have these workarounds