编辑openapi胡子模板以在方法签名中包含其他参数

问题描述

我正在尝试从API JSON自动生成的胡子模板中添加其他参数。

代码: 我在方法签名中为CancellationToken添加了另一个参数作为最后一个参数。

{{#returnType}}System.Threading.Tasks.Task<{{{returnType}}}>{{/returnType}}{{^returnType}}System.Threading.Tasks.Task{{/returnType}} {{operationId}}Async ({{#allParams}}{{{dataType}}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = default({{{dataType}}}){{/optionalMethodArgument}}{{/required}}{{#hasMore}},{{/hasMore}}{{/allParams}},CancellationToken cancellationToken = default(CancellationToken));

问题:此修改工作正常,我们可以生成适当的C#脚本,但是在没有任何内置参数的方法中存在问题。这些方法最终像:

DoThis(,CancellationToken token = default(CancellationToken)).

我们如何为逗号加上一个条件-仅当存在一个或多个内置参数时才编写此逗号?帮助表示赞赏。

解决方法

在您的情况下这非常简单。当前列表中还有更多项目时,您的模板会添加一个逗号,因此,如果您从该逗号中删除条件(因为您总是将要添加另一个项目),则不需要在您的CancellationToken之前专门添加一个。

这具有令人高兴的副作用,如果列表中没有参数,则不会生成逗号(因为逗号只会在每个参数后的之后生成)

{{#returnType}}System.Threading.Tasks.Task<{{{returnType}}}>{{/returnType}}{{^returnType}}System.Threading.Tasks.Task{{/returnType}} {{operationId}}Async ({{#allParams}}{{{dataType}}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = default({{{dataType}}}){{/optionalMethodArgument}}{{/required}},{{/allParams}} CancellationToken cancellationToken = default(CancellationToken));

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...