I was wondering is there any way to create a custom class or attribute which I can call and keep all swagger documentation attributes at one place, for example This is my original code
[HttpGet]
[SwaggerOperation(
Summary = "Get the list of customers",
Description = "This Api returns the list of customers from database and and display on Customer Screen",
Tags = ["Customer"]
)
]
[SwaggerResponse(200, description: "Returns a list of customers if the request was successful.")]
[SwaggerResponse(500, "Internal Server Error along with the complete Error message.")]
public async Task<ActionResult> GetCustomersAsync(CancellationToken cancellationToken)
{
// Code to Get the customers
return Ok(customers);
}
Now, I was wondering, is there any way to reduce all this code and create a custom attribute class and pass the values, something like this
[AttributeUsage(AttributeTargets.Method, Inherited = true)]
public class SwaggerDocumentation : Attribute
{
public SwaggerDocumentation(string operationSummary, string operationDescription, string operationTags, string responseSuccessMessage)
{
// I am not sure what will come here, but ideally this is how it should look like
[SwaggerOperation(
Summary = operationSummary,
Description = operationDescription,
Tags = operationTags)]
[SwaggerResponse(200, description: $"Returns a {responseSuccessMessage} if the request was successful.")]
[SwaggerResponse(500, "Internal Server Error along with the complete Error message.")]
}
}
and then decorate my function with something like
[HttpGet]
[SwaggerDocumentation("MySummary","MyDescription", "MyTags", "List of Customers")]
public async Task<ActionResult> GetCustomersAsync(CancellationToken cancellationToken)
{
}
I am not sure even this possible.