简体   繁体   English

编辑 openapi mustache 模板以在方法签名中包含附加参数

[英]Edit openapi mustache template to include an additional parameter in method signature

I am trying to add an additional parameter in a mustache template autogenerated from API JSON.我正在尝试在从 API JSON 自动生成的胡子模板中添加一个附加参数。

Code: I have added an additional parameter for CancellationToken in the method signature as last parameter.代码:我在方法签名中为 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));

Problem: This modification works fine and we can generate proper C# scripts, but we have a problem in methods that don't have any inbuilt parameter.问题:此修改工作正常,我们可以生成正确的 C# 脚本,但是我们在没有任何内置参数的方法中遇到了问题。 Those methods end up being like:这些方法最终是这样的:

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

How do we put a condition for the comma which says - write this comma only when there are one or more inbuilt parameters?我们如何为逗号设置一个条件,即 - 只有当有一个或多个内置参数时才写这个逗号? Helps appreciated.帮助赞赏。

It's pretty straightforward in your scenario.这在您的场景中非常简单。 Your template adds a comma currently when there are more items left in the list, so if you remove the condition from that comma (because you're always going to be adding another item) you don't need to specifically add one before your CancellationToken.当列表中还有更多项目时,您的模板当前会添加一个逗号,因此如果您从该逗号中删除条件(因为您总是要添加另一个项目),您不需要在 CancellationToken 之前专门添加一个.

This has the happy side-effect that if you have no parameters in your list, no commas will be generated (because they're only generated after each parameter)这有一个愉快的副作用,如果您的列表中没有参数,则不会生成逗号(因为它们仅每个参数之后生成)

{{#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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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