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)
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).
- Copy the RestApi-multipart-formdata.yml contents into the online Swagger Editor
- Click on
Generate Serverdrop-down menu. - Select
aspnetcore. - Open the downloaded generated server content (zip file):
-
Open the
src\IO.Swagger\Controllersfolder -
Inspect the
CalcSessionsApi.csfile.
ℹ️ I copied the contents of the CalcSessionsApi.cs file into a gist, for easier inspection. -
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
- 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. - When the
multipart/form-dataschemaonly declares oneFileproperty (usingtype: stringandformat: binaryorformat: 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)- Currently, when there is only one 'File' property (
type: stringandformat: binaryorformat: base64) declared in themultipart/form-dataschema, 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])
- Currently, when there is only one 'File' property (
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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