Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 596
T.S. Avatar asked Oct 16 '25 03:10

T.S.


1 Answers

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>();
});
like image 146
CodingMytra Avatar answered Oct 17 '25 16:10

CodingMytra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!