RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.69k stars 1.24k forks source link

Issue with IFormFile and IFormFileCollection using AddOpenApiDocument() #2259

Open skolmer opened 5 years ago

skolmer commented 5 years ago

Thanks for this great project! Unfortunately I have some issues with the Open API document generator in ASP.NET Core. If I use AddOpenApiDocument instead of AddSwaggerDocument, all IFormFile and IFormFileCollection parameters are exported as string or ICollection<string>. If I switch to AddSwaggerDocument the generated document has the parameter type file and the generated client code uses FileParameter instead of string, which is the expected behavior.

Is this a limitation in the OpenAPI Spec or the implementation or did I miss some configuration detail? thanks in advance.

Znow commented 4 years ago

We are facing a similar issue, but here FileParameter is generated when we use IFormFile in C#. What is FileParameter? it is no existing class anywhere...

jeremyVignelles commented 4 years ago

On the controller side, you declare a Stream or a IFormFile parameter (Stream + file name). The Stream class doesn't serialize correctly into json, so you can't use it on the client side, so NSwag generates a FileParameter class.

Znow commented 4 years ago

@jeremyVignelles The IFormFile is also generated as a FileParameter class... Which is nowhere to be found, not on any page on the internet. What is this class, why is it generated, and why can it not be found?

jeremyVignelles commented 4 years ago

If you can't serialize a Stream, you can't serialize something that contains a stream (IFormFile), so FileParameter is generated on your C# client file.

IFormFile -> FileParameter is the expected behavior, IFormFile -> string isn't.

@skolmer : Can you post a repro project somewhere? I've never seen such issue.

Znow commented 4 years ago

@jeremyVignelles That didnt quite answer my question: "What is this class, why is it generated, and why can it not be found?" FileParameter does not exist anywhere...

jeremyVignelles commented 4 years ago

I mean that the class is in your C# generated code (if you are using the C# generator). It is declared like this : https://www.github.com/RicoSuter/NSwag/tree/master/src%2FNSwag.CodeGeneration.CSharp%2FTemplates%2FFile.liquid#L32

It can't really be defined as such because:

As for the why it is being generated, I think I answered the question properly, but let me correct something. IFormFile is not serialized as JSON. That still stands:

Why can't you find it anywhere? because of the usual issue with many projects : documentation. Now the best thing you could do is contribute to the NSwag wiki page and explain that to others.

Now, please stop polluting the issue with your questions. Either come and chat on gitter, or open your own question. This discussion is unrelated to the initial topic.