Nswag: OpenAPI v3 multipart/form-data

Created on 23 Sep 2019  路  2Comments  路  Source: RicoSuter/NSwag

Given the following OpenAPI v3 path:

  /multipart_file_upload:
    post:
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                file:
                  type: string
                  format: binary
      responses:
        200:
          description: File uploaded

It would be expected that the generated C# client would allow specifying a filename and data source for the file upload, with MultipartFormDataContent being used under the hood. However, the only parameter that is generated is System.IO.Stream body, which does not perform any sort of multipart encoding.

OpenAPI 3 bug

Most helpful comment

I'm also having this issue with any IFormFile inputs on the controller. This worked fine with the Swagger 2.0 spec.

When generating a client for a multipart_file_upload using Swagger 2.0 spec a FileParameter(Stream data, string fileName, string contentType) would be generated and then within the client the following code would be generated for handling the boundaries:

var boundary_ = System.Guid.NewGuid().ToString();
var content_ = new System.Net.Http.MultipartFormDataContent(boundary_);
content_.Headers.Remove("Content-Type");
content_.Headers.TryAddWithoutValidation("Content-Type", "multipart/form-data; boundary=" + boundary_);
if (file != null)
{
    var content_file_ = new System.Net.Http.StreamContent(file.Data);
    if (!string.IsNullOrEmpty(file.ContentType))
         content_file_.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse(file.ContentType);
    content_.Add(content_file_, "file", file.FileName ?? "file");
}

Where file is of type FileParamter. This does not happen when generating code for the same endpoint using OpenApi3, instead, we do not generate a FileParameter class and pass in a Stream directly to the client api method. This means that the receiving server responds with a "missing content-type boundary".

Let me know if I can provide any more information.

All 2 comments

I think this is a known problem and there are already open issues for this.

I'm also having this issue with any IFormFile inputs on the controller. This worked fine with the Swagger 2.0 spec.

When generating a client for a multipart_file_upload using Swagger 2.0 spec a FileParameter(Stream data, string fileName, string contentType) would be generated and then within the client the following code would be generated for handling the boundaries:

var boundary_ = System.Guid.NewGuid().ToString();
var content_ = new System.Net.Http.MultipartFormDataContent(boundary_);
content_.Headers.Remove("Content-Type");
content_.Headers.TryAddWithoutValidation("Content-Type", "multipart/form-data; boundary=" + boundary_);
if (file != null)
{
    var content_file_ = new System.Net.Http.StreamContent(file.Data);
    if (!string.IsNullOrEmpty(file.ContentType))
         content_file_.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse(file.ContentType);
    content_.Add(content_file_, "file", file.FileName ?? "file");
}

Where file is of type FileParamter. This does not happen when generating code for the same endpoint using OpenApi3, instead, we do not generate a FileParameter class and pass in a Stream directly to the client api method. This means that the receiving server responds with a "missing content-type boundary".

Let me know if I can provide any more information.

Was this page helpful?
0 / 5 - 0 ratings