Skip to content

How to support multiple swagger files per version/group? #516

Closed
@mattmazzola

Description

@mattmazzola

This is a question about best practices to achieve the multi swagger generation per version rather than specific issue with library. Hope someone can help or suggest a work around.

Background:

We have existing Asp.net core service that was using hard coded url versioning such as [Route("api/v1/app")]. We also have certain controller actions separated into groups to generate two different swagger files for Azure APIM. These different operations needed to be billed and managed differently (throttling, quota, etc). We did this by using the group name:

[ApiExplorerSettings(GroupName = "authoring")]
Controller1

[ApiExplorerSettings(GroupName = "session")]
Controller2

and then manually generating matching swagger files

c.SwaggerEndpoint("/swagger/authoring/swagger.json", ... )
c.SwaggerEndpoint("/swagger/session/swagger.json", ... )

Intention

We wanted to add a new version (2.0) to the app to change the urls to more closely align with the Microsoft REST guidelines and wanted to set it up in a scalable way based on the sample https://github.com/microsoft/aspnet-api-versioning/tree/master/samples/aspnetcore/SwaggerSample
this is involved changes to AddApiVersioning, AddVersionedApiExplorer, and most notably how the swagger files are generated.

We wanted to preserve the separate of authoring and session operates and hoped to be able to generate two files per version descriptor such as:

foreach (var description in provider.ApiVersionDescriptions)
{
    c.SwaggerEndpoint($"/swagger/authoring-{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant());
    c.SwaggerEndpoint($"/swagger/session-{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant());
}

to achieve something like this:

/v1
  /authoring/swagger.json
  /sesssion/swagger.json
/v2
  /authoring/swagger.json
  /session/swagger.json

However there seems to be catch-22 situation. It seems Swagger/Swashbuckle was using group name to associate operations and generate files. If we change group name to be generic ''v'VVV" it means we can no longer use the split the actions/operations between the groups using the [ApiExplorerSettings(GroupName = "session")] attribute since it is one group per version. If we keep the specific groups then we can't generate them dynamically from the version descriptors because the group names ("authoring" and "session" won't map to the versions "v1" and "v2" etc.

Our thought was maybe there is some hack like making the version name include a slash such as
v{version}/authoring and v{version}/session but that would increase complexity and put us off standard sample for people to help us.

Our only current idea is to go to generic versioning with single swagger per version and sepearate the authoring vs session groups by moving it to a new .net core project. That is a much larger work than we thought as we have to re-architect to share resources across them and thus creating this issue asking for help.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions