From e6c62eae0954808bdcfd418c3c2175b3eb652047 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Fri, 26 Jan 2024 08:44:48 +0300 Subject: [PATCH] feat: add swagger configuration --- Endpoint/ConfigureSwaggerOptions.cs | 36 +++++++++++++++++++ Endpoint/SwaggerDefaultValues.cs | 55 +++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 Endpoint/ConfigureSwaggerOptions.cs create mode 100644 Endpoint/SwaggerDefaultValues.cs diff --git a/Endpoint/ConfigureSwaggerOptions.cs b/Endpoint/ConfigureSwaggerOptions.cs new file mode 100644 index 0000000..9b53140 --- /dev/null +++ b/Endpoint/ConfigureSwaggerOptions.cs @@ -0,0 +1,36 @@ +using Microsoft.AspNetCore.Mvc.ApiExplorer; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; +using Microsoft.OpenApi.Models; +using Swashbuckle.AspNetCore.SwaggerGen; +using System; + +namespace Mirea.Api.Endpoint; + +public class ConfigureSwaggerOptions(IApiVersionDescriptionProvider provider) : IConfigureOptions +{ + public void Configure(SwaggerGenOptions options) + { + foreach (var description in provider.ApiVersionDescriptions) + { + options.SwaggerDoc(description.GroupName, CreateInfoForApiVersion(description)); + } + } + + private static OpenApiInfo CreateInfoForApiVersion(ApiVersionDescription description) + { + var info = new OpenApiInfo() + { + Title = "MIREA Schedule Web API", + Version = description.ApiVersion.ToString(), + Description = "This API provides a convenient interface for retrieving data stored in the database. Special attention was paid to the lightweight and easy transfer of all necessary data. Made by the Winsomnia team.", + Contact = new OpenApiContact { Name = "Author name", Email = "support@winsomnia.net" }, + License = new OpenApiLicense { Name = "MIT", Url = new Uri("https://opensource.org/licenses/MIT") } + }; + + if (description.IsDeprecated) + info.Description += " This API version has been deprecated."; + + return info; + } +} \ No newline at end of file diff --git a/Endpoint/SwaggerDefaultValues.cs b/Endpoint/SwaggerDefaultValues.cs new file mode 100644 index 0000000..d47c591 --- /dev/null +++ b/Endpoint/SwaggerDefaultValues.cs @@ -0,0 +1,55 @@ +using Microsoft.AspNetCore.Mvc.ApiExplorer; +using Microsoft.AspNetCore.Mvc.ModelBinding; +using Microsoft.OpenApi.Models; +using Swashbuckle.AspNetCore.SwaggerGen; +using System; +using System.Linq; +using System.Text.Json; + +namespace Mirea.Api.Endpoint; + +public class SwaggerDefaultValues : IOperationFilter +{ + public void Apply(OpenApiOperation operation, OperationFilterContext context) + { + var apiDescription = context.ApiDescription; + operation.Deprecated |= apiDescription.IsDeprecated(); + + foreach (var responseType in context.ApiDescription.SupportedResponseTypes) + { + var responseKey = responseType.IsDefaultResponse ? "default" : responseType.StatusCode.ToString(); + var response = operation.Responses[responseKey]; + + foreach (var contentType in response.Content.Keys) + { + if (responseType.ApiResponseFormats.All(x => x.MediaType != contentType)) + { + response.Content.Remove(contentType); + } + } + } + + if (operation.Parameters == null) + { + return; + } + + foreach (var parameter in operation.Parameters) + { + var description = apiDescription.ParameterDescriptions.First(p => p.Name == parameter.Name); + + parameter.Description ??= description.ModelMetadata?.Description; + + if (parameter.Schema.Default == null && + description.DefaultValue != null && + description.DefaultValue is not DBNull && + description.ModelMetadata is ModelMetadata modelMetadata) + { + var json = JsonSerializer.Serialize(description.DefaultValue, modelMetadata.ModelType); + parameter.Schema.Default = OpenApiAnyFactory.CreateFromJson(json); + } + + parameter.Required |= description.IsRequired; + } + } +} \ No newline at end of file