Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting response HTTP Headers in the C# client #4876

Closed
paulomorgado opened this issue Apr 30, 2024 · 1 comment
Closed

Getting response HTTP Headers in the C# client #4876

paulomorgado opened this issue Apr 30, 2024 · 1 comment

Comments

@paulomorgado
Copy link
Contributor

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
Copy link
Contributor Author

paulomorgado commented Apr 30, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant