question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Swagger-UI doesn't display response body with different Response Content-Type

See original GitHub issue

I 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:open
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
heldersepucommented, Sep 3, 2017

@niemyjski What do you mean “don’t show any response” I do see Response Messages in your link:

  • 404 | The event occurrence could not be found.
  • 426 | Unable to view event occurrence due to plan limits.

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/0b868aa0b7c87611154c76f2cc6d2fbb5a6aeb2b

0reactions
MiroKovcommented, Mar 3, 2019

any news on this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Swagger-UI doesn't display response body with different ...
I have a method which response class 200 is "byte" or a file and the Response Content Type is "application/octet-stream" and is displayed...
Read more >
Swagger doesn't show response body
My Swagger endpoint always shows "no content" in the Response Body. Al tough the server sends a meaningful response text back. swagger UI....
Read more >
Swagger UI not displaying all response headers
My server is a JAX-RS application running on glassfish, as I am running on different servers I had to set up CORS which...
Read more >
Describing Responses
An API can respond with various media types. JSON is the most common format for data exchange, but not the only one possible....
Read more >
swagger response body no content
When I return "String" from the REST api, swagger-ui is displaying "no content" in the response body with response code as "0".
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found