C# .net core swagger trying to use Multiple API Versions, but all end-points are in all documents
This is how I config swagger with multi version
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info
{
Version = "v1",
Title = "Awesome CMS Core API V1",
Contact = new Contact { Name = "Tony Hudson", Email = "", Url = "https://github.com/ngohungphuc" }
});
c.SwaggerDoc("v2", new Info
{
Version = "v2",
Title = "Awesome CMS Core API V2",
Contact = new Contact { Name = "Tony Hudson", Email = "", Url = "https://github.com/ngohungphuc" }
});
c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
});
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint($"/swagger/v1/swagger.json", "Awesome CMS Core API V1");
c.SwaggerEndpoint($"/swagger/v2/swagger.json", "Awesome CMS Core API V2");
});
And in my controller I need to config like this
[ApiVersion("1.0")]
[ApiExplorerSettings(GroupName = "v1")]
[Route("api/v{version:apiVersion}/Account/")]
The problem was that I had the following line in my swagger config:
services.AddSwaggerGen(c =>
{
...
c.DocInclusionPredicate((_, api) => !string.IsNullOrWhiteSpace(api.GroupName));
...
});
The doc inclusion prediction was always giving back true. That why it added all the end-points to all the docs. We don't need the doc inclusion prediction as we already add the and-points to to correct group by the ApiExplorerGroupPerVersionConvention