RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.61k stars 1.22k forks source link

Getting response HTTP Headers in the C# client #4876

Closed paulomorgado closed 2 months ago

paulomorgado commented 2 months ago

I have this API defined as:

.WithOpenApi(openApi =>
{
    openApi.Responses.Add(
        StatusCodes.Status200OK.ToString(),
        new OpenApiResponse
        {
            Content =
            {
                ["application/octet-stream"] = new OpenApiMediaType
                {
                    Schema = new OpenApiSchema
                    {
                        Type = "string",
                        Format = "binary",
                    },
                }
            },
            Headers =
            {
                ["X-MY-TOKEN"] = new OpenApiHeader
                {
                    Description = "The token to acknowledge the message.",
                    Required = true,
                    Schema = new OpenApiSchema
                    {
                        Type = "string",
                    },
                }
            }
        });

    return openApi;
});

Which ends up generating a Swager 2.0 specification as:

"200": {
  "description": null,
  "schema": {
    "type": "string",
    "format": "binary"
  },
  "headers": {
    "X-MY-TOKEN": {
      "required": true,
      "description": "The token to acknowledge the message.",
      "schema": {
        "type": "string"
      }
    }
  }
}

However, there is no way in the generated C# client (latest version) to get the value of the X-MY-TOKEN HTTP header.

Any hints?

paulomorgado commented 2 months ago

OOPS!, My bad!

It's in the FileResponse:

[System.CodeDom.Compiler.GeneratedCode("NSwag", "14.0.7.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class FileResponse : System.IDisposable
{
    private System.IDisposable _client;
    private System.IDisposable _response;

    public int StatusCode { get; private set; }

    public System.Collections.Generic.IReadOnlyDictionary<string, System.Collections.Generic.IEnumerable<string>> Headers { get; private set; }

    public System.IO.Stream Stream { get; private set; }

    public bool IsPartial
    {
        get { return StatusCode == 206; }
    }

    public FileResponse(int statusCode, System.Collections.Generic.IReadOnlyDictionary<string, System.Collections.Generic.IEnumerable<string>> headers, System.IO.Stream stream, System.IDisposable client, System.IDisposable response)
    {
        StatusCode = statusCode;
        Headers = headers;
        Stream = stream;
        _client = client;
        _response = response;
    }

    public void Dispose()
    {
        Stream.Dispose();
        if (_response != null)
            _response.Dispose();
        if (_client != null)
            _client.Dispose();
    }
}