67 lines
2.4 KiB
C#
67 lines
2.4 KiB
C#
using Asp.Versioning.ApiExplorer;
|
|
using Microsoft.Extensions.Options;
|
|
using Microsoft.OpenApi.Any;
|
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
|
using Swashbuckle.AspNetCore.SwaggerUI;
|
|
|
|
namespace Core.Inventory.DAL.API.Extensions
|
|
{
|
|
public static class SwaggerExtensions
|
|
{
|
|
public static void AddSwagger(this IServiceCollection services)
|
|
{
|
|
services.AddEndpointsApiExplorer();
|
|
services.AddSwaggerGen();
|
|
services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerOptions>();
|
|
}
|
|
|
|
public static void ConfigureSwagger(this WebApplication app)
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI(options =>
|
|
{
|
|
foreach (var version in app.DescribeApiVersions().Select(version => version.GroupName))
|
|
options.SwaggerEndpoint($"/swagger/{version}/swagger.json", version);
|
|
|
|
options.DisplayRequestDuration();
|
|
options.EnableTryItOutByDefault();
|
|
options.DocExpansion(DocExpansion.None);
|
|
});
|
|
}
|
|
|
|
public static IServiceCollection AddVersioning(this IServiceCollection services)
|
|
{
|
|
services.AddApiVersioning(options => options.ReportApiVersions = true)
|
|
.AddApiExplorer(options =>
|
|
{
|
|
options.GroupNameFormat = "'v'VVV";
|
|
options.SubstituteApiVersionInUrl = true;
|
|
});
|
|
return services;
|
|
}
|
|
|
|
public class ConfigureSwaggerOptions(IApiVersionDescriptionProvider provider) : IConfigureOptions<SwaggerGenOptions>
|
|
{
|
|
private readonly IApiVersionDescriptionProvider _provider = provider;
|
|
|
|
public void Configure(SwaggerGenOptions options)
|
|
{
|
|
foreach (var description in _provider.ApiVersionDescriptions)
|
|
options.SwaggerDoc(description.GroupName, new()
|
|
{
|
|
Title = AppDomain.CurrentDomain.FriendlyName,
|
|
Version = description.ApiVersion.ToString()
|
|
});
|
|
|
|
options.MapType<DateOnly>(() => new()
|
|
{
|
|
Format= "date",
|
|
Example = new OpenApiString(DateOnly.MinValue.ToString())
|
|
});
|
|
|
|
options.CustomSchemaIds(type => type.ToString().Replace("+", "."));
|
|
}
|
|
}
|
|
}
|
|
}
|