微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

如何将属性名称的大小写从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

@H_404_45@

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐