RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.72k stars 1.29k forks source link

Dynamic API Versioning with NSwag like with Swashbuckle #3843

Open chrfin opened 2 years ago

chrfin commented 2 years ago

I'm trying to implement api-versioning using .NET 6.0 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.
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 };
        });
}
tdor91 commented 1 year ago

I am also interested in this. DId you find a solution yet?

chrfin commented 1 year ago

No, still using the mentioned workaround...