Nswag: IFormFile issue

Created on 4 Nov 2018  路  2Comments  路  Source: RicoSuter/NSwag

Using v11.20.1 & aspnet core 2.1 in a controller marked with [ApiController], I have the following:

C#
```c#
[HttpPost, Route("save")]
public ActionResult SaveVenue(SaveVenueRequest request, IFormFile logo)
{
}


Swagger generated from the Swagger spec method
```json
    "/api/Venue/save": {
      "post": {
        "tags": [
          "Venue"
        ],
        "operationId": "Venue_SaveVenue",
        "consumes": [
          "multipart/form-data"
        ],
        "produces": [
          "text/plain",
          "application/json",
          "text/json"
        ],
        "parameters": [
          {
            "name": "request",
            "in": "body",
            "required": true,
            "schema": {
              "$ref": "#/definitions/SaveVenueRequest"
            },
            "x-nullable": false
          },
          {
            "type": "file",
            "name": "logo",
            "in": "formData",
            "x-nullable": true
          }
        ],
        "responses": {
          "200": {
            "x-nullable": true,
            "description": "",
            "schema": {
              "$ref": "#/definitions/SaveVenueResponse"
            }
          }
        }
      }
    },

Which generates this in TS:

    saveVenue(request: SaveVenueRequest, logo: FileParameter | null | undefined): Observable<SaveVenueResponse | null> {
        let url_ = this.baseUrl + "/api/Venue/save";
        url_ = url_.replace(/[?&]$/, "");

        const content_ = new FormData();
        if (logo !== null && logo !== undefined)
            content_.append("logo", logo.data, logo.fileName ? logo.fileName : "logo");

        let options_ : any = {
            body: content_,
            observe: "response",
            responseType: "blob",
            headers: new HttpHeaders({
                "Content-Type": "multipart/form-data", 
                "Accept": "application/json"
            })
        };

        return this.http.request("post", url_, options_).pipe(_observableMergeMap((response_ : any) => {
            return this.processSaveVenue(response_);
        })).pipe(_observableCatch((response_: any) => {
            if (response_ instanceof HttpResponseBase) {
                try {
                    return this.processSaveVenue(<any>response_);
                } catch (e) {
                    return <Observable<SaveVenueResponse | null>><any>_observableThrow(e);
                }
            } else
                return <Observable<SaveVenueResponse | null>><any>_observableThrow(response_);
        }));
    }

Issues:

  • The SaveVenueRequest is not being added into content_ by the generated code.
  • Asp freaks out with the multipart/form-data content-type header (500 status code).

What I have tried:

  • Removing the content-type header.
  • Manually adding the request to the FormData
content_.append('request', JSON.stringify(request));
  • Adding [FromForm] to prevent a 400 status code:
    c# [HttpPost, Route("save")] public ActionResult<SaveVenueResponse> SaveVenue([FromForm]SaveVenueRequest request, IFormFile logo)
  • Result: I do not get 400 or 500 errors. The logo IFormFile is not null if sent. The request is _always_ null (not deserializing).

Expectations:

  • The generated code should work properly
  • The model and the file should be sent correctly to the controller
  • The model should be deserialized correctly
  • The form should be set (if sent and not null)

Is there something that I am missing?

NSwag.CodeGeneration.TypeScript bug

All 2 comments

Looks like a bug/or not yet implemented feature...

Closing this issue.

```c#
[HttpPost, Route("branding")]
public ActionResult SaveBranding([FromForm]SaveVenueBranding request)
{


And then inside the class have something like:
```c#
    public class SaveVenueBranding {
        public List<IFormFile> Files { get; set; }
        public int VenueId { get; set; }
    }

Typescript code is generated properly.

Nswag version: 12.0.4

Was this page helpful?
0 / 5 - 0 ratings