RicoSuter / NSwag

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

Handling Multiple File Uploads in WebAPI2 #2048

Open JakeStevenson opened 5 years ago

JakeStevenson commented 5 years ago

I've added an operation processor/attribute pair to allow for single file-uploads on my WebAPI2 endpoints without issue based off code I found from https://stackoverflow.com/questions/54348470/nswag-wep-api-2-multipart-form-data-attribute-file-upload

I think I was able to write another processor/attribute pair that generates a swagger.json that will accept an array of files as well. Here is what a sample of that swagger definition looks like:

"/api/job/{jobId}/list/{moduleId}/documents/Add": { "post": { "tags": [ "Document" ], "operationId": "Document_Add", "parameters": [ { "type": "string", "name": "moduleId", "in": "path", "required": true, "format": "guid", "x-nullable": false }, { "type": "string", "name": "jobId", "in": "path", "required": true, "format": "guid", "x-nullable": false }, { "name": "documentInfo", "in": "body", "required": true, "schema": { "$ref": "#/definitions/Document" }, "x-nullable": true }, { "type": "array", "name": "files", "in": "formData", "description": "file", "collectionFormat": "multi", "items": { "type": "file" } } ], "responses": { "200": { "x-nullable": true, "description": "", "schema": { "$ref": "#/definitions/JobCustomModuleDocument" } } } } },

SwaggerUI seems to be doing the right thing and gives me a UI that allows me to select multiple files before POSTing.

TypeScriptClientGenerator is... OK. It adds a parameter to the client call that accepts an array of type "any" (instead of the FileParam that it did with a single file).

Neither of them actually seems to add the files to the multi-part form data. Chrome just shows the request payload as having something like "files=%5Bobject%20File%5D&files=%5Bobject%20File%5D".

Is my swagger definition missing something? Or is this an issue with the UI and TypeScript Generators?

RicoSuter commented 5 years ago

This is probably not supported/not implemented. Needs further investigation...