72 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			72 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using Asp.Versioning.ApiExplorer;
 | |
| using Microsoft.Extensions.Options;
 | |
| using Microsoft.OpenApi.Any;
 | |
| using Swashbuckle.AspNetCore.SwaggerGen;
 | |
| using Swashbuckle.AspNetCore.SwaggerUI;
 | |
| 
 | |
| namespace Core.Blueprint.Service.API.Extensions
 | |
| {
 | |
|     public static class SwaggerExtensions
 | |
|     {
 | |
|         public static void AddSwagger(this IServiceCollection services)
 | |
|         {
 | |
|             services.AddEndpointsApiExplorer();
 | |
|             services.AddSwaggerGen();
 | |
|             services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerOptions>();
 | |
|         }
 | |
| 
 | |
|         public static void ConfigureSwagger(this WebApplication app)
 | |
|         {
 | |
|             app.UseSwagger();
 | |
|             app.UseSwaggerUI(options =>
 | |
|             {
 | |
|                 foreach (var version in app.DescribeApiVersions().Select(version => version.GroupName))
 | |
|                     options.SwaggerEndpoint($"/swagger/{version}/swagger.json", version);
 | |
| 
 | |
|                 options.DisplayRequestDuration();
 | |
|                 options.EnableTryItOutByDefault();
 | |
|                 options.DocExpansion(DocExpansion.None);
 | |
|             });
 | |
|         }
 | |
|         public static IServiceCollection AddVersioning(this IServiceCollection services)
 | |
|         {
 | |
|             services.AddApiVersioning(options => options.ReportApiVersions = true)
 | |
|                    .AddApiExplorer(options =>
 | |
|                    {
 | |
|                        options.GroupNameFormat = "'v'VVV";
 | |
|                        options.SubstituteApiVersionInUrl = true;
 | |
|                    });
 | |
| 
 | |
|             return services;
 | |
|         }
 | |
|     }
 | |
|     public class ConfigureSwaggerOptions : IConfigureOptions<SwaggerGenOptions>
 | |
|     {
 | |
|         private readonly IApiVersionDescriptionProvider _provider;
 | |
| 
 | |
|         public ConfigureSwaggerOptions(IApiVersionDescriptionProvider provider)
 | |
|         {
 | |
|             _provider = provider;
 | |
|         }
 | |
| 
 | |
|         public void Configure(SwaggerGenOptions options)
 | |
|         {
 | |
|             foreach (var description in _provider.ApiVersionDescriptions)
 | |
|                 options.SwaggerDoc(description.GroupName, new()
 | |
|                 {
 | |
|                     Title = AppDomain.CurrentDomain.FriendlyName,
 | |
|                     Version = description.ApiVersion.ToString()
 | |
|                 });
 | |
| 
 | |
|             //Map ALL Values Format TODO
 | |
|             options.MapType<DateOnly>(() => new()
 | |
|             {
 | |
|                 Format = "date",
 | |
|                 Example = new OpenApiString(DateOnly.MinValue.ToString())
 | |
|             });
 | |
| 
 | |
|             options.CustomSchemaIds(type => type.ToString().Replace("+", "."));
 | |
|         }
 | |
|     }
 | |
| }
 | 
