How to configure MultipleApiVersions in Swashbuckle using aspnet ApiVersioning

Zoop picture Zoop · Jun 5, 2017 · Viewed 9.2k times · Source

How do I configure swashbuckle to work with Aspnet API verisoning? https://github.com/Microsoft/aspnet-api-versioning

In my Startup.cs I have the following code to initialize attribute based routing, api versioning, and swagger.

var constraintResolver = new DefaultInlineConstraintResolver()
{
    ConstraintMap =
    {
        ["apiVersion"] = typeof( ApiVersionRouteConstraint )
    }
};
config.MapHttpAttributeRoutes(constraintResolver);
config.AddApiVersioning();

config.EnableSwagger(c =>
{
    c.MultipleApiVersions(
        (apiDesc, targetApiVersion) => ResolveVersionSupportByRouteConstraint(apiDesc, targetApiVersion),
        (vc) =>
        {
            vc.Version("v1", "Swashbuckle Dummy API V1");
            vc.Version("v2", "Swashbuckle Dummy API V2");
        });
}



public static bool ResolveVersionSupportByRouteConstraint(ApiDescription apiDesc, string targetApiVersion)
{
    var versionConstraint = (apiDesc.Route.Constraints.ContainsKey("apiVersion"))
        ? apiDesc.Route.Constraints["apiVersion"] as RegexRouteConstraint
        : null;

    return (versionConstraint == null)
        ? false
        : versionConstraint.Pattern.Split('|').Contains(targetApiVersion);
}

When the ResolveVersionSupportByRouteConstraintmethod fires the route template includes the literal api string "api/v{version}/users" My users controller is decorated with [ApiVersion("1.0")] and I have the following route defined [Route("api/v{version:apiVersion}/users")]. When I hit api/v1/users with postman the call works, but i cannot figure out how to get this working with Swashbuckle/Swagger.

I want my swagger documentation to look like the example for the asp.net core api boilerplate, except I am using Owin with the owin startup class instead of .net core: https://github.com/ASP-NET-Core-Boilerplate/Templates/blob/master/MVC%206%20API.md

Answer

Andrey Nikolaev picture Andrey Nikolaev · Jan 24, 2018

You can find examples here This is how I done this in startup of self hosted owin app:

public void Configuration(IAppBuilder appBuilder)
    {
        HttpConfiguration config = new HttpConfiguration();
        //configure your app

        config.AddApiVersioning(o =>
        {
            o.ReportApiVersions = true;
            o.ApiVersionReader = new UrlSegmentApiVersionReader();
        });
        var constraintResolver = new DefaultInlineConstraintResolver()
        {
            ConstraintMap = { ["apiVersion"] = typeof(ApiVersionRouteConstraint) }
        };
        config.MapHttpAttributeRoutes(constraintResolver);
        SwaggerConfiguration.Configure(config);            
        appBuilder.UseWebApi(config);
    }

Configuration of swagger is very simple, main part here VersionedApiExplorer(ensure, that you passed right groupnameformat of your api, my format was v1, v2, etc):

public static class SwaggerConfiguration
{       
    public static void Configure(HttpConfiguration config)
    {
        var apiExplorer = config.AddVersionedApiExplorer(o => o.GroupNameFormat = "'v'V");
        config.EnableSwagger(
                swagger =>
                {
                    swagger.MultipleApiVersions(
                        (apiDesc, targetApiVersion) => apiDesc.GetGroupName() == targetApiVersion,
                        versionBuilder =>
                        {
                            foreach (var group in apiExplorer.ApiDescriptions)
                            {
                                var description = "";
                                if (group.IsDeprecated) description += "This API deprecated";

                                versionBuilder.Version(group.Name, $"Service API {group.ApiVersion}")
                                    .Description(description);
                            }
                        });
                    swagger.DocumentFilter<VersionFilter>();
                    swagger.OperationFilter<VersionOperationFilter>();
                })
            .EnableSwaggerUi(cfg => cfg.EnableDiscoveryUrlSelector());
    }

In controller add attributes ApiVersion and RoutePrefix

[ApiVersion("1")]
[RoutePrefix("api/v{version:apiVersion}/history")]
public class HistoryController: ApiController

If you confused about VersionFilter and VersionOperationFilter there is code for that. This filters modifies resulting routes and parameters in swagger(without that your route will look like /v{version}/{actionName} and contain required parameter version)

public class VersionFilter : IDocumentFilter
{
    public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
    {
        swaggerDoc.paths = swaggerDoc.paths
            .ToDictionary(
                path => path.Key.Replace("v{version}", swaggerDoc.info.version),
                path => path.Value
            );
    }
}
public class VersionOperationFilter : IOperationFilter
{
    public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
    {
        var version = operation.parameters?.FirstOrDefault(p => p.name == "version");
        if (version != null)
        {
            operation.parameters.Remove(version);
        }
    }
}