使用 NSwag 和 AutoREST 创建一个代码客户端/扁平化控制器 [英] Create one code client / flatten controllers with NSwag and AutoREST
问题描述
我正在尝试使用 NSwag 和 Autorest 为 api 创建代码包装器.
I'm trying to create a code wrapper for an api with NSwag and Autorest.
以前我使用 Swashbuckle 生成 swagger 文件.它以 actionMethod
格式生成了带有 operationIds 的 swagger 文件.这导致 Autorest 生成一个深度为 1 的代码客户端.所有动作都在顶级班级.
Previously I was using Swashbuckle to generate the swagger file. It generated the swagger file with operationIds in the format actionMethod
. This resulted in Autorest generating a code client that was 1-deep. All of the actions were on the top-level class.
出于各种原因,我需要将 swagger generation 更改为 NSwag.这会以 controller_actionMethod
格式生成 operationId.这导致 AutoRest 创建一个复合类,该类公开带有每个控制器操作的单独类.
For various reasons, I needed to change swagger generation to NSwag. This generates operationIds in the format controller_actionMethod
. This results in AutoRest creating a composite class that exposes separate classes with actions for each controller.
怎么可能
- 改变 NSwag 生成 operationId 的方式
- 更改 Autorest 映射 operationId 的方式
注意:我知道我可以手动更改 swagger.json,但我希望保持一致的自动化流程来生成代码客户端.
Note: I know I can manually change the swagger.json, but I'd like to keep a consistent automated process for generating the code client.
推荐答案
似乎没有任何现成的设置,但你可以钩入 NSwag 的生成过程
There doesn't appear to be any readily available settings, but you can hook into the generation process of NSwag
操作处理器
class FlattenOperationsProcessor: IOperationProcessor
{
public async Task<bool> ProcessAsync(OperationProcessorContext context)
{
context.OperationDescription.Operation.OperationId = $"{context.MethodInfo.Name}";
return true;
}
}
然后在Startup.cs中添加
Then add it in Startup.cs
document.OperationProcessors.Add(new FlattenOperationsProcessor());
这篇关于使用 NSwag 和 AutoREST 创建一个代码客户端/扁平化控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!