-
Notifications
You must be signed in to change notification settings - Fork 679
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
how to get swashbuckle to generate parameter type "form" for testing APIs that need a file upload? #120
Comments
I'm assuming your file upload implementation is similar to this - http://www.asp.net/web-api/overview/advanced/sending-html-form-data,-part-2. Because all the "file upload" specifics are happening at run-time, there is no way for Swashbuckle to automatically generate a Parameter description with the relevant "file upload" details. So, you need to tell it about this specific parameter. This can be easily done with a custom IOperationFilter which you can wire up as follows: AddFileParamTypes.cs
SwaggerConfig.cs
|
Looks like public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
{
if (operation.operationId == "FileUpload_PostFormData") // controller and action name
{
operation.consumes.Add("multipart/form-data");
operation.parameters = new List<Parameter>
{
new Parameter
{
name = "file",
required = true,
type = "file",
}
};
}
} |
Any ideas how to get an upload file button working in Ahoy (Swagger for ASP.NET Core). The above code does not do the trick. I raised the following issue domaindrivendev/Swashbuckle.AspNetCore#193 |
Using an attriubute so it can be added to more controllers public class ImportFileParamType : IOperationFilter
API Controller` [Route("ImageUpload")]
|
Thank you @glyons !
|
nice one @razonrus |
It would be nicer to do this via attribute on the method, rather than in a central place |
@AndrewBragdon I think one of the comments: #120 (comment) does exactly that |
I'm attempting to do what @glyons describes, but the code never seems to be executed when I simply add it as an attribute to the controller method. I ONLY created the attribute and decorated the controller with it, so if I was supposed to do something in the swaggerconfig, that may be what I'm missing. When I refactored it to be closer to what domaindrivendev suggested (including the swaggerconfig modification) then it works perfectly, but then I have to keep hardcoding a dependency on the operationID which I'd prefer not to do that. I appreciate anyone who can chime in, :) |
@xerikai see my commit, that is exactly what @glyons describes + @razonrus suggestion. |
Perfect, based on glyons I had put my sealed class swaggerformattribute inside the importfileparamtype class, after I looked at your checkin, I moved it out and everything started working. Thank you for the effort! |
senhores boa noite
a chamada fica assim
|
@RenanCarlosPereira your solution doesn't work when the IFormFile is tagged with [FromForm], but I've yet to figure out why. My knowledge of Swashbuckle is quite small. |
This is a bit hacky, @theBoringCoder , but it seems to be having the desired result when using
|
I've been trying to figure this out for the last few days. I am writing an API that uploads a file (based it a upload file tutorial on the asp.net/web-api site) , but I cannot figure out how to list the file parameter in the signature so that swashbuckle can generate the appropriate parameter type for the swagger ui to render a "select a file" button instead of a text field. Is this a limitation to web api, swashbuckle or to .Net in general?
The text was updated successfully, but these errors were encountered: