84 lines
2.7 KiB
C#
84 lines
2.7 KiB
C#
using Microsoft.AspNetCore.Mvc.ApiExplorer;
|
|
using Microsoft.Extensions.Options;
|
|
using Microsoft.OpenApi.Models;
|
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
|
using System.Reflection;
|
|
|
|
namespace BMA.EHR.MetaData.Service
|
|
{
|
|
public class ConfigureSwaggerOptions : IConfigureNamedOptions<SwaggerGenOptions>
|
|
{
|
|
private readonly IApiVersionDescriptionProvider _provider;
|
|
|
|
public ConfigureSwaggerOptions(
|
|
IApiVersionDescriptionProvider provider)
|
|
{
|
|
_provider = provider;
|
|
}
|
|
|
|
public void Configure(SwaggerGenOptions options)
|
|
{
|
|
// add swagger document for every API version discovered
|
|
foreach (var description in _provider.ApiVersionDescriptions)
|
|
{
|
|
options.EnableAnnotations();
|
|
|
|
options.SwaggerDoc(
|
|
description.GroupName,
|
|
CreateVersionInfo(description));
|
|
}
|
|
|
|
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
|
{
|
|
In = ParameterLocation.Header,
|
|
Description = "Please enter a valid token",
|
|
Name = "Authorization",
|
|
Type = SecuritySchemeType.Http,
|
|
BearerFormat = "JWT",
|
|
Scheme = "Bearer"
|
|
});
|
|
|
|
options.AddSecurityRequirement(new OpenApiSecurityRequirement
|
|
{
|
|
{
|
|
new OpenApiSecurityScheme
|
|
{
|
|
Reference = new OpenApiReference
|
|
{
|
|
Type = ReferenceType.SecurityScheme,
|
|
Id = "Bearer"
|
|
}
|
|
},
|
|
new string[]{}
|
|
}
|
|
});
|
|
|
|
// generate the XML docs that'll drive the swagger docs
|
|
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
|
|
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
|
|
options.IncludeXmlComments(xmlPath);
|
|
}
|
|
|
|
public void Configure(string name, SwaggerGenOptions options)
|
|
{
|
|
Configure(options);
|
|
}
|
|
|
|
private OpenApiInfo CreateVersionInfo(
|
|
ApiVersionDescription desc)
|
|
{
|
|
var info = new OpenApiInfo()
|
|
{
|
|
Title = "BMA EHR Metadata Service Document",
|
|
Version = desc.ApiVersion.ToString()
|
|
};
|
|
|
|
if (desc.IsDeprecated)
|
|
{
|
|
info.Description += " This API version has been deprecated. Please use one of the new APIs available from the explorer.";
|
|
}
|
|
|
|
return info;
|
|
}
|
|
}
|
|
}
|