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; }
3
  • 1
    Are you using an old version of Swagger Codegen? The recent versions should have convert the C# property name into PascalCase automatically. Commented Mar 4, 2017 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! Commented Mar 6, 2017 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 github.com/swagger-api/swagger-codegen/issues/new as someone else may have started working on one already. Commented Mar 6, 2017 at 17:06

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.