加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 22:42:34 所属栏目:百科 来源:网络整理
导读:我正在使用swagger-codegen生成数据模型.模板 /// summary/// {{^description}}Gets or Sets {{{name}}}{{/description}}{{#description}}{{description}}{{/description}}/// /summary{{#description}}/// value{{description}}/value{{/description}}[Json
我正在使用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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读