Dynamic API Versioning with NSwag like with Swashbuckle

Viewed 566

I'm trying to implement api-versioning using .NET 6.0 and NSwag and it works fine, but I can't get automatic document generation to work.

How can the following be done with NSwag? -> https://github.com/dotnet/aspnet-api-versioning/wiki/API-Documentation#aspnet-core

All examples I can find manually call AddOpenApiDocument for every version, but I would like to do that automatically for every version like shown in the docs for Swashbuckle. For now I use an intermediate IServiceProvider, but this generates valid warnings:

var versionDescriptionProvider = builder.Services.BuildServiceProvider().GetService<IApiVersionDescriptionProvider>();
foreach (var description in versionDescriptionProvider.ApiVersionDescriptions)
{
    builder.Services.AddOpenApiDocument(document =>
        {
            document.DocumentName = description.GroupName;
            document.ApiGroupNames = new[] { description.GroupName };
        });
}
0 Answers
Related