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.

Response headers not being displayed in UI

See original GitHub issue

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

github_iconTop GitHub Comments

3reactions
mattfrearcommented, Dec 20, 2019

@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).

0reactions
WhitWaldocommented, Dec 19, 2019

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>();

    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, OpenApiHeader>();

            response.Headers.Add(attr.Name, new Header { 
                Description = attr.Description,
                Content = new Dictionary<string, OpenApiMediaType> { 
                      [attr.Type] = new OpenApiMediaType()
                }
            });
        }
    }
   
}

}

Read more comments on GitHub >

github_iconTop 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 >

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