Thursday, September 26, 2019

Working with swagger

Here is the way by which we can add swagger to dotnet core api and can customize it with our needs.

(1) To work with swagger search and add below package
Swashbuckle.AspNetCore

(2) Once this is added just to make sure to enter below line of codes under ConfigureServices method in Startup.cs
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "Api", Version = "v1", Description = "" });
c.OperationFilter();// This will be your step - (4)
var xmlFile = "swagger.XML";
        var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
});

and add below line of code under Configure method in Startup.cs
app.UseSwagger();

app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("../swagger/v1/swagger.json", "API V1");
});

(3) swagger.xml file will contain
<doc>
  <assembly>
    <name>Api</name>
  </assembly>
 <members>
    <member name="T:Namespace of member">
      <summary>
        Summary of function
     </summary>
    </member>    
</doc>

(4) Adding SwaggerHeaderFilter
public class SwaggerHeaderFilter : IOperationFilter
{

public void Apply(Swashbuckle.AspNetCore.Swagger.Operation operation, OperationFilterContext context)
{
if (operation.Parameters == null)
                operation.Parameters = new List();

IList filterDescriptors = context.ApiDescription.ActionDescriptor.FilterDescriptors;
            bool isAuthorized = filterDescriptors.Select(filterInfo => filterInfo.Filter).Any(filter => filter is AuthenticationApiAttribute);

if (isAuthorized)
            {
// This will add parameters to each endpoint of passing access token
                operation.Parameters.Add(new NonBodyParameter
                {
                    Name = "X-Auth-Token",
                    In = "header",
                    Type = "string",
                    Required = true,
                    Description = "access token"
                });

// This will add parameters to each endpoint of passing access token
                operation.Parameters.Add(new NonBodyParameter
                {
                    Name = "UserId",
                    In = "header",
                    Type = "integer",
                    Required = true,
                    Description = "user id"
                });
            }

            if (String.IsNullOrWhiteSpace(operation.Description))
                operation.Description = "**Action Name**: " + ((Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)context.ApiDescription.ActionDescriptor).ActionName;
            else
                operation.Description = "**Action Name**: " + ((Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)context.ApiDescription.ActionDescriptor).ActionName + "  " + operation.Description;

}
}

This way you can configure your swagger with API and for each of the endpoint it will have access token and user id. You can add more parameters like this if you require. In step - (4), attribute created requires to register in step - (2).

No comments:

Post a Comment