diff --git a/modules/swagger-codegen/src/main/resources/csharp/ApiClient.mustache b/modules/swagger-codegen/src/main/resources/csharp/ApiClient.mustache index 87d55ab3f6e..8af5250c471 100644 --- a/modules/swagger-codegen/src/main/resources/csharp/ApiClient.mustache +++ b/modules/swagger-codegen/src/main/resources/csharp/ApiClient.mustache @@ -198,10 +198,12 @@ namespace {{packageName}}.Client { if (obj is DateTime) return ((DateTime)obj).ToString ("u"); - else if (obj is IList) { + else if (obj is IList) + { string flattenString = ""; string separator = ","; - foreach (var param in (IList)obj) { + foreach (var param in (IList)obj) + { flattenString += param.ToString() + separator; } return flattenString.Remove(flattenString.Length - 1);; @@ -318,11 +320,29 @@ namespace {{packageName}}.Client { {{#authMethods}} case "{{name}}": - {{#isApiKey}}{{#isKeyInHeader}}headerParams["{{keyParamName}}"] = GetApiKeyWithPrefix("{{keyParamName}}");{{/isKeyInHeader}}{{#isKeyInQuery}}queryParams["{{keyParamName}}"] = GetApiKeyWithPrefix("{{keyParamName}}");{{/isKeyInQuery}}{{/isApiKey}}{{#isBasic}}headerParams["Authorization"] = "Basic " + Base64Encode(Configuration.Username + ":" + Configuration.Password);{{/isBasic}}{{#isOAuth}}headerParams["Authorization"] = "Bearer " + Configuration.AccessToken;{{/isOAuth}} + {{#isApiKey}}{{#isKeyInHeader}} + var apiKeyValue = GetApiKeyWithPrefix("{{keyParamName}}"); + if (!String.IsNullOrEmpty(apiKeyValue)) + { + headerParams["{{keyParamName}}"] = apiKeyValue; + }{{/isKeyInHeader}}{{#isKeyInQuery}} + var apiKeyValue = GetApiKeyWithPrefix("{{keyParamName}}"); + if (!String.IsNullOrEmpty(apiKeyValue)) + { + queryParams["{{keyParamName}}"] = apiKeyValue; + }{{/isKeyInQuery}}{{/isApiKey}}{{#isBasic}} + if (!String.IsNullOrEmpty(Configuration.Username) && !String.IsNullOrEmpty(Configuration.Password)) + { + headerParams["Authorization"] = "Basic " + Base64Encode(Configuration.Username + ":" + Configuration.Password); + }{{/isBasic}}{{#isOAuth}} + if (!String.IsNullOrEmpty(Configuration.AccessToken)) + { + headerParams["Authorization"] = "Bearer " + Configuration.AccessToken; + }{{/isOAuth}} break; {{/authMethods}} default: - //TODO show warning about security definition not found + //show warning about security definition not found break; } } @@ -335,11 +355,14 @@ namespace {{packageName}}.Client /// /// The accepts array to select from. /// The Accept header to use. - public String SelectHeaderAccept(String[] accepts) { + public String SelectHeaderAccept(String[] accepts) + { if (accepts.Length == 0) return null; + if (accepts.Contains("application/json", StringComparer.OrdinalIgnoreCase)) return "application/json"; + return String.Join(",", accepts); } @@ -350,8 +373,7 @@ namespace {{packageName}}.Client /// Encoded string. public static string Base64Encode(string text) { - var textByte = System.Text.Encoding.UTF8.GetBytes(text); - return System.Convert.ToBase64String(textByte); + return System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(text)); } /// @@ -361,7 +383,8 @@ namespace {{packageName}}.Client /// Object to be casted /// Target type /// Casted object - public static dynamic ConvertType(dynamic source, Type dest) { + public static dynamic ConvertType(dynamic source, Type dest) + { return Convert.ChangeType(source, dest); } 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 36217fde02d..a4f9276c882 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 @@ -198,10 +198,12 @@ namespace IO.Swagger.Client { if (obj is DateTime) return ((DateTime)obj).ToString ("u"); - else if (obj is IList) { + else if (obj is IList) + { string flattenString = ""; string separator = ","; - foreach (var param in (IList)obj) { + foreach (var param in (IList)obj) + { flattenString += param.ToString() + separator; } return flattenString.Remove(flattenString.Length - 1);; @@ -318,15 +320,24 @@ namespace IO.Swagger.Client { case "api_key": - headerParams["api_key"] = GetApiKeyWithPrefix("api_key"); + + var apiKeyValue = GetApiKeyWithPrefix("api_key"); + if (!String.IsNullOrEmpty(apiKeyValue)) + { + headerParams["api_key"] = apiKeyValue; + } break; case "petstore_auth": - headerParams["Authorization"] = "Bearer " + Configuration.AccessToken; + + if (!String.IsNullOrEmpty(Configuration.AccessToken)) + { + headerParams["Authorization"] = "Bearer " + Configuration.AccessToken; + } break; default: - //TODO show warning about security definition not found + //show warning about security definition not found break; } } @@ -339,11 +350,14 @@ namespace IO.Swagger.Client /// /// The accepts array to select from. /// The Accept header to use. - public String SelectHeaderAccept(String[] accepts) { + public String SelectHeaderAccept(String[] accepts) + { if (accepts.Length == 0) return null; + if (accepts.Contains("application/json", StringComparer.OrdinalIgnoreCase)) return "application/json"; + return String.Join(",", accepts); } @@ -354,8 +368,7 @@ namespace IO.Swagger.Client /// Encoded string. public static string Base64Encode(string text) { - var textByte = System.Text.Encoding.UTF8.GetBytes(text); - return System.Convert.ToBase64String(textByte); + return System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(text)); } /// @@ -365,7 +378,8 @@ namespace IO.Swagger.Client /// Object to be casted /// Target type /// Casted object - public static dynamic ConvertType(dynamic source, Type dest) { + public static dynamic ConvertType(dynamic source, Type dest) + { return Convert.ChangeType(source, dest); } diff --git a/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.csproj.FilesWrittenAbsolute.txt b/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.csproj.FilesWrittenAbsolute.txt index b46eed9f9d6..6b370d8d490 100644 --- a/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.csproj.FilesWrittenAbsolute.txt +++ b/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.csproj.FilesWrittenAbsolute.txt @@ -1,6 +1,12 @@ +/Users/williamcheng/Code/csharp/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/.NETFramework,Version=v4.5.AssemblyAttribute.cs +/Users/williamcheng/Code/csharp/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll.mdb +/Users/williamcheng/Code/csharp/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll +/Users/williamcheng/Code/csharp/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.dll +/Users/williamcheng/Code/csharp/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.dll.mdb +/Users/williamcheng/Code/csharp/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/Newtonsoft.Json.dll +/Users/williamcheng/Code/csharp/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/nunit.framework.dll +/Users/williamcheng/Code/csharp/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/RestSharp.dll /Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/.NETFramework,Version=v4.5.AssemblyAttribute.cs -/Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll.mdb -/Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll /Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.dll /Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.dll.mdb /Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/Newtonsoft.Json.dll