Nswag: FromForm parameters not supported with OAPI 3.0

Created on 28 Oct 2019  路  4Comments  路  Source: RicoSuter/NSwag

Parameters marked as [FromForm] don't appear in ReDoc when .AddOpenApiDocument() is used (work fine with .AddSwaggerDocument()).

OpenAPI 3 help wanted NSwag.SwaggerGeneration.AspNetCore enhancement

Most helpful comment

From what I'm experiencing, it looks like NSwag is generating wrong openapi specs for [FromForm] parameters. It looks like 2.0 output 馃

The following code

public async Task<IActionResult> UploadAttachment(
            [FromRoute][Required] string caseId,
            [FromForm] CaseAttachmentModel model)
        { /* impl */ }

public class CaseAttachmentModel
    {
        public string Description { get; set; }

        public IFormFile Contents { get; set; }
    }

Generates the following openapi spec:

"/api/case/{caseId}/attachments": {
      "post": {
        "tags": [
          "Attachments"
        ],
        "summary": "summary",
        "description": "description",
        "operationId": "Attachments_UploadAttachment",
        "parameters": [
          {
            "name": "caseId",
            "in": "path",
            "required": true,
            "description": "The case to attach to.",
            "schema": {
              "type": "string",
              "nullable": true
            },
            "x-position": 1
          },
          {
            "name": "Description",
            "in": "formData",
            "schema": {
              "type": "string",
              "nullable": true
            },
            "x-position": 2
          },
          {
            "type": "file",
            "name": "Contents",
            "in": "formData",
            "schema": {
              "type": "string",
              "format": "binary",
              "nullable": true
            },
            "nullable": true
          }
    }

From what I read in the openapi specs, "formData" is no longer supported in openapi 3.0.
A requestBody object should be used instead.

https://swagger.io/docs/specification/describing-request-body/
https://swagger.io/docs/specification/describing-request-body/multipart-requests/

All 4 comments

Probably FromForm is currently not generated in the OpenAPI 3 spec and this is not a ReDoc problem.

From what I'm experiencing, it looks like NSwag is generating wrong openapi specs for [FromForm] parameters. It looks like 2.0 output 馃

The following code

public async Task<IActionResult> UploadAttachment(
            [FromRoute][Required] string caseId,
            [FromForm] CaseAttachmentModel model)
        { /* impl */ }

public class CaseAttachmentModel
    {
        public string Description { get; set; }

        public IFormFile Contents { get; set; }
    }

Generates the following openapi spec:

"/api/case/{caseId}/attachments": {
      "post": {
        "tags": [
          "Attachments"
        ],
        "summary": "summary",
        "description": "description",
        "operationId": "Attachments_UploadAttachment",
        "parameters": [
          {
            "name": "caseId",
            "in": "path",
            "required": true,
            "description": "The case to attach to.",
            "schema": {
              "type": "string",
              "nullable": true
            },
            "x-position": 1
          },
          {
            "name": "Description",
            "in": "formData",
            "schema": {
              "type": "string",
              "nullable": true
            },
            "x-position": 2
          },
          {
            "type": "file",
            "name": "Contents",
            "in": "formData",
            "schema": {
              "type": "string",
              "format": "binary",
              "nullable": true
            },
            "nullable": true
          }
    }

From what I read in the openapi specs, "formData" is no longer supported in openapi 3.0.
A requestBody object should be used instead.

https://swagger.io/docs/specification/describing-request-body/
https://swagger.io/docs/specification/describing-request-body/multipart-requests/

@lordldx Your openapi spec is indeed using OpenApi v2, you should update Swagger to v3 to see the request body.
Here is the spec doc in v3

{
     "openapi": "3.0.1",
     "info": {      
       "version": "v1"
    },
    "paths": {
      "/api/Demo": {
        "post": {
          "tags": [
            "Demo"
          ],
          "parameters": [
            {
              "name": "caseId",
              "in": "path",
              "required": true,
              "schema": {
                "type": "string"
              }
            }
          ],
          "requestBody": {
            "content": {
              "multipart/form-data": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "Description": {
                      "type": "string",
                      "nullable": true
                    },
                    "Contents": {
                      "type": "string",
                      "format": "binary",
                      "nullable": true
                    }
                  }
                },
                "encoding": {
                  "Description": {
                    "style": "form"
                  },
                  "Contents": {
                    "style": "form"
                  }
                }
              }
            }
          },
          "responses": {
            "200": {
              "description": "Success"
            }
          }
        }
      }
    },
    "components": {
      "securitySchemes": {
        "oauth2": {
          "type": "apiKey",
          "description": "Standard Authorization header using the Bearer scheme. Example: \"bearer {token}\"",
          "name": "Authorization",
          "in": "header"
        }
      }
    }
  }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

p0wertiger picture p0wertiger  路  3Comments

akamyshanov picture akamyshanov  路  4Comments

jjohs picture jjohs  路  3Comments

saephraim picture saephraim  路  3Comments

RawsomeGH picture RawsomeGH  路  4Comments