question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NSwag.AspNetCore Swagger document generation not using Name property from HTTP verb attribute as Operation ID

See original GitHub issue

I’m using version 13.1.6 the NSwag.AspNetCore NuGet package and the Swagger document generation is not correctly setting Operation ID of the API endpoints (the framework of my website project is netcoreapp2.2).

It is using the {controller name}_{HTTP method type} convention instead of honouring the Name property on the HTTP verb attribute.

Here is a sample of my code and generated Swagger document.

Controller

    [ApiController]
    [Route("[controller]")]
    public class UsersController : Controller
    {
        [HttpPost(Name = "CreateUser")]
        [ProducesResponseType(200)]
        [ProducesResponseType(400, Type = typeof(ValidationProblemDetails))]
        public async Task<IActionResult> Post([FromBody]RegistrationRequest request)
        {
            ...
        }
    }

Startup

    public class Startup
    {
        public static void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            ...
            app.UseOpenApi();
            app.UseSwaggerUi3();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}");

                routes.MapRoute(
                    name: "single",
                    template: "{controller=Home}/{id}");
            });
        }

        public void ConfigureServices(IServiceCollection services)
        {
            ...
            services
                .AddMvc(options =>
                {
                    options
                        .Conventions
                        .Add(new KebabCaseRouteTokenReplacementControllerModelConvention());
                })
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
            ...
            services.AddSwaggerDocument(options =>
            {
                options.Title = "My API";
                options.Version = "v1";
            });
        }
    }

Swagger JSON

{
...
  "paths": {
       /users": {
      "post": {
        "tags": [
          "Users"
        ],
        "operationId": "Users_Post",
        ...
     }
   }
  ...
}

Based on the Name property on the HttpPostAttribute, I would have expected the operationId in the JSON to be CreateUser, but it is Users_Post

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
RicoSutercommented, Jan 21, 2020

Is there a way to achieve the same

https://github.com/RicoSuter/NSwag/blob/master/src/NSwag.Annotations/OpenApiOperationAttribute.cs#L19

or customize the default generation in any way?

You can implement a custom operation processor and set the operation ids however you want.

But it might make sense to also consider the attribute Name when defined out of the box.

2reactions
Leon99commented, Jan 21, 2020

@TheMagnificent11 did you manage to solve this? I have the same issue.

Swashbuckle used the Name property to generate operationId. Is there a way to achieve the same or customize the default generation in any way?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Get started with NSwag and ASP.NET Core
Learn how to use NSwag to generate documentation and help pages ... Add the [Required] attribute to the Name property of the TodoItem...
Read more >
Manually set operationId to allow multiple operations with ...
For some reason [SwaggerOperation("operationId")] didn't work in our case. Using .Net Core 3.1 and swashbuckle 5.6.0. However [HttpGet(Name = " ...
Read more >
Mastering Web APIs with Swagger, ApiExplorer and NSwag
The fix is to specify the name so Swashbuckle can generate an operationId. That's easy with the Name property in the HttpGet or...
Read more >
Enriched Web API Documentation using Swagger/OpenAPI in ...
In practice, in an ASP .NET Core project, we use specific Attributes and XML comments to define all the needed information (e.g., HTTP...
Read more >
Using OpenApiReference To Generate Open API Client ...
Background. I have been using Rico Suter's brilliant NSwag for some time now to generate client code for working with HTTP API endpoints....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found