From 559e11e6d23ff35912b96104ea6a1699f9f97d17 Mon Sep 17 00:00:00 2001 From: wing328 Date: Tue, 22 Mar 2016 21:24:41 +0800 Subject: [PATCH] add uwp support --- .../languages/CSharpClientCodegen.java | 14 ++++++++ .../main/resources/csharp/ApiClient.mustache | 32 +++++++++++++++++++ .../resources/csharp/Configuration.mustache | 2 ++ .../main/resources/csharp/Project.mustache | 8 +++++ .../Lib/SwaggerClient.Test/NameTests.cs | 15 +++++---- .../csharp/IO/Swagger/Client/ApiClient.cs | 9 ++++++ .../csharp/IO/Swagger/Client/Configuration.cs | 2 ++ .../src/main/csharp/IO/Swagger/Model/Name.cs | 30 ++++++++--------- .../SwaggerClientTest.userprefs | 6 ++-- 9 files changed, 92 insertions(+), 26 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 0db384adf8b..d02568e973c 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 @@ -34,6 +34,7 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen { private static final Logger LOGGER = LoggerFactory.getLogger(CSharpClientCodegen.class); private static final String NET45 = "v4.5"; private static final String NET35 = "v3.5"; + private static final String UWP = "uwp"; private static final String DATA_TYPE_WITH_ENUM_EXTENSION = "plainDatatypeWithEnum"; protected String packageGuid = "{" + java.util.UUID.randomUUID().toString().toUpperCase() + "}"; @@ -48,6 +49,7 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen { protected String targetFramework = NET45; protected String targetFrameworkNuget = "net45"; protected boolean supportsAsync = Boolean.TRUE; + protected boolean supportsUWP = Boolean.FALSE; protected final Map frameworks; @@ -89,6 +91,7 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen { frameworks = new ImmutableMap.Builder() .put(NET35, ".NET Framework 3.5 compatible") .put(NET45, ".NET Framework 4.5+ compatible") + .put(UWP, "Universal Windows Platform (beta support)") .build(); framework.defaultValue(this.targetFramework); framework.setEnum(frameworks); @@ -156,6 +159,13 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen { if(additionalProperties.containsKey("supportsAsync")){ additionalProperties.remove("supportsAsync"); } + } else if (UWP.equals(this.targetFramework)){ + setTargetFrameworkNuget("uwp"); + setSupportsAsync(Boolean.TRUE); + setSupportsUWP(Boolean.TRUE); + additionalProperties.put("supportsAsync", this.supportsUWP); + additionalProperties.put("supportsUWP", this.supportsAsync); + } else { setTargetFrameworkNuget("net45"); setSupportsAsync(Boolean.TRUE); @@ -453,4 +463,8 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen { public void setSupportsAsync(Boolean supportsAsync){ this.supportsAsync = supportsAsync; } + + public void setSupportsUWP(Boolean supportsUWP){ + this.supportsUWP = supportsUWP; + } } diff --git a/modules/swagger-codegen/src/main/resources/csharp/ApiClient.mustache b/modules/swagger-codegen/src/main/resources/csharp/ApiClient.mustache index 75f43b8aa1c..b67c65572f7 100644 --- a/modules/swagger-codegen/src/main/resources/csharp/ApiClient.mustache +++ b/modules/swagger-codegen/src/main/resources/csharp/ApiClient.mustache @@ -103,7 +103,16 @@ namespace {{packageName}}.Client // add file parameter, if any foreach(var param in fileParams) + { + {{^supportsUWP}} request.AddFile(param.Value.Name, param.Value.Writer, param.Value.FileName, param.Value.ContentType); + {{/supportsUWP}} + {{#supportsUWP}} + byte[] paramWriter = null; + param.Value.Writer = delegate (Stream stream) { paramWriter = ToByteArray(stream); }; + request.AddFile(param.Value.Name, paramWriter, param.Value.FileName, param.Value.ContentType); + {{/supportsUWP}} + } if (postBody != null) // http body (model or byte[]) parameter { @@ -148,7 +157,15 @@ namespace {{packageName}}.Client // set user agent RestClient.UserAgent = Configuration.UserAgent; + {{^supportsUWP}} var response = RestClient.Execute(request); + {{/supportsUWP}} + {{#supportsUWP}} + // TODO: This is obviously not a valid response, but it prevents compiling-errors + // 'RestClient.CallApiAsync(..)' is used instead, because 'RestClient.ExecuteAsync(...)' + // is available for Windows UWP, while 'RestClient.CallApi(...)' is not + var response = new object(); + {{/supportsUWP}} return (Object) response; } {{#supportsAsync}} @@ -444,5 +461,20 @@ namespace {{packageName}}.Client return filename; } } + {{#supportsUWP}} + /// + /// Convert stream to byte array + /// + /// IO stream + /// Byte array + public static byte[] ToByteArray(Stream stream) + { + stream.Position = 0; + byte[] buffer = new byte[stream.Length]; + for (int totalBytesCopied = 0; totalBytesCopied < stream.Length;) + totalBytesCopied += stream.Read(buffer, totalBytesCopied, Convert.ToInt32(stream.Length) - totalBytesCopied); + return buffer; + } + {{/supportsUWP}} } } diff --git a/modules/swagger-codegen/src/main/resources/csharp/Configuration.mustache b/modules/swagger-codegen/src/main/resources/csharp/Configuration.mustache index 6cec586b453..bf7f4d003ad 100644 --- a/modules/swagger-codegen/src/main/resources/csharp/Configuration.mustache +++ b/modules/swagger-codegen/src/main/resources/csharp/Configuration.mustache @@ -270,11 +270,13 @@ namespace {{packageName}}.Client public static String ToDebugReport() { String report = "C# SDK ({{packageName}}) Debug Report:\n"; + {{^supportsUWP}} report += " OS: " + Environment.OSVersion + "\n"; report += " .NET Framework Version: " + Assembly .GetExecutingAssembly() .GetReferencedAssemblies() .Where(x => x.Name == "System.Core").First().Version.ToString() + "\n"; + {{/supportsUWP}} report += " Version of the API: {{version}}\n"; report += " SDK Package Version: {{packageVersion}}\n"; diff --git a/modules/swagger-codegen/src/main/resources/csharp/Project.mustache b/modules/swagger-codegen/src/main/resources/csharp/Project.mustache index d418dc74f8b..d38e5d92d1e 100644 --- a/modules/swagger-codegen/src/main/resources/csharp/Project.mustache +++ b/modules/swagger-codegen/src/main/resources/csharp/Project.mustache @@ -8,7 +8,15 @@ Properties {{packageTitle}} {{packageTitle}} + {{^supportsUWP}} {{targetFramework}} + {{/supportsUWP}} + {{#supportsUWP}} + UAP + 10.0.10240.0 + 10.0.10240.0 + 14 + {{/supportsUWP}} 512 diff --git a/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient.Test/NameTests.cs b/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient.Test/NameTests.cs index 896cb8f12bf..561197ab9f1 100644 --- a/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient.Test/NameTests.cs +++ b/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient.Test/NameTests.cs @@ -31,7 +31,7 @@ namespace IO.Swagger.Test { instance = new Name(); } - + /// /// Clean up after each test /// @@ -39,7 +39,7 @@ namespace IO.Swagger.Test public void Cleanup() { - } + } /// /// Test an instance of Name @@ -50,22 +50,25 @@ namespace IO.Swagger.Test Assert.IsInstanceOf (instance, "instance is a Name"); } + /// - /// Test the property '_Name' + /// Test the property '_Name' /// [Test] public void _NameTest() { - // TODO: unit test for the property '_Name' + // TODO: unit test for the property '_Name' } + /// - /// Test the property 'SnakeCase' + /// Test the property 'SnakeCase' /// [Test] public void SnakeCaseTest() { - // TODO: unit test for the property 'SnakeCase' + // TODO: unit test for the property 'SnakeCase' } + } diff --git a/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Client/ApiClient.cs b/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Client/ApiClient.cs index ec9c793b7d3..25ab8f02693 100644 --- a/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Client/ApiClient.cs +++ b/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Client/ApiClient.cs @@ -103,7 +103,12 @@ namespace IO.Swagger.Client // add file parameter, if any foreach(var param in fileParams) + { + request.AddFile(param.Value.Name, param.Value.Writer, param.Value.FileName, param.Value.ContentType); + + + } if (postBody != null) // http body (model or byte[]) parameter { @@ -148,7 +153,10 @@ namespace IO.Swagger.Client // set user agent RestClient.UserAgent = Configuration.UserAgent; + var response = RestClient.Execute(request); + + return (Object) response; } /// @@ -443,5 +451,6 @@ namespace IO.Swagger.Client return filename; } } + } } diff --git a/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Client/Configuration.cs b/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Client/Configuration.cs index acbaee1e688..9aa1d84a2a8 100644 --- a/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Client/Configuration.cs +++ b/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Client/Configuration.cs @@ -270,11 +270,13 @@ namespace IO.Swagger.Client public static String ToDebugReport() { String report = "C# SDK (IO.Swagger) Debug Report:\n"; + report += " OS: " + Environment.OSVersion + "\n"; report += " .NET Framework Version: " + Assembly .GetExecutingAssembly() .GetReferencedAssemblies() .Where(x => x.Name == "System.Core").First().Version.ToString() + "\n"; + report += " Version of the API: 1.0.0\n"; report += " SDK Package Version: 1.0.0\n"; diff --git a/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Model/Name.cs b/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Model/Name.cs index 663e9d88e4c..55fd01dfb97 100644 --- a/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Model/Name.cs +++ b/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Model/Name.cs @@ -12,7 +12,7 @@ using Newtonsoft.Json.Converters; namespace IO.Swagger.Model { /// - /// Model for testing model name same as property name + /// /// [DataContract] public partial class Name : IEquatable @@ -22,22 +22,16 @@ namespace IO.Swagger.Model /// Initializes a new instance of the class. /// Initializes a new instance of the class. /// - /// _Name (required). + /// _Name. + /// SnakeCase. - public Name(int? _Name = null) + public Name(int? _Name = null, int? SnakeCase = null) { - // to ensure "_Name" is required (not null) - if (_Name == null) - { - throw new InvalidDataException("_Name is a required property for Name and cannot be null"); - } - else - { - this._Name = _Name; - } + this._Name = _Name; + this.SnakeCase = SnakeCase; } - + /// /// Gets or Sets _Name @@ -49,7 +43,7 @@ namespace IO.Swagger.Model /// Gets or Sets SnakeCase /// [DataMember(Name="snake_case", EmitDefaultValue=false)] - public int? SnakeCase { get; private set; } + public int? SnakeCase { get; set; } /// /// Returns the string presentation of the object @@ -60,11 +54,12 @@ namespace IO.Swagger.Model var sb = new StringBuilder(); sb.Append("class Name {\n"); sb.Append(" _Name: ").Append(_Name).Append("\n"); -sb.Append(" SnakeCase: ").Append(SnakeCase).Append("\n"); + sb.Append(" SnakeCase: ").Append(SnakeCase).Append("\n"); + sb.Append("}\n"); return sb.ToString(); } - + /// /// Returns the JSON string presentation of the object /// @@ -120,10 +115,13 @@ sb.Append(" SnakeCase: ").Append(SnakeCase).Append("\n"); { int hash = 41; // Suitable nullity checks etc, of course :) + if (this._Name != null) hash = hash * 59 + this._Name.GetHashCode(); + if (this.SnakeCase != null) hash = hash * 59 + this.SnakeCase.GetHashCode(); + return hash; } } diff --git a/samples/client/petstore/csharp/SwaggerClientTest/SwaggerClientTest.userprefs b/samples/client/petstore/csharp/SwaggerClientTest/SwaggerClientTest.userprefs index d261de4844c..3665a5b7e57 100644 --- a/samples/client/petstore/csharp/SwaggerClientTest/SwaggerClientTest.userprefs +++ b/samples/client/petstore/csharp/SwaggerClientTest/SwaggerClientTest.userprefs @@ -1,11 +1,9 @@  - + - + - -