Asp.NET Core - Web Api - OpenApi Client method names

Viewed 324

I'm generating my Swagger/OpenAPI descriptor in my API:

services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Title = "AthCoin API",
                    Version = "v1",
                    Description = "Ath coin interop service.",
                    Contact = new OpenApiContact
                    {
                        Name = "Ricerca e Sviluppo",
                        Email = "xx@my.it",
                        Url = new Uri("https://athena.srl/"),
                    },
                });
                
            });

... and in my client application (from Visual Studio) I'm generating automatically the client adding a "connected service".

On my API client each API method is mapped only using the action name:

  • UserController.Create() -> is mapped as SwaggerClient.Create()

  • ProductController.Create() -> is mapped ad SwaggerCient.Create2()

This is not useful. Is there is a way to change the autogenerated client, adding the controller's name in the generated method name?

For example:

  • UserController.Create() -> is mapped as SwaggerClient.UserCreate()
  • ProductController.Create() -> is mapped ad SwaggerCient.ProductCreate()
1 Answers

Add nuget package Swashbuckle.AspNetCore.Annotations to your API project and mark your controller action with SwaggerOperationAttribute with OperationId parameter like this:

[SwaggerOperation(OperationId = "UserCreate")]
public async Task<IActionResult> Create()
{
    //whatever
    return Ok();
}

This will produce "operationId": "UserCreate" in the swagger JSON which in turn produces the desired UserCreate method in your generated client.

Related