| | 1 | | using System.Text; |
| | 2 | | using Asp.Versioning; |
| | 3 | | using Asp.Versioning.ApiExplorer; |
| | 4 | | using Microsoft.Extensions.Options; |
| | 5 | | using Microsoft.OpenApi.Models; |
| | 6 | | using Swashbuckle.AspNetCore.SwaggerGen; |
| | 7 | |
|
| | 8 | | namespace Gateway.Swagger; |
| | 9 | |
|
| 0 | 10 | | public class ConfigureSwaggerOptions( |
| 0 | 11 | | IApiVersionDescriptionProvider provider, |
| 0 | 12 | | IOptions<ServiceMetadata> serviceMetaData |
| 0 | 13 | | ) : IConfigureOptions<SwaggerGenOptions> |
| | 14 | | { |
| | 15 | | private const string BreakLine = "<br />"; |
| | 16 | |
|
| | 17 | | public void Configure(SwaggerGenOptions options) |
| | 18 | | { |
| 0 | 19 | | foreach (var description in provider.ApiVersionDescriptions.Reverse()) |
| | 20 | | { |
| 0 | 21 | | options.SwaggerDoc(description.GroupName, CreateInfoForApiVersion(description)); |
| | 22 | | } |
| 0 | 23 | | } |
| | 24 | |
|
| | 25 | | private OpenApiInfo CreateInfoForApiVersion(ApiVersionDescription description) |
| | 26 | | { |
| 0 | 27 | | var text = new StringBuilder(serviceMetaData.Value.Description); |
| 0 | 28 | | text.Append(BreakLine); |
| 0 | 29 | | text.Append($"[Link to BluePrint]({serviceMetaData.Value.BluePrintUri})"); |
| 0 | 30 | | text.Append(BreakLine); |
| | 31 | |
|
| 0 | 32 | | var info = new OpenApiInfo() |
| 0 | 33 | | { |
| 0 | 34 | | Title = serviceMetaData.Value.ServiceName, |
| 0 | 35 | | Version = description.ApiVersion.ToString(), |
| 0 | 36 | | Contact = new OpenApiContact() |
| 0 | 37 | | { |
| 0 | 38 | | Name = serviceMetaData.Value.ContactName, |
| 0 | 39 | | Email = serviceMetaData.Value.ContactEmail |
| 0 | 40 | | }, |
| 0 | 41 | | License = new OpenApiLicense() |
| 0 | 42 | | { |
| 0 | 43 | | Name = serviceMetaData.Value.License, |
| 0 | 44 | | Url = serviceMetaData.Value.LicenseUri |
| 0 | 45 | | }, |
| 0 | 46 | | TermsOfService = serviceMetaData.Value.TermsOfService |
| 0 | 47 | | }; |
| | 48 | |
|
| 0 | 49 | | if (description.IsDeprecated) |
| | 50 | | { |
| 0 | 51 | | text.Append(BreakLine); |
| 0 | 52 | | text.Append("This API version has been deprecated."); |
| | 53 | | } |
| | 54 | |
|
| 0 | 55 | | if (description.SunsetPolicy is SunsetPolicy policy) |
| | 56 | | { |
| 0 | 57 | | if (policy.Date is DateTimeOffset when) |
| | 58 | | { |
| 0 | 59 | | text.Append("The API will be sunset on ") |
| 0 | 60 | | .Append(when.Date.ToShortDateString()) |
| 0 | 61 | | .Append('.'); |
| | 62 | | } |
| | 63 | |
|
| 0 | 64 | | if (policy.HasLinks) |
| | 65 | | { |
| 0 | 66 | | text.Append(BreakLine); |
| 0 | 67 | | foreach (var link in policy.Links) |
| | 68 | | { |
| 0 | 69 | | text.Append($"[{link.Title}]({link.LinkTarget})"); |
| | 70 | | } |
| | 71 | | } |
| | 72 | | } |
| 0 | 73 | | text.Append(BreakLine); |
| | 74 | |
|
| 0 | 75 | | info.Description = text.ToString(); |
| 0 | 76 | | return info; |
| | 77 | | } |
| | 78 | | } |