public static void AddSwagger(this IServiceCollection services) { services.AddSwaggerGen(options => { options.SwaggerDoc("v1", new OpenApiInfo { Title = "Arya.API", Version = "v1", Description = "API REST criada com o ASP.NET Core", TermsOfService = new Uri("https://github.com/lucasluizss"), Contact = new OpenApiContact { Name = "Lucas Silva", Email = string.Empty, Url = new Uri("https://twitter.com/lucasluizss"), }, License = new OpenApiLicense { Name = "MIT", Url = new Uri("https://github.com/lucasluizss/Arya.API/blob/master/LICENSE"), } }); options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme { Description = @$ "JWT Authorization header using the Bearer scheme. {Environment.NewLine} Enter 'Bearer' [space] and then your token in the text input below. {Environment.NewLine} Example: Bearer 12345abcdef", Name = "Authorization", In = ParameterLocation.Header, Type = SecuritySchemeType.ApiKey, Scheme = "Bearer" }); options.AddSecurityRequirement(new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Bearer" }, Scheme = "oauth2", Name = "Bearer", In = ParameterLocation.Header }, new List <string>() } }); var appPath = PlatformServices.Default.Application.ApplicationBasePath; var appName = PlatformServices.Default.Application.ApplicationName; var xmlDocPath = Path.Combine(appPath, $"{appName}.xml"); options.IncludeXmlComments(xmlDocPath); });
/// <summary> /// Configure swaager for api project /// </summary> /// <param name="services"></param> /// <param name="configuration"></param> /// <param name="version">Version</param> /// <param name="title">Documentation Title</param> /// <param name="description">Documentation description</param> /// <returns></returns> public static IServiceCollection AddSwaggerDoc(this IServiceCollection services, IConfiguration configuration, string version, string title, string description) { var appSettings = configuration.GetSection(nameof(AppSettings)).Get <AppSettings>(); services.AddSwaggerGen(c => { c.OrderActionsBy((apiDesc) => $"{apiDesc.ActionDescriptor.RouteValues["controller"]}_{apiDesc.HttpMethod}"); c.SwaggerDoc(version, new OpenApiInfo { Title = title, Version = version, License = new OpenApiLicense { Name = "Microsoft Licence", Url = new Uri("https://automedsys.net/licence"), }, Description = description }); c.SchemaFilter <EnumSchemaFilter>(); //c.DescribeAllEnumsAsStrings(); c.AddSecurityDefinition(appSettings.Scheme, new OpenApiSecurityScheme { Description = @$ "JWT Authorization header using the Bearer scheme. {Environment.NewLine}Enter 'Bearer' [space] and then your token in the text input below. {Environment.NewLine}Example: 'Bearer 12345abcdef'", Name = "Authorization", In = ParameterLocation.Header, Type = SecuritySchemeType.ApiKey, Scheme = appSettings.Scheme }); c.AddSecurityRequirement(new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = appSettings.Scheme }, Scheme = "oauth2", Name = appSettings.Scheme, In = ParameterLocation.Header }, new List <string>() } }); var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); c.IncludeXmlComments(xmlPath); c.EnableAnnotations(); });