简体   繁体   中英

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

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);
}

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;
        }
    }
}

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, eg 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;
         }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM