From e31cef8c715eaa143b5fd6590be1442022fe5aec Mon Sep 17 00:00:00 2001 From: Morten Christensen Date: Tue, 15 Aug 2017 09:30:35 +0200 Subject: [PATCH] Added modelPropertyNaming option to the csharp generator - Almost 100% based of the implementation of the option for Scala (#6305) --- .../languages/AbstractCSharpCodegen.java | 2 + .../languages/CSharpClientCodegen.java | 54 +++++++++++++++++++ .../csharp/CSharpClientOptionsTest.java | 3 +- .../options/CSharpClientOptionsProvider.java | 2 + 4 files changed, 60 insertions(+), 1 deletion(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractCSharpCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractCSharpCodegen.java index 435c12337e9..31f98f6a2a6 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractCSharpCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractCSharpCodegen.java @@ -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"; diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/CSharpClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/CSharpClientCodegen.java index bf8adcad205..a605fe36a94 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/CSharpClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/CSharpClientCodegen.java @@ -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, @@ -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()); @@ -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 postProcessOperations(Map objs) { super.postProcessOperations(objs); @@ -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; diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/csharp/CSharpClientOptionsTest.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/csharp/CSharpClientOptionsTest.java index 12766c76ff3..1aa3abc4c54 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/csharp/CSharpClientOptionsTest.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/csharp/CSharpClientOptionsTest.java @@ -58,7 +58,8 @@ protected void setExpectations() { times = 1; clientCodegen.setNetCoreProjectFileFlag(false); times = 1; - + clientCodegen.setModelPropertyNaming(CSharpClientOptionsProvider.MODEL_PROPERTY_NAMING); + times = 1; }}; } } diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/options/CSharpClientOptionsProvider.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/options/CSharpClientOptionsProvider.java index 38bf01d2df1..8e3bf9869d9 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/options/CSharpClientOptionsProvider.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/options/CSharpClientOptionsProvider.java @@ -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() { @@ -40,6 +41,7 @@ public Map 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(); }