From c804f6cd93d71f46ca0b708e582227f7e71664ec Mon Sep 17 00:00:00 2001 From: Andrey Kukharenko Date: Fri, 7 Feb 2025 22:06:10 +0300 Subject: [PATCH] Updated packages. Added Swagger OperationId filter. (#71) --- Directory.Packages.props | 24 +++++++++---------- .../Extensions/ServiceCollectionExtensions.cs | 3 +++ .../Swagger/SwaggerOperationIdFilter.cs | 22 +++++++++++++++++ 3 files changed, 37 insertions(+), 12 deletions(-) create mode 100644 src/SimpleServicesDashboard.Api/Infrastructure/Swagger/SwaggerOperationIdFilter.cs diff --git a/Directory.Packages.props b/Directory.Packages.props index b27f594..699ebdf 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -16,17 +16,17 @@ - - - - - - - - - + + + + + + + + + - + @@ -43,7 +43,7 @@ - - + + \ No newline at end of file diff --git a/src/SimpleServicesDashboard.Api/Infrastructure/Extensions/ServiceCollectionExtensions.cs b/src/SimpleServicesDashboard.Api/Infrastructure/Extensions/ServiceCollectionExtensions.cs index a7b21c1..26adfc8 100644 --- a/src/SimpleServicesDashboard.Api/Infrastructure/Extensions/ServiceCollectionExtensions.cs +++ b/src/SimpleServicesDashboard.Api/Infrastructure/Extensions/ServiceCollectionExtensions.cs @@ -156,6 +156,9 @@ private static IServiceCollection ConfigureSwaggerGeneration(this IServiceCollec // add a custom operation filter which sets default values options.OperationFilter(); + + // add operation filter to generate the OperationId value for endpoints + options.OperationFilter(); // Set the comments path for the Swagger JSON and UI. var xmlDocFiles = new[] diff --git a/src/SimpleServicesDashboard.Api/Infrastructure/Swagger/SwaggerOperationIdFilter.cs b/src/SimpleServicesDashboard.Api/Infrastructure/Swagger/SwaggerOperationIdFilter.cs new file mode 100644 index 0000000..0864aee --- /dev/null +++ b/src/SimpleServicesDashboard.Api/Infrastructure/Swagger/SwaggerOperationIdFilter.cs @@ -0,0 +1,22 @@ +using Microsoft.OpenApi.Models; +using Swashbuckle.AspNetCore.SwaggerGen; + +namespace SimpleServicesDashboard.Api.Infrastructure.Swagger; + +/// +/// Represents the Swagger/Swashbuckle operation filter used to document the implicit API operationId to be generated. +/// +public sealed class SwaggerOperationIdFilter : IOperationFilter +{ + /// + /// Applies the filter to the specified operation using the given context. + /// + /// The operation to apply the filter to. + /// The current operation filter context. + public void Apply(OpenApiOperation operation, OperationFilterContext context) + { + var controllerName = context.ApiDescription.ActionDescriptor.RouteValues["controller"]; + var actionName = context.ApiDescription.ActionDescriptor.RouteValues["action"]; + operation.OperationId = $"{controllerName}_{actionName}"; + } +} \ No newline at end of file