OpenAPITools / OpenAPITools/openapi-generator

[C#] aspnetcore / server: Property type binary generates Stream instead of IFormFile

Open
#2,398 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Hacktoberfest help wanted Issue: Bug Server: C-Sharp
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

I try to generate a ASP .NET Core server side library for file uploads.
All examples I've seen so far are using the C# type IFormFile to receive file content.
However if I declare an interface with content type multipart/form-data and a property of type string with format binary (see yaml snippet) openapi generates a method parameter of type `` System.IO.Stream.
If I try uploading a file using this interface I am getting an exception:

System.InvalidOperationException: Could not create an instance of type 'System.IO.Stream'. Model bound complex types must not be abstract or value types and must have a parameterless constructor. Alternatively, give the 'image' parameter a non-null default value.
   at Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ComplexTypeModelBinder.CreateModel(ModelBindingContext bindingContext)
   at Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ComplexTypeModelBinder.BindModelCoreAsync(ModelBindingContext bindingContext)
   at Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder.BindModelAsync(ActionContext actionContext, IModelBinder modelBinder, IValueProvider valueProvider, ParameterDescriptor parameter, ModelMetadata metadata, Object value)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerBinderDelegateProvider.<>c__DisplayClass0_0.<<CreateBinderDelegate>g__Bind|0>d.MoveNext()

Am I missing something or is there any kind of workaround (make System.IO.Stream working or forcing openapi to use IFormFile)?

openapi-generator version

3.3.4

OpenAPI declaration file content or url

openapi.yaml

Command line used for generation

java -jar openapi-generator-cli-3.3.4.jar generate -i openapi.yaml -g aspnetcore --model-name-suffix DTO

Steps to reproduce
  1. Use openapi.yaml to call command line.
  2. Have a look at the generated file FileUploadApiController.
  3. If you want to reproduce the exception in action: Create a small implementation an try posting a file to http://127.0.0.1/api/upload
Related issues/PRs

There is a similar issue #1381 about problems with files on client side in C#.
And also one about generation of byte/binary body in C# aspdotnet service side: #1327

Suggest a fix/enhancement

In my opinion best / easiest would be if the generated method uses IFormFile.
Other options could be to make this some how configurable or to generate additional code to make Stream work out of the box. I came acrose an example suggested on stackoverflow to make Stream working, but I guess this is .NET Framework. Anyway I'v not tested it yet.

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 openapi.yaml and the generated FileUploadApiController produced by the aspnetcore generator command. Reproduce the generation with openapi-generator 3.3.4 and inspect how a multipart/form-data binary property becomes System.IO.Stream. Done means the generated upload endpoint uses a type that can successfully bind the posted file, with the behavior covered by an appropriate generator test if one is available.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, openapi
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.