forked from dotnet/aspnet-api-versioning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigureSwaggerOptions.cs
89 lines (75 loc) · 3.17 KB
/
ConfigureSwaggerOptions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
namespace ApiVersioning.Examples;
using Asp.Versioning;
using Asp.Versioning.ApiExplorer;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Text;
/// <summary>
/// Configures the Swagger generation options.
/// </summary>
/// <remarks>This allows API versioning to define a Swagger document per API version after the
/// <see cref="IApiVersionDescriptionProvider"/> service has been resolved from the service container.</remarks>
public class ConfigureSwaggerOptions : IConfigureOptions<SwaggerGenOptions>
{
private readonly IApiVersionDescriptionProvider provider;
/// <summary>
/// Initializes a new instance of the <see cref="ConfigureSwaggerOptions"/> class.
/// </summary>
/// <param name="provider">The <see cref="IApiVersionDescriptionProvider">provider</see> used to generate Swagger documents.</param>
public ConfigureSwaggerOptions( IApiVersionDescriptionProvider provider ) => this.provider = provider;
/// <inheritdoc />
public void Configure( SwaggerGenOptions options )
{
// add a swagger document for each discovered API version
// note: you might choose to skip or document deprecated API versions differently
foreach ( var description in provider.ApiVersionDescriptions )
{
options.SwaggerDoc( description.GroupName, CreateInfoForApiVersion( description ) );
}
}
private static OpenApiInfo CreateInfoForApiVersion( ApiVersionDescription description )
{
var text = new StringBuilder( "An example application with OpenAPI, Swashbuckle, and API versioning." );
var info = new OpenApiInfo()
{
Title = "Example API",
Version = description.ApiVersion.ToString(),
Contact = new OpenApiContact() { Name = "Bill Mei", Email = "bill.mei@somewhere.com" },
License = new OpenApiLicense() { Name = "MIT", Url = new Uri( "https://opensource.org/licenses/MIT" ) }
};
if ( description.IsDeprecated )
{
text.Append( " This API version has been deprecated." );
}
if ( description.SunsetPolicy is SunsetPolicy policy )
{
if ( policy.Date is DateTimeOffset when )
{
text.Append( " The API will be sunset on " )
.Append( when.Date.ToShortDateString() )
.Append( '.' );
}
if ( policy.HasLinks )
{
text.AppendLine();
for ( var i = 0; i < policy.Links.Count; i++ )
{
var link = policy.Links[i];
if ( link.Type == "text/html" )
{
text.AppendLine();
if ( link.Title.HasValue )
{
text.Append( link.Title.Value ).Append( ": " );
}
text.Append( link.LinkTarget.OriginalString );
}
}
}
}
info.Description = text.ToString();
return info;
}
}