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