NSwag: FileResponse compile-time error

Hi,

I’I have one issue with NSWAG (i’m using the latest 12.0.10.0).

When i have in openapi 3.0 definition , response for api which downloads a file, like this:

responses: ‘200’: description: The content of the file content: application/octet-stream: schema: type: string format: binary

In my C# Web Api controller generated code , i have compile-time error in:

HttpResponseMessage response = Request.CreateResponse(status, result.Result);

where Result doesn’t exists in FileResponse class:

  public partial class FileResponse : System.IDisposable
  {
    private System.IDisposable _client;
    private System.IDisposable _response;

    public int StatusCode { get; private set; }

    public System.Collections.Generic.Dictionary<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.Dictionary<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()
    {
      if (Stream != null)
        Stream.Dispose();
      if (_response != null)
        _response.Dispose();
      if (_client != null)
        _client.Dispose();
    }
  }

So either this is a compile-time error with generated code or i should use some other type of response ?

Thank you.

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Comments: 20 (9 by maintainers)

Commits related to this issue

Most upvoted comments

@novakmarkov, @RicoSuter, @Alamaster99,

Is there any update on this?

I temporaly solved it by adding the following in the generated c# client

using FileResponse=Microsoft.AspNetCore.Mvc.IActionResult;

So the output is

//----------------------
// <auto-generated>
//     Generated using the NSwag toolchain v12.0.11.0 (NJsonSchema v9.13.13.0 (Newtonsoft.Json v11.0.0.0)) (http://NSwag.org)
// </auto-generated>
//----------------------

namespace MyNamespace
{
    #pragma warning disable

    [System.CodeDom.Compiler.GeneratedCode("NSwag", "12.0.11.0 (NJsonSchema v9.13.13.0 (Newtonsoft.Json v11.0.0.0))")]
    public interface IController
    {
        /// <summary>Download</summary>
        /// <returns>The content of the file</returns>
        System.Threading.Tasks.Task<FileResponse> DownloadAsync();
    
    }
    
    [System.CodeDom.Compiler.GeneratedCode("NSwag", "12.0.11.0 (NJsonSchema v9.13.13.0 (Newtonsoft.Json v11.0.0.0))")]
    [System.Web.Http.RoutePrefix("api")]
    public partial class Controller : System.Web.Http.ApiController
    {
        private IController _implementation;
    
        public Controller(IController implementation)
        {
            _implementation = implementation;
        }
    
        /// <summary>Download</summary>
        /// <returns>The content of the file</returns>
        [System.Web.Http.HttpGet, System.Web.Http.Route("download")]
        public System.Threading.Tasks.Task<FileResponse> Download()
        {
            return _implementation.DownloadAsync();
        }
    }
    
    public partial class FileResponse : System.IDisposable
    {
        private System.IDisposable _client; 
        private System.IDisposable _response; 

        public int StatusCode { get; private set; }

        public System.Collections.Generic.Dictionary<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.Dictionary<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() 
        {
            if (Stream != null)
                Stream.Dispose();
            if (_response != null)
                _response.Dispose();
            if (_client != null)
                _client.Dispose();
        }
    }

    #pragma warning restore
}

I think for controllers it should not generate FileResponse at all but just return a Stream - or is this not working in ASP.NET (Core)?