When I generate a C# client for an API using NSwag, where 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.
E.g. Using this API: https://api.premiumfunding.net.au/assets/scripts/swagger/v1/swagger.json
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:
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 IDGetContractAsync
for GET requests with ID (method overload)PostContractAsync
for POST requests with ID (method overload)DeleteContractAsync
for DELETE requests with IDAt the moment I am just renaming the methods manually.
Is it possible to configure NSwag to generated these method names?
(Or is there an alternative tool that will give me this result?)
The NSwag project provides tools to generate OpenAPI specifications from existing ASP.NET Web API controllers and client code from these OpenAPI specifications. The project combines the functionality of Swashbuckle (OpenAPI/Swagger generation) and AutoRest (client generation) in one toolchain.
nswag.json defines a set of parameters required by NSwag for generating client code like input assembly and output file path, as well as other different options allowing to adjust the shape of output code to our needs. The easiest way to generate the manifest file is to use Windows UI application called NSwag Studio.
Download NSwag Studio and install it. Start NSwag Studio and enter the “Specification URL” from the previous step of this guide (e.q. “https://localhost:5002/swagger/v1/swagger.json”). Select “TypeScript Client” as “Outputs”: Make sure your API project from the previous step is running.
You can implement and provide an own IOperationNameGenerator:
https://github.com/RSuter/NSwag/blob/master/src/NSwag.CodeGeneration/OperationNameGenerators/IOperationNameGenerator.cs
Another option would be to preprocess the spec and change the “operationId”s to the form “controller_operation” (simple console app based on the NSwag.Core package)
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