swagger-api / swagger-api/swagger-codegen

[CSHARP-NETCORE] ASPNET Core Endpoint taking List<IFormFile> argument causes CSHARP-NETCORE generated client to contain build errors

Open
#10,278 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Att: @jimschubert - Thanks for the preliminary discussion on slack!

Description

I have an ASPNET Core WebApi application which exposes an endpoint that takes List<IFormFile> files as argument:

        [HttpPost]
        public IActionResult PostFiles(List<IFormFile> files)
        {
            foreach(var file in files)
            {
                // do stuff
            }

            return Ok();
        }
Swagger-codegen version

Latest

Swagger declaration file content or url

The generated swagger json looks to be correct:

{
  "openapi": "3.0.1",
  "info": {
    "title": "My API",
    "version": "v1"
  },
  "paths": {
    "/WeatherForecast": {
      "post": {
        "tags": [
          "WeatherForecast"
        ],
        "requestBody": {
          "content": {
            "multipart/form-data": {
              "schema": {
                "type": "object",
                "properties": {
                  "files": {
                    "type": "array",
                    "items": {
                      "type": "string",
                      "format": "binary"
                    },
                    "nullable": true
                  }
                }
              },
              "encoding": {
                "files": {
                  "style": "form"
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Success"
          }
        }
      }
    }
  },
  "components": { }
}
Command line used for generation

docker run --rm -v c:\TEMP\FormFileBugRepro:/TEMP openapitools/openapi-generator-cli generate -i /TEMP/swagger.json -g csharp-netcore -o /TEMP/Swag

Steps to reproduce
  1. Create ASPNET Core WebApi
  2. Add Swagger dependencies and setup properly in Startup
  3. Create endpoint as specified in description
  4. Generate csharp-netcore client using the command specified above
  5. Open generated solution and see build errors:

build-error

  1. Even if you fix the build error by doing so:

image

  1. Its still going to try to add each file to the FileParameter dictionary under the same key, which throws an error. It seems as FileParameter being a dictionary type prevents implementing correct behavior as it's perfectly legal to attach multiple form files with the same name in a HTTP Request.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with the provided Docker generation command and the csharp-netcore generator. Inspect the generated client’s multipart/form-data handling for the files array and FileParameter dictionary. Done means the generated solution builds and multiple files can be sent under the same form key without an error.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, openapi
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.