swagger-api / swagger-api/swagger-codegen

[aspnetcore] Bug generating method in Asp.Net Core 2.2 Controller when action is File Upload (multipart/form-data) (OAS 3)

Open
#10,137 4 comments 1 reaction 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

Description
When trying to describe a File Upload action in OAS 3 YAML using multipart/form-data content (as described in OAS 3.0.1 specs), the generated aspnetcore code contains NO controller method at all.
(aspnetcore 2.2)

Swagger-codegen version
3.0.18 (using the "3rd Gen" online generator (https://generator3.swagger.io/index.html))

Declaration File
In the RestApi-multipart-formdata.yml, there is one path describing a POST action using the multipart/form-data content.

Options used for generation
(note: not including the spec object, for simplicity; see subsequent note in Repro Steps)

{
  "lang": "aspnetcore",
  "specURL": "string",
  "type": "SERVER",
  "codegenVersion": "V3",
  "options": {
    "additionalProperties": {
      "packageName": "Test.Lib",
      "packageVersion": "0.0.1",
      "useDateTimeOffset": true,
      "returnICollection": true,
      "aspnet-core-version": "2.2"
    }
  }
}

Steps to reproduce
❗️ For simplicity sake, I'm listing the easiest way to reproduce the issue. This is not the process we use "in real life" (but the results are the same).

  1. Copy the RestApi-multipart-formdata.yml contents into the online Swagger Editor
  2. Click on Generate Server drop-down menu.
  3. Select aspnetcore.
  4. Open the downloaded generated server content (zip file):
    1. Open the src\IO.Swagger\Controllers folder

    2. Inspect the CalcSessionsApi.cs file.
      ℹ️ I copied the contents of the CalcSessionsApi.cs file into a gist, for easier inspection.

    3. Notice there is no method generated.

Related issues/PRs
I was not able to find any related/similar issues already posted.

Additional Details
On a whim, I tried multipart\form-data (instead of multipart/form-data, as it's written in the OAS 3 spec) and at least something gets generated:

public virtual IActionResult CreateCalcSessionFromFile([FromBody]Body body)

Body.cs:

[DataContract]
public partial class Body : IEquatable<Body>
{
   /// <summary>
  /// Gets or Sets TheFile
  /// </summary>
  [Required]
  [DataMember(Name="theFile")]
  public byte[] TheFile { get; set; }

  // remaining contents elided
}

HOWEVER, if the above is an example of what will be produced, then there are additional issues that should be fixed (noted in Requested Fix(es) / Expected Results section).

Requested Fix(es) / Expected Results

  1. The generator should generate a controller method in the ASP.Net Core server code when the correct content-type (of multipart/form-data) is specified in the Swagger declaration file.
  2. When the multipart/form-data schema only declares one File property (using type: string and format: binary or format: base64), the method signature for the generated method should be (or very similar to) what it was in Swagger 2.0 (e.g.)
    public virtual IActionResult CreateCalcSessionFromFile([FromForm][Required()]System.IO.Stream theFile)
    
    1. Currently, when there is only one 'File' property (type: string and format: binary or format: base64) declared in the multipart/form-data schema, a new model (type) is generated for that property, which is unnecessary complexity and results in an incorrect method signature (the parameter is marked [FromBody] when it should be [FromForm] )

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 with the linked RestApi-multipart-formdata.yml and compare the generated src/IO.Swagger/Controllers/CalcSessionsApi.cs with the Body.cs example. Trace how the aspnetcore generator handles multipart/form-data in the OpenAPI 3 input, then use the requested controller signature and [FromForm] behavior as the completion criteria. Reproduce through the Swagger Editor or generator version 3.0.18 before checking the generated output.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.