user3249448
user3249448

Reputation: 1409

Deprecate specific route out of multiple routes on single Web API method

Hi I have WEB API implementation as shown below. Where we are using multiple routes on single method.

[SwaggerOperation("Update Records By Id")]
[Route("/construction/field-record")]
[Route("/construction/fieldRecord")]
public async Task<IActionResult> UpdateRecord([FromBody] UpdateRecordRequest request)
{
   //  ...
}

Two questions,

  1. How to mark only one route out of two as deprecated?
  2. How to update swagger indicating that route is "Deprecated" on sawagger UI?

-Thanks

Upvotes: 18

Views: 8952

Answers (3)

Matthias M&#252;ller
Matthias M&#252;ller

Reputation: 572

You can add an OperationFilter that checks the RelativePath. This string contains the designation of the route. Just specify the HttpPost/HttpGet-routes

[HttpPost("/construction/field-record")]
[HttpPost("/construction/fieldRecord")]

and then use the following operation filter

public class ExplicitObsoleteRoutes : IOperationFilter
{
    public void Apply(OpenApiOperation operation, OperationFilterContext context)
    {
        if (context.ApiDescription.RelativePath.EndsWith("fieldRecord"))
        {
            operation.Deprecated = true;
        }
    }
}

Upvotes: 10

GrahamB
GrahamB

Reputation: 1428

You can add an OperationFilter that checks the OperationId. This is a string version of the route that has a consistent format; parameters are rendered as "ByXXXX" where XXXX is the name of the variable in the route. You can check this OperationId for the routes you want to deprecate, e.g. In the controller:

[HttpGet]
//Obsolete route
[Route("api/customerId}/account/read/{orderId}")]
//Correct route
[Route("api/customerId}/account/{orderId}/read")]

and then use an operation filter:

public class ExplictObsoleteRoutes : IOperationFilter
{
    public void Apply(Operation operation, OperationFilterContext context)
    {
         if (operation.OperationId.EndsWith("ByOrderIdGet")"))
         {  
             operation.Deprecated = true;
         }
    }
}

Upvotes: 3

Ja9ad335h
Ja9ad335h

Reputation: 5075

as a workaround you can do like this

[SwaggerOperation("Update Records By Id")]
[Route("/construction/field-record")]
public async Task<IActionResult> UpdateRecord([FromBody] UpdateRecordRequest request)
{
   // code
}

[SwaggerOperation("Update Records By Id (Deprecated. Use '/construction/field-record')")]
[Route("/construction/fieldRecord")]
[Obsolete("Deprecated. Use 'UpdateRecord'")]
public async Task<IActionResult> UpdateRecordDeprecated([FromBody] UpdateRecordRequest request)
{
    return UpdateRecord(request);
}

Upvotes: 14

Related Questions