Advertisement
Intermediate ASP.NET Core, API Documentation with Swagger/OpenAPI

How do you version your OpenAPI documentation to reflect API versioning implemented via the Asp.Versioning library?

Short answer

Swashbuckle integrates with Asp.Versioning by generating a separate SwaggerDoc per registered API version (using IApiVersionDescriptionProvider to discover them dynamically), and configuring SwaggerUI with a corresponding SwaggerEndpoint per version, producing a dropdown in the…

Save ASP.NET Core mock interview
Advertisement
AModel answer
Swashbuckle integrates with Asp.Versioning by generating a separate SwaggerDoc per registered API version (using IApiVersionDescriptionProvider to discover them dynamically), and configuring SwaggerUI with a corresponding SwaggerEndpoint per version, producing a dropdown in the UI letting consumers browse documentation specific to each API version rather than one merged, potentially confusing document mixing all versions together.
builder.Services.ConfigureOptions<ConfigureSwaggerOptions>();  // discovers versions dynamically

app.UseSwaggerUI(options => {
    foreach (var description in app.DescribeApiVersions()) {
        options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant());
    }
});
Real-world example
An API supporting both v1 and v2 shows a version selector dropdown in Swagger UI, letting a developer integrating against the legacy v1 contract view accurate, version-specific documentation without seeing v2-only endpoints mixed in.
Likely follow-ups
  • How does ConfigureSwaggerOptions dynamically discover registered API versions?
  • What happens to a deprecated version's documentation once it's fully removed from the API?
Related topics