| | | 1 | | using Asp.Versioning.ApiExplorer; |
| | | 2 | | using Microsoft.Extensions.Options; |
| | | 3 | | using Microsoft.OpenApi.Models; |
| | | 4 | | using Swashbuckle.AspNetCore.SwaggerGen; |
| | | 5 | | |
| | | 6 | | namespace WebApiVersioning; |
| | | 7 | | |
| | 1 | 8 | | public class ConfigureSwaggerOptions(IApiVersionDescriptionProvider provider) |
| | | 9 | | : IConfigureNamedOptions<SwaggerGenOptions> |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Configure each API discovered for Swagger Documentation |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="options"></param> |
| | | 15 | | public void Configure(SwaggerGenOptions options) |
| | 1 | 16 | | { |
| | | 17 | | // add swagger document for every API version discovered |
| | 7 | 18 | | foreach (var description in provider.ApiVersionDescriptions) |
| | 2 | 19 | | { |
| | 2 | 20 | | options.SwaggerDoc( |
| | 2 | 21 | | description.GroupName, |
| | 2 | 22 | | CreateVersionInfo(description)); |
| | 2 | 23 | | } |
| | 1 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Configure Swagger Options. Inherited from the Interface |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="name"></param> |
| | | 30 | | /// <param name="options"></param> |
| | | 31 | | public void Configure(string? name, SwaggerGenOptions options) |
| | 1 | 32 | | { |
| | 1 | 33 | | Configure(options); |
| | 1 | 34 | | } |
| | | 35 | | |
| | | 36 | | |
| | | 37 | | |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Create information about the version of the API |
| | | 41 | | /// </summary> |
| | | 42 | | /// <param name="description"></param> |
| | | 43 | | /// <returns>Information about the API</returns> |
| | | 44 | | private OpenApiInfo CreateVersionInfo( |
| | | 45 | | ApiVersionDescription description) |
| | 2 | 46 | | { |
| | 2 | 47 | | var info = new OpenApiInfo() |
| | 2 | 48 | | { |
| | 2 | 49 | | Title = ".NET Web API Versioning", |
| | 2 | 50 | | Version = description.ApiVersion.ToString() |
| | 2 | 51 | | }; |
| | | 52 | | |
| | 2 | 53 | | if (description.IsDeprecated) |
| | 0 | 54 | | { |
| | 0 | 55 | | info.Description += " This API version has been deprecated. Please use one of the new APIs available from th |
| | 0 | 56 | | } |
| | | 57 | | |
| | 2 | 58 | | return info; |
| | 2 | 59 | | } |
| | | 60 | | } |