Nswag: C# Generated Client - Use IFormFileCollection instead of IEnumerable<byte[]>

Created on 20 May 2019  路  2Comments  路  Source: RicoSuter/NSwag

I'm trying to generate a C# client from a swagger doc. For a POST endpoint that accepts a collection of files, the C# client generated client is using an IEnumerable or IFormFileCollection parameter so that the file metadata can be passed along with the file bytes.

Using NSwagStudio v12.2.5.0

Sample swagger doc:

    "/api/v3/Services/WorkItem/Ticket/Attachment": {
      "post": {
        "tags": [
          "WorkItemTicket"
        ],
        "operationId": "WorkItemTicket_PostAttachment",
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "type": "integer",
            "name": "ticketId",
            "in": "query",
            "format": "int32",
            "x-nullable": false
          },
          {
            "type": "array",
            "name": "files",
            "in": "formData",
            "collectionFormat": "multi",
            "x-nullable": true,
            "items": {
              "type": "file"
            }
          }
        ],
        "responses": {
          "204": {
            "description": ""
          },
          "400": {
            "x-nullable": true,
            "description": "",
            "schema": {
              "$ref": "#/definitions/ValidationProblemDetails"
            }
          },
          "401": {
            "x-nullable": true,
            "description": "",
            "schema": {
              "$ref": "#/definitions/ProblemDetails"
            }
          },
          "403": {
            "x-nullable": true,
            "description": "",
            "schema": {
              "$ref": "#/definitions/ProblemDetails"
            }
          },
          "500": {
            "x-nullable": true,
            "description": "",
            "schema": {
              "$ref": "#/definitions/ProblemDetails"
            }
          },
          "200": {
            "description": ""
          }
        }
      }
    },

Sample C# generated client:

        public async System.Threading.Tasks.Task PostAttachmentAsync(int? ticketId = null, System.Collections.Generic.IEnumerable<byte[]> files = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
        {
            var urlBuilder_ = new System.Text.StringBuilder();
            urlBuilder_.Append("api/v3/Services/WorkItem/Ticket/Attachment?");
            if (ticketId != null) 
            {
                urlBuilder_.Append("ticketId=").Append(System.Uri.EscapeDataString(ConvertToString(ticketId, System.Globalization.CultureInfo.InvariantCulture))).Append("&");
            }
            urlBuilder_.Length--;
OpenAPI 3 bug

All 2 comments

It should be a FileParameter

Where can I find information or documentation on how to use FileParameter?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

akamyshanov picture akamyshanov  路  4Comments

alanedwardes picture alanedwardes  路  3Comments

Rui90 picture Rui90  路  4Comments

p0wertiger picture p0wertiger  路  3Comments

saephraim picture saephraim  路  3Comments