在swagger-codegen中更改生成的数据模型属性的名称

时间:2017-03-03 21:21:49

标签: c# swagger-codegen handlebars.net

我正在使用swagger-codegen生成数据模型。模板

/// <summary>
/// {{^description}}Gets or Sets {{{name}}}{{/description}}{{#description}}{{description}}{{/description}}
/// </summary>{{#description}}
/// <value>{{description}}</value>{{/description}}
[JsonProperty("{{baseName}}")]
public {{{datatype}}} {{name}} { get; {{#isReadOnly}}private {{/isReadOnly}}set; }

产生

/// <summary>
/// Description of property.
/// </summary>
/// <value>Description of property.</value>
[JsonProperty("property_name")]
public String property_name { get; set; }

如何将property name的情况从snake_case更改为PascalCase?我想我必须对{{name}}进行某种转换,但我对把手模板不是很熟悉。

/// <summary>
/// Description of property.
/// </summary>
/// <value>Description of property.</value>
[JsonProperty("property_name")]
public String PropertyName { get; set; }

1 个答案:

答案 0 :(得分:1)

我不知道Swagger Codegen中是否有任何内置,但使用handlebars.net,您可以register a helper将字符串转换为PascalCase:

Handlebars.RegisterHelper("PascalCase", (writer, context, parameters) => {
  // paramaters[0] should be name, convert it to PascalCase here
});

我的c#已经尘土飞扬,以至于我不记得PascalCasing是否有内置的字符串方式,但如果不存在则不应该太难。

然后从您的模板中调用它:

public {{{datatype}}} {{PascalCase name}} ...

编辑:它看起来像Swagger Codegen uses jmustache,并且快速浏览一下,但我认为你可以做something similar with Lambdas