using Microsoft.AspNetCore.Mvc.ApiExplorer; using Microsoft.Extensions.Options; using Microsoft.OpenApi.Models; using Swashbuckle.AspNetCore.SwaggerGen; namespace HeartTrackAPI.Utils; public class SwaggerOptions: IConfigureNamedOptions { private readonly IApiVersionDescriptionProvider _provider; public SwaggerOptions( IApiVersionDescriptionProvider provider) { _provider = provider; } /// /// Configure each API discovered for Swagger Documentation /// /// public void Configure(SwaggerGenOptions options) { // add swagger document for every API version discovered foreach (var description in _provider.ApiVersionDescriptions) { options.SwaggerDoc( description.GroupName, CreateVersionInfo(description)); } } /// /// Configure Swagger Options. Inherited from the Interface /// /// /// public void Configure(string? name, SwaggerGenOptions options) { Configure(options); } /// /// Create information about the version of the API /// /// /// Information about the API private OpenApiInfo CreateVersionInfo( ApiVersionDescription desc) { var info = new OpenApiInfo() { Title = "Web API For HeartTrack .NET 8", Version = desc.ApiVersion.ToString(), Description = "The HeartTrack project API, aims to provide an Open Source solution for heart rate data analysis.", Contact = new OpenApiContact { Name = "HeartTrackDev", Email = "toto@toto.fr" }, License = new OpenApiLicense { Name = "MIT", Url = new Uri("https://opensource.org/licenses/MIT") } }; if (desc.IsDeprecated) { info.Description += " This API version has been deprecated. Please use one of the new APIs available from the explorer."; } return info; } }