OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.35k stars 6.46k forks source link

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

Open jnnwnk opened 5 years ago

jnnwnk commented 5 years ago
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.

auto-labeler[bot] commented 5 years ago

👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

jwr456 commented 5 years ago

I had this issue as well. I worked around it by changing the generated model files from byte[] to IFormFile and then using the helper function below to convert to byte[]

    public byte[] getUploadedFile(IFormFile file)
    {
        byte[] result;
        using (var memoryStream = new MemoryStream())
        {
            if (file != null)
            {
                file.CopyTo(memoryStream);
                result = memoryStream.ToArray();
            }
            else
                result = null;
        }
        return result;
    }
mdulisch commented 2 years ago

@jwr456 could you please also provide the change of the model mustache for your workaround? Iam facing for this problem as well right now and have no idea how to solve it. Thanks

sdgirard commented 7 months ago

Hello guys, I'm having the same issue for generating the csharp client for Upload rest endpoint. I was wondering if this has been resolve, if so how should one correctly generate a csharp client object that uses IFormFile in the Dot 7 framework?