Swagger for Fileupload

What is Swagger?

Swagger is an documentation tool for REST APIs. Swagger(open source project called Swashbuckle) can easily be integrated with WEB API project and work as a client to call the REST APIs. No need to use third party tools like fiddler, postman to test the WEB APIs.

For File upload WEB API methods, the swagger ui shows multiple parameters to pass as input to test.

These values are properties of IFormFile(Microsoft.AspNet.Http / Microsoft.AspNetCore.Http) which represent the file upload.

SW

In swagger – swash buckle we have the IOperationFilter – Which allows us to modify the Post operations descriptions.

Sample usage : lets add class which implements the IOperationFilter(Swashbuckle.SwaggerGen.Generator) and then have to implement the Apply method. Below the code snippet.

we have to include all the method parameters in Operation.parameters list. In below method having two parameters.

public class FileUploadOperation : IOperationFilter
    {
        public void Apply(Operation operation, OperationFilterContext context)
        {
            // Note: OperationId = api + [MethodName] + [HTTP verb]
            if (operation.OperationId.ToLower() == "apiuploadsavefilepost")
            {
                operation.Parameters.Clear();
                operation.Parameters.Add(new NonBodyParameter
                {
                    Name = "id",
                    In = "formData",
                    Description = "Id",
                    Required = true,
                    Type = "long"
                }); 
               operation.Parameters.Add(new NonBodyParameter
                {
                    Name = "file",
                    In = "formData",
                    Description = "Upload File",
                    Required = true,
                    Type = "file"
                });
                operation.Consumes.Add("multipart/form-data");
            }
        }
    }

Next we have to register the operation filter in startup.cs.

services.ConfigureSwaggerGen(options =>
           {
               options.SingleApiVersion(new Info
               {
                   Version = "v1",
                   Title = "ABC",
                   Description = "WEB APIs",
                   TermsOfService = "None"
               });
               //options.IncludeXmlComments(pathToDoc);
               options.DescribeAllEnumsAsStrings();
               options.OperationFilter<FileUploadOperation>();
           });

Then on next run the upload method you will see like below in swagger ui and we can easily select file for upload method test.

SW1

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s