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 a200c2f7c86..6f5140495c1 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 @@ -37,6 +37,7 @@ public class CSharpClientCodegen extends DefaultCodegen implements CodegenConfig languageSpecificPrimitives = new HashSet( Arrays.asList( + "String", "string", "bool?", "double?", @@ -68,7 +69,7 @@ public class CSharpClientCodegen extends DefaultCodegen implements CodegenConfig typeMapping.put("number", "double?"); typeMapping.put("datetime", "DateTime?"); typeMapping.put("date", "DateTime?"); - typeMapping.put("file", "FileStream"); + typeMapping.put("file", "String"); typeMapping.put("array", "List"); typeMapping.put("list", "List"); typeMapping.put("map", "Dictionary"); diff --git a/modules/swagger-codegen/src/main/resources/csharp/ApiClient.mustache b/modules/swagger-codegen/src/main/resources/csharp/ApiClient.mustache index e3061aa94bc..0ae71bda25c 100644 --- a/modules/swagger-codegen/src/main/resources/csharp/ApiClient.mustache +++ b/modules/swagger-codegen/src/main/resources/csharp/ApiClient.mustache @@ -120,8 +120,6 @@ namespace {{packageName}}.Client { { if (obj is DateTime) { return ((DateTime)obj).ToString ("u"); - } else if (obj is FileStream) { - return ((FileStream)obj).Name; } else if (obj is List) { return String.Join(",", obj as List); } else { @@ -138,7 +136,7 @@ namespace {{packageName}}.Client { public object Deserialize(string content, Type type, IList headers=null) { if (type.GetType() == typeof(Object)) { // return an object return (Object)content; - } else if (type.Name == "FileStream") { // return a file + } else if (type.Name == "FileStream") { // return a file (full path) // e.g. Content-Disposition: attachment; filename=checkimage.jpp String fileName; String filePath; @@ -157,7 +155,7 @@ namespace {{packageName}}.Client { fileName = filePath + Guid.NewGuid().ToString(); } System.IO.File.WriteAllText (fileName, content); - return File.Open (fileName, FileMode.Open); + return fileName; } else if (type.Name.StartsWith("System.Nullable`1[[System.DateTime")) { // return a datetime object return DateTime.Parse(content, null, System.Globalization.DateTimeStyles.RoundtripKind); } else if (type.Name == "String" || type.Name.StartsWith("System.Nullable")) { // return primitive diff --git a/modules/swagger-codegen/src/main/resources/csharp/api.mustache b/modules/swagger-codegen/src/main/resources/csharp/api.mustache index a2a9a0d156f..de5083b6b22 100644 --- a/modules/swagger-codegen/src/main/resources/csharp/api.mustache +++ b/modules/swagger-codegen/src/main/resources/csharp/api.mustache @@ -121,8 +121,11 @@ namespace {{packageName}}.Api { if (((int)response.StatusCode) >= 400) { throw new ApiException ((int)response.StatusCode, "Error calling {{nickname}}: " + response.Content, response.Content); } - {{#returnType}}return ({{{returnType}}}) ApiClient.Deserialize(response.Content, typeof({{{returnType}}}), response.Headers);{{/returnType}}{{^returnType}} - return;{{/returnType}} + + {{#returnType}} // if return type is "String" (not "string"), it implies a Filestream and should return the file path + String returnTypeString = "{{{returnType}}}"; + Type returnType = returnTypeString == "String" ? typeof(FileStream) : typeof({{{returnType}}}); + return ({{{returnType}}}) ApiClient.Deserialize(response.Content, returnType, response.Headers);{{/returnType}}{{^returnType}}return;{{/returnType}} } /// diff --git a/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/PetApi.cs b/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/PetApi.cs index 609ea7326e5..db51ab1f753 100644 --- a/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/PetApi.cs +++ b/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/PetApi.cs @@ -122,7 +122,7 @@ namespace IO.Swagger.Api { /// Additional data to pass to server /// file to upload /// - void UploadFile (long? petId, string additionalMetadata, FileStream file); + void UploadFile (long? petId, string additionalMetadata, String file); /// /// uploads an image @@ -131,7 +131,7 @@ namespace IO.Swagger.Api { /// Additional data to pass to server /// file to upload /// - Task UploadFileAsync (long? petId, string additionalMetadata, FileStream file); + Task UploadFileAsync (long? petId, string additionalMetadata, String file); } @@ -220,7 +220,7 @@ namespace IO.Swagger.Api { if (((int)response.StatusCode) >= 400) { throw new ApiException ((int)response.StatusCode, "Error calling UpdatePet: " + response.Content, response.Content); } - + return; } @@ -295,7 +295,7 @@ namespace IO.Swagger.Api { if (((int)response.StatusCode) >= 400) { throw new ApiException ((int)response.StatusCode, "Error calling AddPet: " + response.Content, response.Content); } - + return; } @@ -370,7 +370,11 @@ namespace IO.Swagger.Api { if (((int)response.StatusCode) >= 400) { throw new ApiException ((int)response.StatusCode, "Error calling FindPetsByStatus: " + response.Content, response.Content); } - return (List) ApiClient.Deserialize(response.Content, typeof(List), response.Headers); + + // if return type is "String" (not "string"), it implies a Filestream and should return the file path + String returnTypeString = "List"; + Type returnType = returnTypeString == "String" ? typeof(FileStream) : typeof(List); + return (List) ApiClient.Deserialize(response.Content, returnType, response.Headers); } /// @@ -443,7 +447,11 @@ namespace IO.Swagger.Api { if (((int)response.StatusCode) >= 400) { throw new ApiException ((int)response.StatusCode, "Error calling FindPetsByTags: " + response.Content, response.Content); } - return (List) ApiClient.Deserialize(response.Content, typeof(List), response.Headers); + + // if return type is "String" (not "string"), it implies a Filestream and should return the file path + String returnTypeString = "List"; + Type returnType = returnTypeString == "String" ? typeof(FileStream) : typeof(List); + return (List) ApiClient.Deserialize(response.Content, returnType, response.Headers); } /// @@ -519,7 +527,11 @@ namespace IO.Swagger.Api { if (((int)response.StatusCode) >= 400) { throw new ApiException ((int)response.StatusCode, "Error calling GetPetById: " + response.Content, response.Content); } - return (Pet) ApiClient.Deserialize(response.Content, typeof(Pet), response.Headers); + + // if return type is "String" (not "string"), it implies a Filestream and should return the file path + String returnTypeString = "Pet"; + Type returnType = returnTypeString == "String" ? typeof(FileStream) : typeof(Pet); + return (Pet) ApiClient.Deserialize(response.Content, returnType, response.Headers); } /// @@ -602,7 +614,7 @@ namespace IO.Swagger.Api { if (((int)response.StatusCode) >= 400) { throw new ApiException ((int)response.StatusCode, "Error calling UpdatePetWithForm: " + response.Content, response.Content); } - + return; } @@ -689,7 +701,7 @@ namespace IO.Swagger.Api { if (((int)response.StatusCode) >= 400) { throw new ApiException ((int)response.StatusCode, "Error calling DeletePet: " + response.Content, response.Content); } - + return; } @@ -742,7 +754,7 @@ namespace IO.Swagger.Api { /// Additional data to pass to server /// file to upload /// - public void UploadFile (long? petId, string additionalMetadata, FileStream file) { + public void UploadFile (long? petId, string additionalMetadata, String file) { // verify the required parameter 'petId' is set @@ -776,7 +788,7 @@ namespace IO.Swagger.Api { if (((int)response.StatusCode) >= 400) { throw new ApiException ((int)response.StatusCode, "Error calling UploadFile: " + response.Content, response.Content); } - + return; } @@ -787,7 +799,7 @@ namespace IO.Swagger.Api { /// Additional data to pass to server /// file to upload /// - public async Task UploadFileAsync (long? petId, string additionalMetadata, FileStream file) { + public async Task UploadFileAsync (long? petId, string additionalMetadata, String file) { // verify the required parameter 'petId' is set diff --git a/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/StoreApi.cs b/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/StoreApi.cs index 67276bbce88..b7bab55ed9a 100644 --- a/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/StoreApi.cs +++ b/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/StoreApi.cs @@ -150,7 +150,11 @@ namespace IO.Swagger.Api { if (((int)response.StatusCode) >= 400) { throw new ApiException ((int)response.StatusCode, "Error calling GetInventory: " + response.Content, response.Content); } - return (Dictionary) ApiClient.Deserialize(response.Content, typeof(Dictionary), response.Headers); + + // if return type is "String" (not "string"), it implies a Filestream and should return the file path + String returnTypeString = "Dictionary"; + Type returnType = returnTypeString == "String" ? typeof(FileStream) : typeof(Dictionary); + return (Dictionary) ApiClient.Deserialize(response.Content, returnType, response.Headers); } /// @@ -221,7 +225,11 @@ namespace IO.Swagger.Api { if (((int)response.StatusCode) >= 400) { throw new ApiException ((int)response.StatusCode, "Error calling PlaceOrder: " + response.Content, response.Content); } - return (Order) ApiClient.Deserialize(response.Content, typeof(Order), response.Headers); + + // if return type is "String" (not "string"), it implies a Filestream and should return the file path + String returnTypeString = "Order"; + Type returnType = returnTypeString == "String" ? typeof(FileStream) : typeof(Order); + return (Order) ApiClient.Deserialize(response.Content, returnType, response.Headers); } /// @@ -297,7 +305,11 @@ namespace IO.Swagger.Api { if (((int)response.StatusCode) >= 400) { throw new ApiException ((int)response.StatusCode, "Error calling GetOrderById: " + response.Content, response.Content); } - return (Order) ApiClient.Deserialize(response.Content, typeof(Order), response.Headers); + + // if return type is "String" (not "string"), it implies a Filestream and should return the file path + String returnTypeString = "Order"; + Type returnType = returnTypeString == "String" ? typeof(FileStream) : typeof(Order); + return (Order) ApiClient.Deserialize(response.Content, returnType, response.Headers); } /// @@ -376,7 +388,7 @@ namespace IO.Swagger.Api { if (((int)response.StatusCode) >= 400) { throw new ApiException ((int)response.StatusCode, "Error calling DeleteOrder: " + response.Content, response.Content); } - + return; } diff --git a/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/UserApi.cs b/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/UserApi.cs index 2d87996dd9e..27cd4afc92e 100644 --- a/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/UserApi.cs +++ b/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/UserApi.cs @@ -212,7 +212,7 @@ namespace IO.Swagger.Api { if (((int)response.StatusCode) >= 400) { throw new ApiException ((int)response.StatusCode, "Error calling CreateUser: " + response.Content, response.Content); } - + return; } @@ -287,7 +287,7 @@ namespace IO.Swagger.Api { if (((int)response.StatusCode) >= 400) { throw new ApiException ((int)response.StatusCode, "Error calling CreateUsersWithArrayInput: " + response.Content, response.Content); } - + return; } @@ -362,7 +362,7 @@ namespace IO.Swagger.Api { if (((int)response.StatusCode) >= 400) { throw new ApiException ((int)response.StatusCode, "Error calling CreateUsersWithListInput: " + response.Content, response.Content); } - + return; } @@ -439,7 +439,11 @@ namespace IO.Swagger.Api { if (((int)response.StatusCode) >= 400) { throw new ApiException ((int)response.StatusCode, "Error calling LoginUser: " + response.Content, response.Content); } - return (string) ApiClient.Deserialize(response.Content, typeof(string), response.Headers); + + // if return type is "String" (not "string"), it implies a Filestream and should return the file path + String returnTypeString = "string"; + Type returnType = returnTypeString == "String" ? typeof(FileStream) : typeof(string); + return (string) ApiClient.Deserialize(response.Content, returnType, response.Headers); } /// @@ -512,7 +516,7 @@ namespace IO.Swagger.Api { if (((int)response.StatusCode) >= 400) { throw new ApiException ((int)response.StatusCode, "Error calling LogoutUser: " + response.Content, response.Content); } - + return; } @@ -588,7 +592,11 @@ namespace IO.Swagger.Api { if (((int)response.StatusCode) >= 400) { throw new ApiException ((int)response.StatusCode, "Error calling GetUserByName: " + response.Content, response.Content); } - return (User) ApiClient.Deserialize(response.Content, typeof(User), response.Headers); + + // if return type is "String" (not "string"), it implies a Filestream and should return the file path + String returnTypeString = "User"; + Type returnType = returnTypeString == "String" ? typeof(FileStream) : typeof(User); + return (User) ApiClient.Deserialize(response.Content, returnType, response.Headers); } /// @@ -669,7 +677,7 @@ namespace IO.Swagger.Api { if (((int)response.StatusCode) >= 400) { throw new ApiException ((int)response.StatusCode, "Error calling UpdateUser: " + response.Content, response.Content); } - + return; } @@ -752,7 +760,7 @@ namespace IO.Swagger.Api { if (((int)response.StatusCode) >= 400) { throw new ApiException ((int)response.StatusCode, "Error calling DeleteUser: " + response.Content, response.Content); } - + return; } 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 648f10df280..a6b5c3a91da 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 @@ -120,8 +120,6 @@ namespace IO.Swagger.Client { { if (obj is DateTime) { return ((DateTime)obj).ToString ("u"); - } else if (obj is FileStream) { - return ((FileStream)obj).Name; } else if (obj is List) { return String.Join(",", obj as List); } else { @@ -138,7 +136,7 @@ namespace IO.Swagger.Client { public object Deserialize(string content, Type type, IList headers=null) { if (type.GetType() == typeof(Object)) { // return an object return (Object)content; - } else if (type.Name == "FileStream") { // return a file + } else if (type.Name == "FileStream") { // return a file (full path) // e.g. Content-Disposition: attachment; filename=checkimage.jpp String fileName; String filePath; @@ -157,7 +155,7 @@ namespace IO.Swagger.Client { fileName = filePath + Guid.NewGuid().ToString(); } System.IO.File.WriteAllText (fileName, content); - return File.Open (fileName, FileMode.Open); + return fileName; } else if (type.Name.StartsWith("System.Nullable`1[[System.DateTime")) { // return a datetime object return DateTime.Parse(content, null, System.Globalization.DateTimeStyles.RoundtripKind); } else if (type.Name == "String" || type.Name.StartsWith("System.Nullable")) { // return primitive diff --git a/samples/client/petstore/csharp/SwaggerClientTest/SwaggerClientTest.userprefs b/samples/client/petstore/csharp/SwaggerClientTest/SwaggerClientTest.userprefs index f202e578741..3bc490c9a58 100644 --- a/samples/client/petstore/csharp/SwaggerClientTest/SwaggerClientTest.userprefs +++ b/samples/client/petstore/csharp/SwaggerClientTest/SwaggerClientTest.userprefs @@ -1,10 +1,9 @@  - + - - - + + diff --git a/samples/client/petstore/csharp/SwaggerClientTest/TestPet.cs b/samples/client/petstore/csharp/SwaggerClientTest/TestPet.cs index 8e6ea0f99d2..bc8fe0fb757 100644 --- a/samples/client/petstore/csharp/SwaggerClientTest/TestPet.cs +++ b/samples/client/petstore/csharp/SwaggerClientTest/TestPet.cs @@ -1,5 +1,6 @@ using NUnit.Framework; using System; +using System.IO; using System.Collections.Generic; using IO.Swagger.Api; using IO.Swagger.Model; @@ -48,6 +49,30 @@ namespace SwaggerClient.TestPet } + [Test ()] + public void TestGetPetByIdAsync () + { + PetApi petApi = new PetApi (); + var task = petApi.GetPetByIdAsync (petId); + Pet response = task.Result; + Assert.IsInstanceOf (response, "Response is a Pet"); + + Assert.AreEqual ("Csharp test", response.Name); + Assert.AreEqual ("available", response.Status); + + Assert.IsInstanceOf> (response.Tags, "Response.Tags is a Array"); + Assert.AreEqual (petId, response.Tags [0].Id); + Assert.AreEqual ("sample tag name1", response.Tags [0].Name); + + Assert.IsInstanceOf> (response.PhotoUrls, "Response.PhotoUrls is a Array"); + Assert.AreEqual ("sample photoUrls", response.PhotoUrls [0]); + + Assert.IsInstanceOf (response.Category, "Response.Category is a Category"); + Assert.AreEqual (56, response.Category.Id); + Assert.AreEqual ("sample category name2", response.Category.Name); + + } + [Test ()] public void TestGetPetById () { @@ -89,6 +114,15 @@ namespace SwaggerClient.TestPet Assert.AreEqual (56, response.Category.Id); } + [Test ()] + public void TestUploadFile () + { + PetApi petApi = new PetApi (); + //NOTE: please provide a valid file (full path) + petApi.UploadFile(petId, "new form name", "/var/tmp/small.gif"); + } + + [Test ()] public void TestFindPetByStatus () { diff --git a/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll b/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll index 60d81e320bc..b9ffd217f0c 100755 Binary files a/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll and b/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll differ diff --git a/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll.mdb b/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll.mdb index 53958fc7ab6..2269827753b 100644 Binary files a/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll.mdb and b/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll.mdb differ 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 9afb7990f57..ff57f8c80e3 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,8 +1,8 @@ /Users/williamcheng/Code/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/.NETFramework,Version=v4.5.AssemblyAttribute.cs -/Users/williamcheng/Code/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/nunit.framework.dll -/Users/williamcheng/Code/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/Newtonsoft.Json.dll -/Users/williamcheng/Code/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/RestSharp.dll /Users/williamcheng/Code/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll.mdb /Users/williamcheng/Code/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll /Users/williamcheng/Code/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.dll /Users/williamcheng/Code/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.dll.mdb +/Users/williamcheng/Code/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/nunit.framework.dll +/Users/williamcheng/Code/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/Newtonsoft.Json.dll +/Users/williamcheng/Code/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/RestSharp.dll diff --git a/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.dll b/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.dll index 60d81e320bc..b9ffd217f0c 100755 Binary files a/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.dll and b/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.dll differ diff --git a/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.dll.mdb b/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.dll.mdb index 53958fc7ab6..2269827753b 100644 Binary files a/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.dll.mdb and b/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.dll.mdb differ