Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File upload support is currently partially/incorrectly documented #1183

Closed
mikebeaton opened this issue Jul 1, 2019 · 3 comments
Closed

Comments

@mikebeaton
Copy link
Contributor

mikebeaton commented Jul 1, 2019

Hi,

Would a PR adding (very brief) documentation for the file upload support be welcomed?

At the moment:

  • The README.md still points the user to Swashbuckle.AspNetCore.Filters for file upload support, but in fact (on looking through the commit histories and issues of both projects - but unfortunately not the current README.md of either) file upload support has been in this project since 4.0, and hence has been removed from Swashbuckle.AspNetCore.Filters.

It would simplify tracking down how to use file uploads if that small piece of text in the README was removed.

  • IFormFile is indeed now supported by this project, with a clean file upload button, but this does not work if the IFormFile is marked [FromForm], which is counterintuitive (to me, at least!)

With IformFile file parameter:

image

With [FromForm]IFormFile file parameter:

image

It would be helpful if the requirement not to tag IFormFile with [FromForm] was documented, since I guess (?!) that this is something that maybe not just I am going to try by mistake. (At the same time, having this info, very briefly, would serve the additional purpose of clarifying what the user has to do to get a file upload up and running, right in the README.)

Does that sound sensible? Thanks!

@mikebeaton mikebeaton changed the title File upload support not documented File upload support is currently partially/incorrectly documented Jul 1, 2019
@domaindrivendev domaindrivendev added this to the Backlog milestone Sep 12, 2019
@domaindrivendev
Copy link
Owner

@mikebeaton makes sense. If you want to go ahead and create a PR for the docs update I'll be happy to merge. Thx

@dernop
Copy link

dernop commented Apr 2, 2020

Is this really just a documentation issue and not a bug? I was stuck on this problem for two days now and i was only able to solve it by "trial-and-error", when i eventually removed the [FromForm] attribute from the IFormFile parameter.
Since the ASP app works fine WITH this attribute and i thus consider it "legal" to do, i rather consider this a bug. It should also work with this attribute...

@domaindrivendev
Copy link
Owner

@dernop - while it may not prevent your API from working, it's technically incorrect to decorate IFormFile parameters with the [FromForm] attribute. If you take a look at the official MS docs on file uploads you'll see there's absolutely no mention of the attribute.

While the type and the attribute sound related, they're actually expressing two different concepts. The IFormFile type indicates to the ASP.NET Core model binder that the parameter should be deserialized from binary data in the request payload. However, when you decorate it with the [FromForm] attribute, you're indicating that it's indidividual properties should be populated from corresponding form fields in the request payload.

This distinction is aptly captured in the ApiParameterDescription.Source property, which is part of the metadata that's surfaced by ASP.NET Core to Swashbuckle. For binary data, this property should have the value FormFile, whereas for form fields, it should have the value Form. When you decorate the parameter with the [FromForm] attribute, ASP.NET Core assigns the value Form which is semantically incorrect, causing downstream problems for libraries like Swashbuckle.

The fact that the API still behaves as you expect, even with the incorrect use of the attribute, is an issue you should take up with ASP.NET Core team. Swashbuckle depends heavily on the metadata that's surfaced from ASP.NET Core, and if that metadata states that the model is deserialized from form-fields as opposed to binary data, then that's how Swashbuckle will describe it.

mikebeaton added a commit to mikebeaton/Swashbuckle.AspNetCore that referenced this issue Sep 24, 2020
mikebeaton added a commit to mikebeaton/Swashbuckle.AspNetCore that referenced this issue Sep 24, 2020
Blackclaws added a commit to Blackclaws/openapi-generator that referenced this issue Mar 18, 2021
wing328 pushed a commit to OpenAPITools/openapi-generator that referenced this issue Mar 22, 2021
…re (#9000)

* Aspnetcore fix file parameters

domaindrivendev/Swashbuckle.AspNetCore#1183 (comment)

* Update Samples

* Add Consumes to Controller. Needed for correct swashbuckle

* Add bodyParam annotation again

* Add InputFormatterStream

* Selective Use of IFormFIle

* Update samples

* Add missing List

* Fix duplicate consumes

* Update samples

* Remove executable bit

* Add missing files

* Add Comments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants