问题
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:
ContractAsyncfor GET requests without IDContract2Asyncfor POST requests without IDContract3Asyncfor GET requests with IDContract4Asyncfor POST requests with IDContract5Asyncfor DELETE requests with ID
I would like it to generate methods named:
GetContractAsyncfor GET requests without IDPostContractAsyncfor POST requests without IDGetContractAsyncfor GET requests with ID (method overload)PostContractAsyncfor POST requests with ID (method overload)DeleteContractAsyncfor DELETE requests with ID
At 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?)
回答1:
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)
来源:https://stackoverflow.com/questions/49891178/how-to-include-http-request-method-name-in-client-method-names-generated-with-ns