Swagger-UI doesn't display response body with different Response Content-Type
See original GitHub issueI have a method which response class 200 is “byte” or a file and the Response Content Type is “application/octet-stream” and is displayed in the UI. This customization is done by implementing a IOperationFilter like this:
public sealed class UpdateFileResponseTypeFilter : IOperationFilter
{
public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
{
if (apiDescription.GetControllerAndActionAttributes<SwaggerResponseRemoveDefaultsAttribute>().Any())
{
operation.responses.Clear();
}
var responseAttributes = apiDescription.GetControllerAndActionAttributes<SwaggerFileResponseAttribute>()
.OrderBy(attr => attr.StatusCode);
foreach (var attr in responseAttributes)
{
var statusCode = attr.StatusCode.ToString();
Schema responseSchema = new Schema { format = "byte", type = "file" };
operation.produces.Clear();
operation.produces.Add("application/octet-stream");
operation.responses[statusCode] = new Response
{
description = attr.Description ?? InferDescriptionFrom(statusCode),
schema = responseSchema
};
}
}
...
}
Before in 5.5.3 version of Swashbuckle , If the status code was a 503, or 500 or any other code which is not a success 200, I was able to see in the UI the message displayed in the Response Body whatever was a string or and Exception object. When it was sucessful I was unable to see any link to file perhaps UI didn’t support previewing files at that version.
Now in the latest Nuget Swashbuckle version 5.6.0, after updating I’m almost in the opposite situation: When response is 200 I can see files with a link, but can’t see any message when status code is 503, or 500, spinner keeps loading and Response Header and Response Body are never displayed (but in Chrome inspector still can see the response).
For example, In the case of status 503 is determined by a custom action filter which returns the following Response:
...
string Message = string.Format("Server is in maintainance");
actionContext.Response = actionContext.Request
.CreateResponse(HttpStatusCode.ServiceUnavailable,Message);
...
If just found that If I send that response without that Message, swagger-ui displays Responses Body and Headers.
Another workaround I found was adding another Response Content Type in the operation filter that I said:
operation.produces.Add("application/octet-stream");
operation.produces.Add("text/json"); //Adding this line
And with that I can see the messages in Response Body in the UI, but I need to manually select in combo the Response Content Type to text/json otherwise it keeps loading forever.
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (1 by maintainers)
@niemyjski What do you mean “don’t show any response” I do see Response Messages in your link:
Try adding after
[ResponseType(typeof(PersistentEvent))]
Add:[SwaggerResponse(200, "Get by id 200", typeof(PersistentEvent))]
and you should see a Response Class. https://github.com/heldersepu/Exceptionless/commit/0b868aa0b7c87611154c76f2cc6d2fbb5a6aeb2bany news on this?