From df72188bc0c7e4db21d87b288fb6ae97ab5ccfd5 Mon Sep 17 00:00:00 2001 From: wing328 Date: Thu, 18 Jun 2015 22:06:59 +0800 Subject: [PATCH 1/3] add cli support to csharp --- .../languages/CSharpClientCodegen.java | 64 +++++++++++++------ .../main/resources/csharp/ApiClient.mustache | 2 +- .../resources/csharp/ApiException.mustache | 2 +- .../resources/csharp/Configuration.mustache | 3 +- .../src/main/resources/csharp/api.mustache | 6 +- .../src/main/resources/csharp/model.mustache | 2 +- samples/client/petstore/csharp/compile.bat | 2 +- .../csharp/io/swagger/client/Configuration.cs | 1 - 8 files changed, 53 insertions(+), 29 deletions(-) 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 a564ec5f883..33ea13b5e31 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 @@ -7,6 +7,7 @@ import io.swagger.codegen.SupportingFile; import io.swagger.models.properties.ArrayProperty; import io.swagger.models.properties.MapProperty; import io.swagger.models.properties.Property; +import io.swagger.codegen.CliOption; import java.io.File; import java.util.Arrays; @@ -14,15 +15,14 @@ import java.util.HashMap; import java.util.HashSet; public class CSharpClientCodegen extends DefaultCodegen implements CodegenConfig { - protected String invokerPackage = "IO.Swagger.Client"; - protected String groupId = "io.swagger"; - protected String artifactId = "swagger-csharp-client"; - protected String artifactVersion = "1.0.0"; - protected String sourceFolder = "src/main/csharp"; + protected String packageName = "IO.Swagger"; + protected String packageVersion = "1.0.0"; + protected String clientPackage = "IO.Swagger.Client"; + protected String sourceFolder = "src" + File.separator + "main" + File.separator + "csharp"; public CSharpClientCodegen() { super(); - outputFolder = "generated-code/csharp"; + outputFolder = "generated-code" + File.separator + "csharp"; modelTemplateFiles.put("model.mustache", ".cs"); apiTemplateFiles.put("api.mustache", ".cs"); templateDir = "csharp"; @@ -34,17 +34,6 @@ public class CSharpClientCodegen extends DefaultCodegen implements CodegenConfig "abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char", "checked", "class", "const", "continue", "decimal", "default", "delegate", "do", "double", "else", "enum", "event", "explicit", "extern", "false", "finally", "fixed", "float", "for", "foreach", "goto", "if", "implicit", "in", "int", "interface", "internal", "is", "lock", "long", "namespace", "new", "null", "object", "operator", "out", "override", "params", "private", "protected", "public", "readonly", "ref", "return", "sbyte", "sealed", "short", "sizeof", "stackalloc", "static", "string", "struct", "switch", "this", "throw", "true", "try", "typeof", "uint", "ulong", "unchecked", "unsafe", "ushort", "using", "virtual", "void", "volatile", "while") ); - additionalProperties.put("invokerPackage", invokerPackage); - - supportingFiles.add(new SupportingFile("Configuration.mustache", - (sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), "Configuration.cs")); - supportingFiles.add(new SupportingFile("ApiClient.mustache", - (sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), "ApiClient.cs")); - supportingFiles.add(new SupportingFile("ApiException.mustache", - (sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), "ApiException.cs")); - supportingFiles.add(new SupportingFile("Newtonsoft.Json.dll", "bin", "Newtonsoft.Json.dll")); - supportingFiles.add(new SupportingFile("RestSharp.dll", "bin", "RestSharp.dll")); - supportingFiles.add(new SupportingFile("compile.mustache", "", "compile.bat")); languageSpecificPrimitives = new HashSet( Arrays.asList( @@ -85,6 +74,43 @@ public class CSharpClientCodegen extends DefaultCodegen implements CodegenConfig typeMapping.put("map", "Dictionary"); typeMapping.put("object", "Object"); + cliOptions.clear(); + cliOptions.add(new CliOption("packageName", "C# package name (convention: Camel.Case), default: IO.Swagger")); + cliOptions.add(new CliOption("packageVersion", "C# package version, default: 1.0.0")); + + } + + @Override + public void processOpts() { + super.processOpts(); + + if (additionalProperties.containsKey("packageVersion")) { + packageVersion = (String) additionalProperties.get("packageVersion"); + } else { + additionalProperties.put("packageVersion", packageVersion); + } + + if (additionalProperties.containsKey("packageName")) { + packageName = (String) additionalProperties.get("packageName"); + apiPackage = packageName + ".Api"; + modelPackage = packageName + ".Model"; + clientPackage = packageName + ".Client"; + } else { + additionalProperties.put("packageName", packageName); + } + + additionalProperties.put("clientPackage", clientPackage); + + supportingFiles.add(new SupportingFile("Configuration.mustache", + (sourceFolder + File.separator + clientPackage).replace(".", java.io.File.separator), "Configuration.cs")); + supportingFiles.add(new SupportingFile("ApiClient.mustache", + (sourceFolder + File.separator + clientPackage).replace(".", java.io.File.separator), "ApiClient.cs")); + supportingFiles.add(new SupportingFile("ApiException.mustache", + (sourceFolder + File.separator + clientPackage).replace(".", java.io.File.separator), "ApiException.cs")); + supportingFiles.add(new SupportingFile("Newtonsoft.Json.dll", "bin", "Newtonsoft.Json.dll")); + supportingFiles.add(new SupportingFile("RestSharp.dll", "bin", "RestSharp.dll")); + supportingFiles.add(new SupportingFile("compile.mustache", "", "compile.bat")); + } public CodegenType getTag() { @@ -106,11 +132,11 @@ public class CSharpClientCodegen extends DefaultCodegen implements CodegenConfig @Override public String apiFileFolder() { - return (outputFolder + "/" + sourceFolder + "/" + apiPackage().replace('.', '/')).replace('.', File.separatorChar); + return (outputFolder + File.separator + sourceFolder + File.separator + apiPackage()).replace('.', File.separatorChar); } public String modelFileFolder() { - return (outputFolder + "/" + sourceFolder + "/" + modelPackage().replace('.', '/')).replace('.', File.separatorChar); + return (outputFolder + File.separator + sourceFolder + File.separator + modelPackage()).replace('.', File.separatorChar); } @Override diff --git a/modules/swagger-codegen/src/main/resources/csharp/ApiClient.mustache b/modules/swagger-codegen/src/main/resources/csharp/ApiClient.mustache index ad57cffdf34..c6f57bd4a37 100644 --- a/modules/swagger-codegen/src/main/resources/csharp/ApiClient.mustache +++ b/modules/swagger-codegen/src/main/resources/csharp/ApiClient.mustache @@ -8,7 +8,7 @@ using System.Threading.Tasks; using Newtonsoft.Json; using RestSharp; -namespace {{invokerPackage}} { +namespace {{packageName}}.Client { /// /// API client is mainly responible for making the HTTP call to the API backend /// diff --git a/modules/swagger-codegen/src/main/resources/csharp/ApiException.mustache b/modules/swagger-codegen/src/main/resources/csharp/ApiException.mustache index 38a340be249..65c6193b84f 100644 --- a/modules/swagger-codegen/src/main/resources/csharp/ApiException.mustache +++ b/modules/swagger-codegen/src/main/resources/csharp/ApiException.mustache @@ -1,6 +1,6 @@ using System; -namespace {{invokerPackage}} { +namespace {{packageName}}.Client { /// /// API Exception /// diff --git a/modules/swagger-codegen/src/main/resources/csharp/Configuration.mustache b/modules/swagger-codegen/src/main/resources/csharp/Configuration.mustache index 1f5c00a356d..86e35441493 100644 --- a/modules/swagger-codegen/src/main/resources/csharp/Configuration.mustache +++ b/modules/swagger-codegen/src/main/resources/csharp/Configuration.mustache @@ -4,9 +4,8 @@ using System.IO; using System.Linq; using System.Net; using System.Text; -using {{invokerPackage}}; -namespace {{invokerPackage}} { +namespace {{packageName}}.Client { /// /// Represents a set of configuration settings /// diff --git a/modules/swagger-codegen/src/main/resources/csharp/api.mustache b/modules/swagger-codegen/src/main/resources/csharp/api.mustache index e22d5517ab5..652d15a30b3 100644 --- a/modules/swagger-codegen/src/main/resources/csharp/api.mustache +++ b/modules/swagger-codegen/src/main/resources/csharp/api.mustache @@ -2,12 +2,12 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; using RestSharp; -using {{invokerPackage}}; -using {{modelPackage}}; +using {{packageName}}.Client; +using {{packageName}}.Model; {{#imports}} {{/imports}} -namespace {{package}} { +namespace {{packageName}}.Api { {{#operations}} public interface I{{classname}} { diff --git a/modules/swagger-codegen/src/main/resources/csharp/model.mustache b/modules/swagger-codegen/src/main/resources/csharp/model.mustache index ce0e62de192..0d86e0e2b99 100644 --- a/modules/swagger-codegen/src/main/resources/csharp/model.mustache +++ b/modules/swagger-codegen/src/main/resources/csharp/model.mustache @@ -7,7 +7,7 @@ using Newtonsoft.Json; {{#models}} {{#model}} -namespace {{package}} { +namespace {{packageName}}.Model { /// /// {{description}} diff --git a/samples/client/petstore/csharp/compile.bat b/samples/client/petstore/csharp/compile.bat index 63b79e03eed..bb6ae8097ea 100644 --- a/samples/client/petstore/csharp/compile.bat +++ b/samples/client/petstore/csharp/compile.bat @@ -1,3 +1,3 @@ SET CSCPATH=%SYSTEMROOT%\Microsoft.NET\Framework\v4.0.30319 -%CSCPATH%\csc /reference:bin/Newtonsoft.Json.dll;bin/RestSharp.dll /target:library /out:bin/IO.Swagger.Client.dll /recurse:src\*.cs /doc:bin/IO.Swagger.Client.xml +%CSCPATH%\csc /reference:bin/Newtonsoft.Json.dll;bin/RestSharp.dll /target:library /out:bin/.dll /recurse:src\*.cs /doc:bin/.xml diff --git a/samples/client/petstore/csharp/src/main/csharp/io/swagger/client/Configuration.cs b/samples/client/petstore/csharp/src/main/csharp/io/swagger/client/Configuration.cs index 4e7975e3ad0..cf5162b62ea 100644 --- a/samples/client/petstore/csharp/src/main/csharp/io/swagger/client/Configuration.cs +++ b/samples/client/petstore/csharp/src/main/csharp/io/swagger/client/Configuration.cs @@ -4,7 +4,6 @@ using System.IO; using System.Linq; using System.Net; using System.Text; -using IO.Swagger.Client; namespace IO.Swagger.Client { /// From 6ab7be405834c17c3ac9a11ab06c5c43b6f7c9be Mon Sep 17 00:00:00 2001 From: wing328 Date: Thu, 18 Jun 2015 22:20:52 +0800 Subject: [PATCH 2/3] add package version --- .../src/main/resources/csharp/Configuration.mustache | 5 +++++ .../src/main/csharp/io/swagger/client/Configuration.cs | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/modules/swagger-codegen/src/main/resources/csharp/Configuration.mustache b/modules/swagger-codegen/src/main/resources/csharp/Configuration.mustache index 86e35441493..485d97058ce 100644 --- a/modules/swagger-codegen/src/main/resources/csharp/Configuration.mustache +++ b/modules/swagger-codegen/src/main/resources/csharp/Configuration.mustache @@ -11,6 +11,11 @@ namespace {{packageName}}.Client { /// public class Configuration{ + /// + /// Version of the package + /// + public const string Version = "{{packageVersion}}"; + /// /// Gets or sets the API client. This is the default API client for making HTTP calls. /// diff --git a/samples/client/petstore/csharp/src/main/csharp/io/swagger/client/Configuration.cs b/samples/client/petstore/csharp/src/main/csharp/io/swagger/client/Configuration.cs index cf5162b62ea..ab24fc1252c 100644 --- a/samples/client/petstore/csharp/src/main/csharp/io/swagger/client/Configuration.cs +++ b/samples/client/petstore/csharp/src/main/csharp/io/swagger/client/Configuration.cs @@ -11,6 +11,11 @@ namespace IO.Swagger.Client { /// public class Configuration{ + /// + /// Version of the package + /// + public const string Version = "1.0.0"; + /// /// Gets or sets the API client. This is the default API client for making HTTP calls. /// From 6aac24398a3538e664fd56fc5d96b95e5f1ae29d Mon Sep 17 00:00:00 2001 From: wing328 Date: Wed, 24 Jun 2015 14:34:05 +0800 Subject: [PATCH 3/3] update model to support inheritance (#879) --- .../src/main/resources/csharp/model.mustache | 8 ++++---- .../csharp/src/main/csharp/io/swagger/Model/Category.cs | 2 -- .../csharp/src/main/csharp/io/swagger/Model/Order.cs | 2 -- .../csharp/src/main/csharp/io/swagger/Model/Pet.cs | 2 -- .../csharp/src/main/csharp/io/swagger/Model/Tag.cs | 2 -- .../csharp/src/main/csharp/io/swagger/Model/User.cs | 2 -- 6 files changed, 4 insertions(+), 14 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/csharp/model.mustache b/modules/swagger-codegen/src/main/resources/csharp/model.mustache index 0d86e0e2b99..c0fac63b385 100644 --- a/modules/swagger-codegen/src/main/resources/csharp/model.mustache +++ b/modules/swagger-codegen/src/main/resources/csharp/model.mustache @@ -13,7 +13,7 @@ namespace {{packageName}}.Model { /// {{description}} /// [DataContract] - public class {{classname}} { + public class {{classname}}{{#parent}} : {{{parent}}}{{/parent}} { {{#vars}} {{#description}}/* {{{description}}} */{{/description}} [DataMember(Name="{{baseName}}", EmitDefaultValue=false)] @@ -39,11 +39,11 @@ namespace {{packageName}}.Model { /// Get the JSON string presentation of the object /// /// JSON string presentation of the object - public string ToJson() { + public {{#parent}} new {{/parent}}string ToJson() { return JsonConvert.SerializeObject(this, Formatting.Indented); } } - {{/model}} - {{/models}} +{{/model}} +{{/models}} } diff --git a/samples/client/petstore/csharp/src/main/csharp/io/swagger/Model/Category.cs b/samples/client/petstore/csharp/src/main/csharp/io/swagger/Model/Category.cs index 74121762af9..4a410e8220d 100644 --- a/samples/client/petstore/csharp/src/main/csharp/io/swagger/Model/Category.cs +++ b/samples/client/petstore/csharp/src/main/csharp/io/swagger/Model/Category.cs @@ -49,6 +49,4 @@ namespace IO.Swagger.Model { } } - - } diff --git a/samples/client/petstore/csharp/src/main/csharp/io/swagger/Model/Order.cs b/samples/client/petstore/csharp/src/main/csharp/io/swagger/Model/Order.cs index 7451619050c..9e7e706f77e 100644 --- a/samples/client/petstore/csharp/src/main/csharp/io/swagger/Model/Order.cs +++ b/samples/client/petstore/csharp/src/main/csharp/io/swagger/Model/Order.cs @@ -77,6 +77,4 @@ namespace IO.Swagger.Model { } } - - } diff --git a/samples/client/petstore/csharp/src/main/csharp/io/swagger/Model/Pet.cs b/samples/client/petstore/csharp/src/main/csharp/io/swagger/Model/Pet.cs index 0bfba35b1e3..49e6d582f5e 100644 --- a/samples/client/petstore/csharp/src/main/csharp/io/swagger/Model/Pet.cs +++ b/samples/client/petstore/csharp/src/main/csharp/io/swagger/Model/Pet.cs @@ -77,6 +77,4 @@ namespace IO.Swagger.Model { } } - - } diff --git a/samples/client/petstore/csharp/src/main/csharp/io/swagger/Model/Tag.cs b/samples/client/petstore/csharp/src/main/csharp/io/swagger/Model/Tag.cs index 53901491a47..47c87ae9ef1 100644 --- a/samples/client/petstore/csharp/src/main/csharp/io/swagger/Model/Tag.cs +++ b/samples/client/petstore/csharp/src/main/csharp/io/swagger/Model/Tag.cs @@ -49,6 +49,4 @@ namespace IO.Swagger.Model { } } - - } diff --git a/samples/client/petstore/csharp/src/main/csharp/io/swagger/Model/User.cs b/samples/client/petstore/csharp/src/main/csharp/io/swagger/Model/User.cs index e58d0296a06..f2c3db54608 100644 --- a/samples/client/petstore/csharp/src/main/csharp/io/swagger/Model/User.cs +++ b/samples/client/petstore/csharp/src/main/csharp/io/swagger/Model/User.cs @@ -91,6 +91,4 @@ namespace IO.Swagger.Model { } } - - }