'Asp.Net core Swashbuckle set operationId
How can I set swagger operationId attribute in Asp.Net Core 2.1 project? According to this post I should use SwaggerOperationAttribute but I cannot find it in Swashbuckle.AspNetCore library. Also there is an IOperationFilter
public interface IOperationFilter
{
void Apply(Operation operation, OperationFilterContext context);
}
and I can't find any implementations for swagger generation purposes.
Solution 1:[1]
You can enable annotation on swagger with the Swashbuckle.AspNetCore.Annotations NuGet package. (https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/master/README.md#swashbuckleaspnetcoreannotations)
Once annotations have been enabled, you can enrich the generated Operation metadata by decorating actions with a SwaggerOperationAttribute.
[HttpPost]
[SwaggerOperation(
Summary = "Creates a new product",
Description = "Requires admin privileges",
OperationId = "CreateProduct",
Tags = new[] { "Purchase", "Products" }
)]
public IActionResult Create([FromBody]Product product)
Solution 2:[2]
Adding a Name parameter to [HttpGet]/[HttpPost] fails with an exception in the most recent version, but putting a Name parameter on the Route attribute seems to work:
/// <summary>
/// Get all devices
/// </summary>
[Route("devices", Name = "GetAllDevices")]
[Authorize]
[HttpGet]
[Produces(typeof(Device[]))]
public async Task<IActionResult> GetAllDevices() { ...}
Solution 3:[3]
Finally, I came to this solution:
public class SwaggerOperationNameFilter : IOperationFilter
{
public void Apply(Operation operation, OperationFilterContext context)
{
operation.OperationId = context.MethodInfo.DeclaringType.GetCustomAttributes(true)
.Union(context.MethodInfo.GetCustomAttributes(true))
.OfType<SwaggerOperationAttribute>()
.Select(a => a.OperationId)
.FirstOrDefault();
}
}
[AttributeUsage(AttributeTargets.Method)]
public sealed class SwaggerOperationAttribute : Attribute
{
public SwaggerOperationAttribute(string operationId)
{
OperationId = operationId;
}
public string OperationId { get; }
}
// Startup.cs
services.AddSwaggerGen(c =>
{
...
c.OperationFilter<SwaggerOperationNameFilter>();
};
[HttpGet("{id:int}")]
[SwaggerOperation("GetById")]
public async Task<IActionResult> Get(int id)
{
...
}
But it still seems to me that I've reinvented the wheel.
Solution 4:[4]
Its pretty simple
Add EnableAnnotations in ConfigureService Method
{
options.SwaggerDoc("v1", new OpenApiInfo
{
Title = "Project HTTP API",
Version = "v1",
Description = "...."
});
options.EnableAnnotations();
});
And use in controllers
[SwaggerOperation(OperationId = "GET_API")]
You can see in this in Swagger Json as
get": {
"tags": [
"API"
],
"summary": "....",
"operationId": "GET_API"
}
Solution 5:[5]
You can also generate the operation id based on the action name which is the method name, I found this handy when generating the API client.
services.AddSwaggerGen(c =>
{
c.CustomOperationIds(e => $"{e.ActionDescriptor.RouteValues["action"]}");
c.SwaggerDoc("v1", new Info { Title = "Test API", Version = "v1" });
});
Solution 6:[6]
add this line - swagger.CustomOperationIds(e => $"{e.RelativePath}"); in services.AddSwaggerGen function call
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pang |
| Solution 2 | Pang |
| Solution 3 | Pang |
| Solution 4 | Akshay H |
| Solution 5 | Bowen |
| Solution 6 | Yogesh Patil |
