Reputation: 1409
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,
-Thanks
Upvotes: 18
Views: 8952
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
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
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