From 5870eef552d85c985269be135ee873b5521ae063 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Sat, 1 Feb 2025 20:47:08 +0300 Subject: [PATCH] feat: add a tag schema to combine similar controllers. --- .../Core/Startup/SwaggerConfiguration.cs | 1 + .../SwaggerOptions/SwaggerTagSchemeFilter.cs | 40 +++++++++++++++++++ .../ConfigurationBaseController.cs | 11 +++++ 3 files changed, 52 insertions(+) create mode 100644 Endpoint/Configuration/SwaggerOptions/SwaggerTagSchemeFilter.cs create mode 100644 Endpoint/Controllers/ConfigurationBaseController.cs diff --git a/Endpoint/Configuration/Core/Startup/SwaggerConfiguration.cs b/Endpoint/Configuration/Core/Startup/SwaggerConfiguration.cs index 8d51276..9947041 100644 --- a/Endpoint/Configuration/Core/Startup/SwaggerConfiguration.cs +++ b/Endpoint/Configuration/Core/Startup/SwaggerConfiguration.cs @@ -17,6 +17,7 @@ public static class SwaggerConfiguration { services.AddSwaggerGen(options => { + options.OperationFilter(); options.SchemaFilter(); options.OperationFilter(); options.OperationFilter(); diff --git a/Endpoint/Configuration/SwaggerOptions/SwaggerTagSchemeFilter.cs b/Endpoint/Configuration/SwaggerOptions/SwaggerTagSchemeFilter.cs new file mode 100644 index 0000000..3e3291b --- /dev/null +++ b/Endpoint/Configuration/SwaggerOptions/SwaggerTagSchemeFilter.cs @@ -0,0 +1,40 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc.Controllers; +using Microsoft.OpenApi.Models; +using Swashbuckle.AspNetCore.SwaggerGen; +using System.Linq; +using System.Reflection; + +namespace Mirea.Api.Endpoint.Configuration.SwaggerOptions; + +public class SwaggerTagSchemeFilter : IOperationFilter +{ + public void Apply(OpenApiOperation operation, OperationFilterContext context) + { + if (context.ApiDescription.ActionDescriptor is not ControllerActionDescriptor controllerActionDescriptor) + return; + + var controllerType = controllerActionDescriptor.ControllerTypeInfo; + + var tagsAttribute = controllerType.GetCustomAttributes(inherit: true).FirstOrDefault(); + + if (tagsAttribute == null) + { + var baseType = controllerType.BaseType; + while (baseType != null) + { + tagsAttribute = baseType.GetCustomAttributes(inherit: true).FirstOrDefault(); + if (tagsAttribute != null) + break; + + baseType = baseType.BaseType; + } + } + + if (tagsAttribute == null) + return; + + operation.Tags ??= []; + operation.Tags.Add(new OpenApiTag { Name = tagsAttribute.Tags[0] }); + } +} \ No newline at end of file diff --git a/Endpoint/Controllers/ConfigurationBaseController.cs b/Endpoint/Controllers/ConfigurationBaseController.cs new file mode 100644 index 0000000..7b8bdb3 --- /dev/null +++ b/Endpoint/Controllers/ConfigurationBaseController.cs @@ -0,0 +1,11 @@ +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; + +namespace Mirea.Api.Endpoint.Controllers; + +[Route("api/v{version:apiVersion}/Configuration/[controller]")] +[Authorize] +[ProducesResponseType(StatusCodes.Status401Unauthorized)] +[Tags("Configuration")] +public class ConfigurationBaseController : BaseController; \ No newline at end of file