11using System . Reflection ;
2+ using System . Text . RegularExpressions ;
23using Microsoft . AspNetCore . Builder ;
4+ using Microsoft . AspNetCore . Mvc . ApplicationModels ;
35using Microsoft . Extensions . DependencyInjection ;
6+ using Microsoft . OpenApi . Models ;
47
58namespace SharedKernel . Extensions ;
69
710public static class ControllerExtensions
811{
912 public static WebApplicationBuilder AddControllers ( this WebApplicationBuilder builder , Assembly [ ] assemblies )
1013 {
11- var mvcBuilder = builder . Services . AddControllers ( ) ;
14+ var mvcBuilder = builder . Services . AddControllers ( options => options . Conventions . Add ( new ToLowerNamingConvention ( ) ) ) ;
1215 foreach ( var assembly in assemblies )
1316 {
1417 mvcBuilder . AddApplicationPart ( assembly ) ;
1518 }
1619
1720 return builder ;
1821 }
19- }
22+ }
23+
24+ public class ToLowerNamingConvention : IControllerModelConvention
25+ {
26+ public void Apply ( ControllerModel controller )
27+ {
28+ controller . ControllerName = controller . ControllerName . ToLower ( ) ;
29+
30+ foreach ( var action in controller . Actions )
31+ {
32+ action . ActionName = action . ActionName . ToLower ( ) ;
33+ }
34+ }
35+ }
36+
37+ // public class KebabCaseTagNamingDocumentFilter : IDocumentFilter
38+ // {
39+ // public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
40+ // {
41+ // // Modify the OpenAPI document here if needed
42+ // foreach (var pathItem in swaggerDoc.Paths)
43+ // {
44+ // // Modify the OpenAPI operation tags to kebab case
45+ // foreach (var operation in pathItem.Value.Operations)
46+ // {
47+ // operation.Value.Tags = operation.Value
48+ // .Tags
49+ // .Select(tag => KebabCase(tag))
50+ // .ToList();
51+ // }
52+ // }
53+ // }
54+ //
55+ // private OpenApiTag KebabCase(OpenApiTag tag)
56+ // {
57+ // // Simple kebab case conversion logic
58+ // const string pattern = "[^a-zA-Z-]";
59+ //
60+ // var name = Regex.Replace(tag.Name, pattern, "");
61+ //
62+ // var newName = string
63+ // .Concat(name.Select((x, i) => i > 0 && char.IsUpper(x)
64+ // ? "-" + x.ToString()
65+ // .ToLower()
66+ // : x.ToString()))
67+ // .ToLower();
68+ //
69+ // tag.Name = newName;
70+ // return tag;
71+ // }
72+ // }
0 commit comments