According to the helpful answer by the AutoRest team on GitHub (here: https://github.com/Azure/autorest/issues/1497 ) the answer is to use an _
in the OperationId where you want the split to happen. This is my version of the Filter
:
public class SwashbuckleOperationFilter : IOperationFilter
{
public void Apply(Operation operation, OperationFilterContext context)
{
try
{
var pathSegments = context.ApiDescription.RelativePath.Split(new[] { '/' }).ToList();
var version = string.Empty;
var controller = string.Empty;
if (pathSegments.Count > 1)
{
version = pathSegments[0];
controller = pathSegments[1] + "_";
pathSegments = pathSegments.Skip(2).Where(x => !x.Contains("{")).ToList();
}
string httpMethod = FirstCharToUpper(context.ApiDescription.HttpMethod);
var routeName = context.ApiDescription.ActionDescriptor?.AttributeRouteInfo?.Name ?? string.Empty;
operation.OperationId = $"{version}{controller}{httpMethod}{string.Join("", pathSegments)}{routeName}";
}
catch (Exception ex)
{
throw new Exception("Are you missing the [Route(\"v1/[controller]/\")] on your Controller?", ex);
}
}
private string FirstCharToUpper(string input)
{
if (String.IsNullOrEmpty(input))
return string.Empty;
input = input.Trim().ToLower();
return input.First().ToString().ToUpper() + new string(input.Skip(1).ToArray());
}
}
Used like this in the StartUp
:
services.AddSwaggerGen(options =>
{
options.OperationFilter<SwashbuckleOperationFilter>();
//...
}
To turn an API Controller Method
like this:
[Route("v1/[controller]/")]
public class ThingController : Controller
{
[HttpGet("ById/{id}")]
[Produces(typeof(ThingDTO))]
public async Task<IActionResult> GetThing([FromRoute] long id)
{
// Your implementation
}
}
Into this kind of generated service method
:
public class ThingClient : IThingClient
{
private readonly AppSettings appSettings;
private readonly IMapper mapper;
private IV1Thing api;
public ThingClient(IOptions<AppSettings> appSettingsOptions, IMapper mapper)
{
appSettings = appSettingsOptions.Value;
this.mapper = mapper;
}
private IV1Thing service => api ??
(api = new V1Thing(new ThingService(new Uri(appSettings.URLs.ThingService))));
public async Task<IThing> GetByIdAsync(long thingId)
{
return mapper.Map<IThing>(await service.GetByIdAsync(thingId));
}
}