RicoSuter / NSwag

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

CSharpClientGenerator: IFormFileCollection support broken #3622

Open mm3141 opened 3 years ago

mm3141 commented 3 years ago

We have an endpoint like this:

[HttpPost]
public Task<int> x(
    [FromQuery] int? x1,
    [FromQuery] string x2,
    [FromQuery] string x3,
    [FromQuery] Enum x4,
    IFormFileCollection file,
    [FromServices] DbContext context)

NSwag 13.11.3 used to generate something like this

public async System.Threading.Tasks.Task<int> x(int? x1, string x2, string x3, Enum? x4, System.Collections.Generic.IEnumerable<FileParameter> file, System.Threading.CancellationToken cancellationToken)
.
.
.
if (file != null)
{
    foreach (var item_ in file)
    {
        var content_file_ = new System.Net.Http.StreamContent(item_.Data);
        if (!string.IsNullOrEmpty(item_.ContentType))
            content_file_.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse(item_.ContentType);
        content_.Add(content_file_, "file", item_.FileName ?? "file");
    }
}

whereas for 13.13.2 we have

public async System.Threading.Tasks.Task<int> x(int? x1, string x2, string x3, Enum? type, System.Collections.Generic.IEnumerable<object> file, System.Threading.CancellationToken cancellationToken)
.
.
.
if (file != null)
{
    foreach (var item_ in file)
    {
        content_.Add(new System.Net.Http.StringContent(ConvertToString(item_, System.Globalization.CultureInfo.InvariantCulture)), "file");
    }
}

effectively breaking the file naming support here.

Any idea what is the reason for that? Any additional settings required?

booradlus commented 3 years ago

I am also experiencing the same issue with 13.13.2 and can confirm this was working in 13.11.3, with a signature as simple as [HttpPost("upload")] public ActionResult Upload([FromForm] IFormFileCollection files) { return Ok(); }

mm3141 commented 2 years ago

Still not working in 13.15.5

mm3141 commented 2 years ago

This happens only with the OpenApi3 schema type, not with the Swagger2 schema type. Maybe it is not supported, idk.

booradlus commented 2 years ago

I ran into this issue as well. With 13.15.10 building against net60, it generated the IEnumerable<object> type for the files parameter.

I was able to fix the problem by switching to List<IFormFile> which restored the IEnumerable<FileParameter> signature in the generated client.

This swapping between a FormFileCollection and List<IFormFile> didn't work when I tried it in earlier versions of nswag (13.10.7).