Last active
November 22, 2020 07:44
-
-
Save sunilkumarmedium/c6768be4d14413b02b0966dbddbd6626 to your computer and use it in GitHub Desktop.
SwaggerConfig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace CleanArchitectureApp.WebApi.Extensions | |
{ | |
using Microsoft.AspNetCore.Builder; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.OpenApi.Models; | |
using System; | |
using System.Reflection; | |
public static class SwaggerConfig | |
{ | |
public static void AddSwaggerConfiguration(this IServiceCollection services) | |
{ | |
if (services == null) | |
{ | |
throw new ArgumentNullException(nameof(services)); | |
} | |
services.AddSwaggerGen(s => | |
{ | |
s.SwaggerDoc("v1", new OpenApiInfo | |
{ | |
Version = "v1", | |
Title = "Clean Architecture Application", | |
Description = "Clean Architecture Application Web API", | |
}); | |
s.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme | |
{ | |
Description = "Input the JWT like: Bearer {your token}", | |
Name = "Authorization", | |
Scheme = "Bearer", | |
BearerFormat = "JWT", | |
In = ParameterLocation.Header, | |
Type = SecuritySchemeType.ApiKey, | |
}); | |
s.AddSecurityRequirement(new OpenApiSecurityRequirement | |
{ | |
{ | |
new OpenApiSecurityScheme | |
{ | |
Reference = new OpenApiReference | |
{ | |
Type = ReferenceType.SecurityScheme, | |
Id = "Bearer", | |
}, | |
}, | |
Array.Empty<string>() | |
}, | |
}); | |
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; | |
var xmlPath = System.IO.Path.Combine(AppContext.BaseDirectory, xmlFile); | |
s.IncludeXmlComments(xmlPath, includeControllerXmlComments: true); | |
}); | |
} | |
public static void UseSwaggerSetup(this IApplicationBuilder app) | |
{ | |
if (app == null) | |
{ | |
throw new ArgumentNullException(nameof(app)); | |
} | |
app.UseSwagger(); | |
app.UseSwaggerUI(c => | |
{ | |
c.SwaggerEndpoint("/swagger/v1/swagger.json", "v1"); | |
c.RoutePrefix = string.Empty; | |
c.DefaultModelsExpandDepth(-1); | |
}); | |
} | |
public static void AddApiVersioningExtension(this IServiceCollection services) | |
{ | |
services.AddApiVersioning(config => | |
{ | |
// Specify the default API Version as 1.0 | |
config.DefaultApiVersion = new ApiVersion(1, 0); | |
// If the client hasn't specified the API version in the request, use the default API version number | |
config.AssumeDefaultVersionWhenUnspecified = true; | |
// Advertise the API versions supported for the particular endpoint | |
config.ReportApiVersions = true; | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment