NSwag.AspNetCore Swagger document generation not using Name property from HTTP verb attribute as Operation ID
See original GitHub issueI’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:
- Created 4 years ago
- Comments:8 (8 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
https://github.com/RicoSuter/NSwag/blob/master/src/NSwag.Annotations/OpenApiOperationAttribute.cs#L19
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.
@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?