With NSwug we can use a DocumentProcessors which can be used to filter out the unwanted swagger paths.
I have used below nuget packages
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="5.0.0" />
<PackageReference Include="NSwag.AspNetCore" Version="13.9.4" />
Below shows how I registered them and fixed the problem
public void ConfigureServices(IServiceCollection services)
{
....
services.AddApiVersioning(options =>
{
options.AssumeDefaultVersionWhenUnspecified = true;
options.DefaultApiVersion = new ApiVersion(1, 0);
})
.AddVersionedApiExplorer(options =>
{
options.GroupNameFormat = "'v'VVV";
options.SubstituteApiVersionInUrl = true;
});
services.AddSwaggerDocument(config =>
{
SwaggerConfigure(config, "v1", true);
});
services.AddSwaggerDocument(config =>
{
SwaggerConfigure(config, "v2", false);
});
}
SwaggerConfigure implementation
private void SwaggerConfigure(AspNetCoreOpenApiDocumentGeneratorSettings configure, string version, bool isDefaultVersion)
{
configure.DocumentName = version;
configure.ApiGroupNames = new[] { version };
if (isDefaultVersion)
{
configure.DocumentProcessors.Add(new RemoveVersionFromDefault(version));
}
configure.PostProcess = document =>
{
document.Info.Version = version;
document.Info.Title = $"{GetApiTitle()} API";
};
configure.AllowNullableBodyParameters = false;
configure.SerializerSettings = new JsonSerializerSettings();
}
DocumentProcessor implementation
public class RemoveVersionFromDefault : IDocumentProcessor
{
private readonly string _defaultVersion;
public RemoveVersionFromDefault(string defaultVersion)
{
_defaultVersion = defaultVersion;
}
public void Process(DocumentProcessorContext context)
{
var keys = new List<string>();
foreach (var (key, value) in context.Document.Paths)
{
if (key.Contains($"/{_defaultVersion}/"))
{
keys.Add(key);
}
}
foreach (var key in keys)
{
context.Document.Paths.Remove(key);
}
}
}
Thats it. Hope this will help someone who is looking for a similar solution with NSwug