[C#] aspnetcore / server: Property type binary generates Stream instead of IFormFile
Created by: jnnwnk
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
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
- Use
openapi.yaml
to call command line. - Have a look at the generated file
FileUploadApiController
. - 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.