I have an API and associated Swagger docs
I have an ASP.NET Core 7 API project, wired up to produce Swagger documentation. The pertinent initialization code, in Program.cs, looks like this:
// Initialization
var builder = WebApplication.CreateBuilder(args);
var services = builder.Services;
var configuration = builder.Configuration;
// Configure Swagger
// See: https://aka.ms/aspnetcore/swashbuckle
services.AddEndpointsApiExplorer();
services.AddSwaggerGen(options => {
// Basic API info
options.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo {
Title = "My example API",
Version = "v1",
Description = "REST API for interacting with my example functions and data",
TermsOfService = new Uri("https://example.com/ApiTermsOfUse")
});
// Wire up the XML comments, which have been generated in .xml files in more than one project
foreach (var filePath in System.IO.Directory.GetFiles(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!), "*.xml")) {
try {
options.IncludeXmlComments(filePath);
}
catch (Exception ex) when(ex.OkToCatch()) {
// <Ignore the exception>
}
}
});
My API contains several controllers (PatientAppController
, ProviderController
, and ServerInfoController
), each of which produces a separate section in the Swagger display.
Now I want to deprecate version 1.0 of the PatientApp
part of the API and add version 2.0 of those same API endpoints.
So, I copied PatientAppController
to a new PatientAppV2Controller
and added some attributes to the class
definitions:
PatientAppController
[ApiController]
[Route("v{version:apiVersion}/[controller]/[action]")] // Rout: https://example.com/v1/PatientApp/SomeAction
[ApiVersion("1.0", Deprecated = true)] // This is API v1, and it's deprecated
[Authorize()]
[Obsolete("Removed in API version 2.0")] // Tell the compiler it's deprecated
public partial class PatientAppController : ControllerBase
{ ... }
PatientAppV2Controller
[ApiController]
[Route("v{version:apiVersion}/PatientApp/[action]")] // Rout: https://example.com/v2/PatientApp/SomeAction
[ApiVersion("2.0")] // This is API v2
[Authorize()]
public partial class PatientAppController : ControllerBase
{ ... }
and I added this additional configuration to Program.cs:
was:
// -- Configure the HTTP request pipeline
app.UseSwagger();
app.UseSwaggerUI();
added Swagger endpoints:
// -- Configure the HTTP request pipeline
app.UseSwagger();
app.UseSwaggerUI(options => {
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Indigo Health API V1");
options.SwaggerEndpoint("/swagger/v2/swagger.json", "Indigo Health API V2");
});
Now, the Swagger display is weird
The "version 1" page looks correct:
but the "version 2" page (select "Indigo Health API V2 from the drop-down at the top of the page) displays a mangled version of the controller name (PatientAppV - no clue why it isn't PatientAppV2):
At last... the question
How do I change the name Swagger displays for the "section"
I want Swagger to display the section name associated with my PatientAppV2Controller
as PatientApp on the page shown above, not PatientAppV. How do I do this?