Problem with multiple API versions and Litium swagger implementation

Is there a working example how to implement and use multiple API versions with IApiVersionCollection?

We want both v1 and v2 of a controller method to be visible in swagger, but we only seem to get one version to show up.

Litium version: 7.7.5

Have you added the ApiVersion attribute on the controller on methods in the controller that specifying the different version that exists.

With the ApiVersionLimiter it’s also possible to specify a range of versions a controller or method belongs to.

Both attributes exists in the Litium.Web.WebApi namespace.

@patric.forsgard

Yes, below example will show both versions of /test/methodB in swagger, but will throw exception on requests:

"Multiple actions were found that match the request: \r\nTestMethodBv1 on type Test.Api.TestController\r\nTestMethodBv2 on type Test.Api.TestController"

If we remove ApiVersion on the controller, v2 will not be visible in swagger.

    [ApiCollection("TestApi")]
    [Litium.Web.WebApi.ApiVersion("1.0")]
    [Litium.Web.WebApi.ApiVersion("2.0")]
    public class TestController : ApiController
    {
        [HttpPost]
        [Litium.Web.WebApi.ApiVersion("1.0")]
        [Litium.Web.WebApi.ApiVersion("2.0")]
        [Route("test/methodA", Name = "TestMethodA")]
        public IHttpActionResult TestMethodA()
        {
            return Ok();
        }

        [HttpPost]
        [Litium.Web.WebApi.ApiVersion("1.0")]
        [Route("test/methodB", Name = "TestMethodBv1")]
        public IHttpActionResult TestMethodBv1()
        {
            return Ok("1.0");
        }

        [HttpPost]
        [Litium.Web.WebApi.ApiVersion("2.0")]
        [Route("test/methodB", Name = "TestMethodBv2")]
        public IHttpActionResult TestMethodBv2()
        {
            return Ok("2.0");
        }
    }

    public class TestApiCollection : IApiVersionCollection
    {
        public void Configure(OpenApiDocumentBuilder builder)
        {
        }

        public bool IsAuthorized()
        {
            return true;
        }

        public string Name => "Test Api";
        public string Collection => "TestApi";

        public void Configure(ApiVersioningOptions options)
        {
            options.ApiVersionHeaderParameter = "x-api-version";
            options.ReportApiVersions = true;
            options.ApiVersionQuerystringParameter = "x-api-version";
            options.DefaultApiVersion = new ApiVersion(1, 0);
            options.AssumeDefaultVersionWhenUnspecified = true;
        }
    }```

@patric.forsgard

Is this a bug or is there a way to make this work?

Try to add the RoutePrefix on the class to instruct ASP.NET WebAPI to use Direct Routes instead of by convention.

I haven’t had time to make an example for use but it may also be that you should use the ApiVersionLimiter instead of the ApiVersion on the methods and only use the ApiVersion on the class.

Using ApiVersionLimiter on the methods will make the routes work as expected without any exceptions being thrown.

However, the v2 version will disappear from the swagger definition. So, that’s the remaining issue right now.

Bug reported:

https://docs.litium.com/support/bugs/bug_details?id=59805

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.