Search code examples
c#asp.net-coreasp.net-web-apiswaggerswashbuckle.aspnetcore

Make swagger display action URL in camel case


If I have an action defined as (note - all names are hypothetical)

[HttpGet("api/[controller]/[action]")]
public string LogMeIn(...)

Swagger will generate URL as .../api/Auth/LogMeIn

enter image description here

I've added option

services.AddRouting(opt => opt.LowercaseUrls = true)

This got me to this point in the swagger .../api/auth/logmein

But I need to be at camelCase --> .../api/auth/logMeIn

I've tried to look through swagger options, app options, but no luck. What can I do here? BTW, searched the internet. Mostly they talk about parameters/models. This is swagger UI URL. Thanks


Solution

  • i don't know if something inbuilt is available or not but you can write a simple document filter like this.

        public class UrlRenameDocumentFilter : IDocumentFilter
        {
            public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
            {
                OpenApiPaths keyValuePairs = new OpenApiPaths();
                foreach(var path in swaggerDoc.Paths)
                {
                    var value = path.Value;
    
                    // here you have to put logic to convert name to camelCase
                    string newkey = ConvertToCamelCase(path.Key);
    
                    keyValuePairs.Add(newkey, value);
                }
                swaggerDoc.Paths = keyValuePairs;
            }
        }
    

    in program.cs

    builder.Services.AddSwaggerGen(o =>
    {
        o.DocumentFilter<UrlRenameDocumentFilter>();
    });