2024-06-01 10:57:52 +03:00
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.ApiExplorer;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
2024-06-21 21:43:40 +03:00
|
|
|
|
using Microsoft.OpenApi.Models;
|
2024-07-05 01:35:19 +03:00
|
|
|
|
using Mirea.Api.Endpoint.Common.Services;
|
2024-06-01 10:57:52 +03:00
|
|
|
|
using Mirea.Api.Endpoint.Configuration.Swagger;
|
|
|
|
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
namespace Mirea.Api.Endpoint.Configuration.AppConfig;
|
|
|
|
|
|
|
|
|
|
public static class SwaggerConfiguration
|
|
|
|
|
{
|
|
|
|
|
public static IServiceCollection AddCustomSwagger(this IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
services.AddSwaggerGen(options =>
|
|
|
|
|
{
|
|
|
|
|
options.SchemaFilter<SwaggerExampleFilter>();
|
|
|
|
|
options.OperationFilter<SwaggerDefaultValues>();
|
|
|
|
|
var basePath = AppDomain.CurrentDomain.BaseDirectory;
|
|
|
|
|
|
2024-06-21 21:43:40 +03:00
|
|
|
|
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
|
|
|
|
{
|
|
|
|
|
In = ParameterLocation.Header,
|
|
|
|
|
Description = "Keep the JWT token in the field (Bearer token)",
|
|
|
|
|
Name = "Authorization",
|
|
|
|
|
Type = SecuritySchemeType.ApiKey
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
options.AddSecurityRequirement(new OpenApiSecurityRequirement
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
new OpenApiSecurityScheme
|
|
|
|
|
{
|
|
|
|
|
Reference = new OpenApiReference
|
|
|
|
|
{
|
|
|
|
|
Type = ReferenceType.SecurityScheme,
|
|
|
|
|
Id = "Bearer"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[]
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2024-07-05 01:57:34 +03:00
|
|
|
|
if (File.Exists(Path.Combine(basePath, "docs.xml")))
|
|
|
|
|
options.IncludeXmlComments(Path.Combine(basePath, "docs.xml"));
|
|
|
|
|
|
|
|
|
|
if (File.Exists(Path.Combine(basePath, "ApiDtoDocs.xml")))
|
|
|
|
|
options.IncludeXmlComments(Path.Combine(basePath, "ApiDtoDocs.xml"));
|
2024-06-01 10:57:52 +03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerOptions>();
|
|
|
|
|
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IApplicationBuilder UseCustomSwagger(this IApplicationBuilder app, IServiceProvider services)
|
|
|
|
|
{
|
|
|
|
|
app.UseSwagger();
|
|
|
|
|
app.UseSwaggerUI(options =>
|
|
|
|
|
{
|
2024-06-10 22:01:58 +03:00
|
|
|
|
options.InjectStylesheet("/css/swagger/SwaggerDark.css");
|
2024-06-01 10:57:52 +03:00
|
|
|
|
var provider = services.GetService<IApiVersionDescriptionProvider>();
|
|
|
|
|
|
|
|
|
|
foreach (var description in provider!.ApiVersionDescriptions)
|
|
|
|
|
{
|
|
|
|
|
var url = $"/swagger/{description.GroupName}/swagger.json";
|
|
|
|
|
var name = description.GroupName.ToUpperInvariant();
|
|
|
|
|
options.SwaggerEndpoint(url, name);
|
2024-07-05 01:35:19 +03:00
|
|
|
|
options.RoutePrefix = UrlHelper.GetSubPathSwagger.Trim('/');
|
2024-06-01 10:57:52 +03:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return app;
|
|
|
|
|
}
|
|
|
|
|
}
|