Response headers not being displayed in UI
See original GitHub issueI wrote a custom SwaggerResponseHeader
attribute to document response headers. I also wrote a custom AddResponseHeadersFilter
to add the documentation to the swagger output. I then used the attribute on one of the controller actions. All works fine. The swagger output contains the specified response header and the editor at editor.swagger.io nicely displays the response header.
My API’s swagger UI however does not display the response header. Is there a way to fix that?
Attribute
public class SwaggerResponseHeaderAttribute : Attribute
{
public SwaggerResponseHeaderAttribute(HttpStatusCode statusCode, string name, string type, string description)
{
StatusCode = statusCode;
Name = name;
Type = type;
Description = description;
}
public HttpStatusCode StatusCode { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public string Description { get; set; }
}
Filter
public class AddResponseHeadersFilter : IOperationFilter
{
public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
{
var responseAttributes = apiDescription.GetControllerAndActionAttributes<SwaggerResponseHeaderAttribute>();
foreach (var attr in responseAttributes)
{
var response = operation.responses.FirstOrDefault(x => x.Key == ((int)attr.StatusCode).ToString(CultureInfo.InvariantCulture)).Value;
if (response != null)
{
if (response.headers == null)
{
response.headers = new Dictionary<string, Header>();
}
response.headers.Add(attr.Name, new Header {description = attr.Description, type = attr.Type});
}
}
}
}
Controller
[SwaggerResponseRemoveDefaults]
[SwaggerResponse(HttpStatusCode.Created, "Created", typeof(Person))]
[SwaggerResponseHeader(HttpStatusCode.Created, "Location", "string", "Location of the newly created resource")]
public IHttpActionResult Post(Person person)
{
//...
}
Documentation (part)
{
"responses":{
"201":{
"description":"Created",
"schema":{
"$ref":"#/definitions/Person"
},
"headers":{
"Location":{
"description":"Location of the newly created resource.",
"type":"string"
}
}
}
}
}
Issue Analytics
- State:
- Created 8 years ago
- Comments:7 (1 by maintainers)
Top Results From Across the Web
Not all response headers are showing up on swagger UI ...
I am hitting an API endpoint using the swagger UI, for some reason I am NOT able to see all the response headers,...
Read more >Response headers not being displayed in UI · Issue #655
My API's swagger UI however does not display the response header. Is there a way to fix that? Attribute. public class ...
Read more >Swagger UI not displaying all response headers
1. Add my server url to the green swagger bar, hit enter. · 2. My api appears below · 3. Browse to the...
Read more >Why are not all response headers exposed
This is done through the Access-Control-Expose-Headers response header. It "indicates which headers can be exposed as part of the response by listing their ......
Read more >Apigee X missing Request/Response Headers on Debug...
With new UI when I DEBUG, I'm unable to view the payload/body section for request/response. I could be missing something can you/somebody help?...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@WhitWaldo yep, I ported it to OpenApi almost a year ago.
https://github.com/mattfrear/Swashbuckle.AspNetCore.Filters/blob/master/src/Swashbuckle.AspNetCore.Filters/ResponseHeaders/AddResponseHeadersFilter.cs
Or you can get it from my NuGet (Swashbuckle.AspNetCore.Filters).
Small tweak to @rocklan 's response to accommodate 5.0.0-rc5 (and the shift to OpenApi):
public class AddResponseHeadersFilter : IOperationFilter { public void Apply(Operation operation, OperationFilterContext context) { var responseAttributes = context.MethodInfo.GetCustomAttributes<ProducesResponseTypeHeader>();
}