4

I am generating a data model using swagger-codegen. The template

/// <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; }

generates

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

How can I change the case of the property name from snake_case to PascalCase? I imagine I have to do some kind of transformation to {{name}} but I'm not very familiar with handlebars templates.

/// <summary>
/// Description of property.
/// </summary>
/// <value>Description of property.</value>
[JsonProperty("property_name")]
public String PropertyName { get; set; }
taj
  • 1,128
  • 1
  • 9
  • 23
  • 1
    Are you using an old version of Swagger Codegen? The recent versions should have convert the C# property name into PascalCase automatically. – William Cheng Mar 04 '17 at 15:11
  • You're right - this works with the CSharp generator! I'm trrying to create a generator for .NET Standard so I'm starting fresh. I didn't see the C# generator has this. Thanks! – taj Mar 06 '17 at 16:24
  • 1
    There's also a C# 2.0 generator, which is used mainly by Unity developers. If you want to build a new generator, I would suggest you to start a discussion via https://github.com/swagger-api/swagger-codegen/issues/new as someone else may have started working on one already. – William Cheng Mar 06 '17 at 17:06

1 Answers1

1

I don't know if there's anything built into Swagger Codegen, but with handlebars.net, you can register a helper to convert the string to PascalCase:

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

My c# is dusty enough that I don't remember if there is a builtin way of PascalCasing a string, but it shouldn't be too hard to do if there isn't.

Then call it from your template:

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

Edit: It looks like Swagger Codegen uses jmustache under the hood, and from a quick glance, but I think you can do something similar with Lambdas

Nathan Friedly
  • 7,837
  • 3
  • 42
  • 59