From dd8d917d04d8f8a6b42fde3c7566ccb31b835207 Mon Sep 17 00:00:00 2001 From: HaikAsatryan Date: Tue, 3 Dec 2024 17:29:37 +0400 Subject: [PATCH 1/2] controller naming to lower --- .../Extensions/ControllerExtensions.cs | 57 ++++++++++++++++++- src/SharedKernel/SharedKernel.csproj | 4 +- 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/src/SharedKernel/Extensions/ControllerExtensions.cs b/src/SharedKernel/Extensions/ControllerExtensions.cs index e0e4271..ff8af37 100644 --- a/src/SharedKernel/Extensions/ControllerExtensions.cs +++ b/src/SharedKernel/Extensions/ControllerExtensions.cs @@ -1,6 +1,9 @@ using System.Reflection; +using System.Text.RegularExpressions; using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Mvc.ApplicationModels; using Microsoft.Extensions.DependencyInjection; +using Microsoft.OpenApi.Models; namespace SharedKernel.Extensions; @@ -8,7 +11,7 @@ public static class ControllerExtensions { public static WebApplicationBuilder AddControllers(this WebApplicationBuilder builder, Assembly[] assemblies) { - var mvcBuilder = builder.Services.AddControllers(); + var mvcBuilder = builder.Services.AddControllers(options => options.Conventions.Add(new ToLowerNamingConvention())); foreach (var assembly in assemblies) { mvcBuilder.AddApplicationPart(assembly); @@ -16,4 +19,54 @@ public static WebApplicationBuilder AddControllers(this WebApplicationBuilder bu return builder; } -} \ No newline at end of file +} + +public class ToLowerNamingConvention : IControllerModelConvention +{ + public void Apply(ControllerModel controller) + { + controller.ControllerName = controller.ControllerName.ToLower(); + + foreach (var action in controller.Actions) + { + action.ActionName = action.ActionName.ToLower(); + } + } +} + +// public class KebabCaseTagNamingDocumentFilter : IDocumentFilter +// { +// public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context) +// { +// // Modify the OpenAPI document here if needed +// foreach (var pathItem in swaggerDoc.Paths) +// { +// // Modify the OpenAPI operation tags to kebab case +// foreach (var operation in pathItem.Value.Operations) +// { +// operation.Value.Tags = operation.Value +// .Tags +// .Select(tag => KebabCase(tag)) +// .ToList(); +// } +// } +// } +// +// private OpenApiTag KebabCase(OpenApiTag tag) +// { +// // Simple kebab case conversion logic +// const string pattern = "[^a-zA-Z-]"; +// +// var name = Regex.Replace(tag.Name, pattern, ""); +// +// var newName = string +// .Concat(name.Select((x, i) => i > 0 && char.IsUpper(x) +// ? "-" + x.ToString() +// .ToLower() +// : x.ToString())) +// .ToLower(); +// +// tag.Name = newName; +// return tag; +// } +// } \ No newline at end of file diff --git a/src/SharedKernel/SharedKernel.csproj b/src/SharedKernel/SharedKernel.csproj index 098fabe..4fbf2e6 100644 --- a/src/SharedKernel/SharedKernel.csproj +++ b/src/SharedKernel/SharedKernel.csproj @@ -8,13 +8,13 @@ Readme.md Pandatech MIT - 1.0.11 + 1.0.12 Pandatech.SharedKernel Pandatech Shared Kernel Library Pandatech, shared kernel, library, OpenAPI, Swagger, utilities, scalar Pandatech.SharedKernel provides centralized configurations, utilities, and extensions for ASP.NET Core projects. For more information refere to readme.md document. https://github.com/PandaTechAM/be-lib-sharedkernel - OpenApi update + Controller naming to lower From 5038eef62897eefd3e33a31fabef185b0253038e Mon Sep 17 00:00:00 2001 From: HaikAsatryan Date: Tue, 3 Dec 2024 17:37:59 +0400 Subject: [PATCH 2/2] updated kebab case for controllers --- .../Extensions/ControllerExtensions.cs | 44 ++----------------- src/SharedKernel/SharedKernel.csproj | 2 +- 2 files changed, 5 insertions(+), 41 deletions(-) diff --git a/src/SharedKernel/Extensions/ControllerExtensions.cs b/src/SharedKernel/Extensions/ControllerExtensions.cs index ff8af37..1303a55 100644 --- a/src/SharedKernel/Extensions/ControllerExtensions.cs +++ b/src/SharedKernel/Extensions/ControllerExtensions.cs @@ -1,5 +1,6 @@ using System.Reflection; using System.Text.RegularExpressions; +using Humanizer; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Mvc.ApplicationModels; using Microsoft.Extensions.DependencyInjection; @@ -25,48 +26,11 @@ public class ToLowerNamingConvention : IControllerModelConvention { public void Apply(ControllerModel controller) { - controller.ControllerName = controller.ControllerName.ToLower(); + controller.ControllerName = controller.ControllerName.Kebaberize(); foreach (var action in controller.Actions) { - action.ActionName = action.ActionName.ToLower(); + action.ActionName = action.ActionName.Kebaberize(); } } -} - -// public class KebabCaseTagNamingDocumentFilter : IDocumentFilter -// { -// public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context) -// { -// // Modify the OpenAPI document here if needed -// foreach (var pathItem in swaggerDoc.Paths) -// { -// // Modify the OpenAPI operation tags to kebab case -// foreach (var operation in pathItem.Value.Operations) -// { -// operation.Value.Tags = operation.Value -// .Tags -// .Select(tag => KebabCase(tag)) -// .ToList(); -// } -// } -// } -// -// private OpenApiTag KebabCase(OpenApiTag tag) -// { -// // Simple kebab case conversion logic -// const string pattern = "[^a-zA-Z-]"; -// -// var name = Regex.Replace(tag.Name, pattern, ""); -// -// var newName = string -// .Concat(name.Select((x, i) => i > 0 && char.IsUpper(x) -// ? "-" + x.ToString() -// .ToLower() -// : x.ToString())) -// .ToLower(); -// -// tag.Name = newName; -// return tag; -// } -// } \ No newline at end of file +} \ No newline at end of file diff --git a/src/SharedKernel/SharedKernel.csproj b/src/SharedKernel/SharedKernel.csproj index 4fbf2e6..3219848 100644 --- a/src/SharedKernel/SharedKernel.csproj +++ b/src/SharedKernel/SharedKernel.csproj @@ -14,7 +14,7 @@ Pandatech, shared kernel, library, OpenAPI, Swagger, utilities, scalar Pandatech.SharedKernel provides centralized configurations, utilities, and extensions for ASP.NET Core projects. For more information refere to readme.md document. https://github.com/PandaTechAM/be-lib-sharedkernel - Controller naming to lower + Controller naming to kebab