I generate an Angular client for a.Net Core 3.1 API using NSwagStudio. The API includes endpoints that can be used with multiple Http request types (e.g. POST, GET). The client generates a method for each request with the same base name, plus a number.
The schema contains an endpoint /contract that supports GET and POST requests, and an endpoint /contract/{ID} that supports GET, POST and DELETE requests.
The generated client has methods like :
ContractAsync for GET requests without IDContract2Async for POST requests without IDContract3Async for GET requests with IDContract4Async for POST requests with IDContract5Async for DELETE requests with IDI would like it to generate methods named:
GetContractAsync for GET requests without IDPostContractAsync for POST requests without IDetc
The answer to the question is to "implement and provide an own IOperationNameGenerator" See https://stackoverflow.com/a/49935417/4180382
I have no clue how to implement and provide this IOperationNameGenerator.
The "Web Api via reflection" tab contains several custom implementations but doesn't mention "IOperationNameGenerator"
How do I implement IOperationNameGenerator?
In your Startup.cs do this:
services.AddSwaggerGen(c =>
{
...
c.CustomOperationIds(d => d.ActionDescriptor is ControllerActionDescriptor controllerActionDescriptor
? controllerActionDescriptor.MethodInfo.Name
: d.ActionDescriptor.AttributeRouteInfo?.Name);
});
The same can also be set via c.SwaggerGeneratorOptions.OperationIdSelector
Note that ActionDescriptor.AttributeRouteInfo?.Name is the default I used from the source code here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With