feat: add a tag schema to combine similar controllers.
This commit is contained in:
@ -17,6 +17,7 @@ public static class SwaggerConfiguration
|
||||
{
|
||||
services.AddSwaggerGen(options =>
|
||||
{
|
||||
options.OperationFilter<SwaggerTagSchemeFilter>();
|
||||
options.SchemaFilter<SwaggerExampleFilter>();
|
||||
options.OperationFilter<SwaggerDefaultValues>();
|
||||
options.OperationFilter<ActionResultSchemaFilter>();
|
||||
|
@ -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<TagsAttribute>(inherit: true).FirstOrDefault();
|
||||
|
||||
if (tagsAttribute == null)
|
||||
{
|
||||
var baseType = controllerType.BaseType;
|
||||
while (baseType != null)
|
||||
{
|
||||
tagsAttribute = baseType.GetCustomAttributes<TagsAttribute>(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] });
|
||||
}
|
||||
}
|
11
Endpoint/Controllers/ConfigurationBaseController.cs
Normal file
11
Endpoint/Controllers/ConfigurationBaseController.cs
Normal file
@ -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;
|
Reference in New Issue
Block a user