简体   繁体   中英

How to include Http request method name in client method names generated with NSwag

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 (eg POST, GET) the client generates a method for each request with the same base name, plus a number.

Eg 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 ID
  • Contract2Async for POST requests without ID
  • Contract3Async for GET requests with ID
  • Contract4Async for POST requests with ID
  • Contract5Async for DELETE requests with ID

I would like it to generate methods named:

  • GetContractAsync for GET requests without ID
  • PostContractAsync for POST requests without ID
  • GetContractAsync for GET requests with ID (method overload)
  • PostContractAsync for POST requests with ID (method overload)
  • DeleteContractAsync for 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?)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM