Forum Discussion

Vilo's avatar
Vilo
New Contributor
2 years ago
Solved

.NET Show deprecated route even if new version exist

On my .NET project I have different version of my ComposerController

For each one, the namespace .Vx is applied for deprecated version and [ApiVersion("X", false/true)] is wrote. Example for V2 :

 

using Swashbuckle.Swagger.Annotations;

namespace CustomNamespace.WebApi.Controller.V2
{
/// <summary></summary>
[Authorize]
[ApiVersion("2", Deprecated = true)]
[RoutePrefix("api/v{version:apiVersion}/composer")]
public class ComposerController : ApiControllerBase
{
[HttpGet]
[Route(Name = "")]
[ResponseType(typeof(IEnumerable<ComposerFileInfo>))]
[SwaggerResponse(HttpStatusCode.BadRequest, "Application not launched!")]
public IHttpActionResult GetComposerFiles()

 

Actually it's the new APIv3 which is the more recent:

using Swashbuckle.Swagger.Annotations;

namespace CustomNamespace.WebApi.Controller
{
/// <summary></summary>
[Authorize]
[ApiVersion("3")]
[RoutePrefix("api/v{version:apiVersion}/composer")]
public class ComposerController : ApiControllerBase
{
[HttpGet]
[Route(Name = "")]
[ResponseType(typeof(IEnumerable<ComposerFileInfo>))]
[SwaggerResponse(HttpStatusCode.BadRequest, "Application not launched!")]
public IHttpActionResult GetComposerFiles()

 

The idea is to show de the deprecated version with " (Deprecated) " value in Swagger UI but to allow the display and the use of the old API version routes.

 

The first route of the v2 doesn't change in the v3, so unfortunally swagger UI seems to decide to not display the first route "api/v2/composer" because "api/v3/composer" exist. Now Swagger UI shows only 3 routes in V2 display wherease there is more.

 

Is it possible fix this ?

  • The explaination :

    Swashbuckle and Swagger only allow one unique route for each method.
    So : api/v{version:apiVersion}/composer for version 2 and api/v{version:apiVersion}/composer for version 3 are the same.


    In order to have really "api/v2/composer" and "api/v3/composer", the HttpConfiguration .NET object must to define :

    httpConfig.AddVersionedApiExplorer(o => o.SubstituteApiVersionInUrl = true);

     

1 Reply

  • Vilo's avatar
    Vilo
    New Contributor

    The explaination :

    Swashbuckle and Swagger only allow one unique route for each method.
    So : api/v{version:apiVersion}/composer for version 2 and api/v{version:apiVersion}/composer for version 3 are the same.


    In order to have really "api/v2/composer" and "api/v3/composer", the HttpConfiguration .NET object must to define :

    httpConfig.AddVersionedApiExplorer(o => o.SubstituteApiVersionInUrl = true);