如何在使用 NSwag 生成的客户端方法名称中包含 Http 请求方法名称 [英] How to include Http request method name in client method names generated with NSwag

查看:114
本文介绍了如何在使用 NSwag 生成的客户端方法名称中包含 Http 请求方法名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 NSwag 为 API 生成 C# 客户端时,其中 API 包括可用于多种 Http 请求类型(例如 POST、GET)的端点客户端为每个具有相同基本名称的请求生成一个方法,并加上一个数字.

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.

例如使用此 API:https://api.premiumfunding.net.au/assets/scripts/swagger/v1/swagger.json

架构包含一个端点 /contract 支持 GETPOST 请求,以及一个端点 /contract/{ID} 支持 GETPOSTDELETE 请求.

The schema contains an endpoint /contract that supports GET and POST requests, and an endpoint /contract/{ID} that supports GET, POST and DELETE requests.

生成的客户端有方法:

  • ContractAsync 用于没有 ID 的 GET 请求
  • Contract2Async 用于没有 ID 的 POST 请求
  • Contract3Async 用于带有 ID 的 GET 请求
  • Contract4Async 用于带有 ID 的 POST 请求
  • Contract5Async 用于带有 ID 的 DELETE 请求
  • 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

我希望它生成名为:

  • GetContractAsync 用于没有 ID 的 GET 请求
  • PostContractAsync 用于没有 ID 的 POST 请求
  • GetContractAsync 用于带有 ID 的 GET 请求(方法重载)
  • PostContractAsync 用于带有 ID 的 POST 请求(方法过载)
  • DeleteContractAsync 用于带有 ID 的 DELETE 请求
  • 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.

是否可以配置 NSwag 来生成这些方法名称?

(或者有没有其他工具可以给我这个结果?)

(Or is there an alternative tool that will give me this result?)

推荐答案

您可以实现并提供自己的 IOperationNameGenerator:

You can implement and provide an own IOperationNameGenerator:

https://github.com/RSuter/NSwag/blob/master/src/NSwag.CodeGeneration/OperationNameGenerators/IOoperationNameGenerator.cs

另一种选择是预处理规范并将operationId"更改为controller_operation"形式(基于 NSwag.Core 包的简单控制台应用程序)

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)

这篇关于如何在使用 NSwag 生成的客户端方法名称中包含 Http 请求方法名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆