我正在使用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; }

最佳答案

我不知道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

关于c# - 在swagger-codegen中更改生成的数据模型属性的名称,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42588643/

10-12 00:36