Skip to content

Commit

Permalink
Added modelPropertyNaming option to the csharp generator - Almost 100…
Browse files Browse the repository at this point in the history
…% based of the implementation of the option for Scala (swagger-api#6305)
  • Loading branch information
Morten Christensen authored and wing328 committed Aug 15, 2017
1 parent 7f50b3c commit e31cef8
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co
protected boolean returnICollection = false;
protected boolean netCoreProjectFileFlag = false;

protected String modelPropertyNaming = "PascalCase";

protected String packageVersion = "1.0.0";
protected String packageName = "IO.Swagger";
protected String packageTitle = "Swagger Library";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ public CSharpClientCodegen() {
framework.setEnum(frameworks);
cliOptions.add(framework);

CliOption modelPropertyNaming = new CliOption(CodegenConstants.MODEL_PROPERTY_NAMING, CodegenConstants.MODEL_PROPERTY_NAMING_DESC);
cliOptions.add(modelPropertyNaming.defaultValue("PascalCase"));

// CLI Switches
addSwitch(CodegenConstants.HIDE_GENERATION_TIMESTAMP,
CodegenConstants.HIDE_GENERATION_TIMESTAMP_DESC,
Expand Down Expand Up @@ -166,6 +169,10 @@ public CSharpClientCodegen() {
public void processOpts() {
super.processOpts();

if (additionalProperties.containsKey(CodegenConstants.MODEL_PROPERTY_NAMING)) {
setModelPropertyNaming((String) additionalProperties.get(CodegenConstants.MODEL_PROPERTY_NAMING));
}

// default HIDE_GENERATION_TIMESTAMP to true
if (!additionalProperties.containsKey(CodegenConstants.HIDE_GENERATION_TIMESTAMP)) {
additionalProperties.put(CodegenConstants.HIDE_GENERATION_TIMESTAMP, Boolean.TRUE.toString());
Expand Down Expand Up @@ -386,6 +393,21 @@ public void processOpts() {
additionalProperties.put("modelDocPath", modelDocPath);
}

public void setModelPropertyNaming(String naming) {
if ("original".equals(naming) || "camelCase".equals(naming) ||
"PascalCase".equals(naming) || "snake_case".equals(naming)) {
this.modelPropertyNaming = naming;
} else {
throw new IllegalArgumentException("Invalid model property naming '" +
naming + "'. Must be 'original', 'camelCase', " +
"'PascalCase' or 'snake_case'");
}
}

public String getModelPropertyNaming() {
return this.modelPropertyNaming;
}

@Override
public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
super.postProcessOperations(objs);
Expand Down Expand Up @@ -611,6 +633,38 @@ public String toEnumVarName(String value, String datatype) {
}
}

@Override
public String toVarName(String name) {
// sanitize name
name = sanitizeName(name);

// if it's all uppper case, do nothing
if (name.matches("^[A-Z_]*$")) {
return name;
}

name = getNameUsingModelPropertyNaming(name);

// for reserved word or word starting with number, append _
if (isReservedWord(name) || name.matches("^\\d.*")) {
name = escapeReservedWord(name);
}

return name;
}

public String getNameUsingModelPropertyNaming(String name) {
switch (CodegenConstants.MODEL_PROPERTY_NAMING_TYPE.valueOf(getModelPropertyNaming())) {
case original: return name;
case camelCase: return camelize(name, true);
case PascalCase: return camelize(name);
case snake_case: return underscore(name);
default: throw new IllegalArgumentException("Invalid model property naming '" +
name + "'. Must be 'original', 'camelCase', " +
"'PascalCase' or 'snake_case'");
}
}


public void setPackageName(String packageName) {
this.packageName = packageName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ protected void setExpectations() {
times = 1;
clientCodegen.setNetCoreProjectFileFlag(false);
times = 1;

clientCodegen.setModelPropertyNaming(CSharpClientOptionsProvider.MODEL_PROPERTY_NAMING);
times = 1;
}};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class CSharpClientOptionsProvider implements OptionsProvider {
public static final String SOURCE_FOLDER_VALUE = "src_csharp";
public static final String PACKAGE_GUID_VALUE = "{894EAEBB-649A-498C-A735-10D0BD7B73E0}";
public static final String ALLOW_UNICODE_IDENTIFIERS_VALUE = "false";
public static final String MODEL_PROPERTY_NAMING = "modelPropertyNaming";

@Override
public String getLanguage() {
Expand Down Expand Up @@ -40,6 +41,7 @@ public Map<String, String> createOptions() {
.put(CodegenConstants.INTERFACE_PREFIX, "X")
.put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE)
.put(CodegenConstants.NETCORE_PROJECT_FILE, "false")
.put(CodegenConstants.MODEL_PROPERTY_NAMING, MODEL_PROPERTY_NAMING)
.build();
}

Expand Down

0 comments on commit e31cef8

Please sign in to comment.