| | 1 | | using Microsoft.Extensions.Options; |
| | 2 | | using Swashbuckle.AspNetCore.SwaggerGen; |
| | 3 | |
|
| | 4 | | namespace Gateway.Swagger; |
| | 5 | |
|
| | 6 | | public static class SwaggerDocumentation |
| | 7 | | { |
| | 8 | | public static WebApplication UseSwaggerDocumentation(this WebApplication app) |
| | 9 | | { |
| 0 | 10 | | if (app.Environment.IsDevelopment()) |
| | 11 | | { |
| 0 | 12 | | app.UseSwagger(); |
| 0 | 13 | | app.UseSwaggerUI(options => |
| 0 | 14 | | { |
| 0 | 15 | | var serviceMetadata = app.Services.GetRequiredService<IOptions<ServiceMetadata>>(); |
| 0 | 16 | | var descriptions = app.DescribeApiVersions(); |
| 0 | 17 | |
|
| 0 | 18 | | foreach (var description in descriptions.Reverse()) |
| 0 | 19 | | { |
| 0 | 20 | | var url = $"/swagger/{description.GroupName}/swagger.json"; |
| 0 | 21 | | var name = description.GroupName.ToUpperInvariant(); |
| 0 | 22 | | options.SwaggerEndpoint(url, name); |
| 0 | 23 | | } |
| 0 | 24 | | options.EnableFilter(); |
| 0 | 25 | | options.DocumentTitle = serviceMetadata.Value.ServiceName; |
| 0 | 26 | | options.EnableValidator(); |
| 0 | 27 | | options.EnableDeepLinking(); |
| 0 | 28 | | options.ShowCommonExtensions(); |
| 0 | 29 | | options.ShowExtensions(); |
| 0 | 30 | | options.DisplayOperationId(); |
| 0 | 31 | | options.DisplayRequestDuration(); |
| 0 | 32 | | }); |
| | 33 | | } |
| 0 | 34 | | return app; |
| | 35 | | } |
| | 36 | |
|
| | 37 | | public static WebApplicationBuilder AddSwagger(this WebApplicationBuilder builder) |
| | 38 | | { |
| 0 | 39 | | builder |
| 0 | 40 | | .Services |
| 0 | 41 | | .AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerOptions>(); |
| | 42 | |
|
| 0 | 43 | | builder.Services.AddSwaggerGen(o => |
| 0 | 44 | | { |
| 0 | 45 | | o.OperationFilter<DeprecationOperationFilter>(); |
| 0 | 46 | | o.UseAllOfForInheritance(); |
| 0 | 47 | | o.UseAllOfToExtendReferenceSchemas(); |
| 0 | 48 | | o.UseInlineDefinitionsForEnums(); |
| 0 | 49 | | o.UseOneOfForPolymorphism(); |
| 0 | 50 | | o.SupportNonNullableReferenceTypes(); |
| 0 | 51 | | o.DescribeAllParametersInCamelCase(); |
| 0 | 52 | | o.OrderActionsBy(x => x.HttpMethod); |
| 0 | 53 | | }); |
| | 54 | |
|
| 0 | 55 | | return builder; |
| | 56 | | } |
| | 57 | | } |