diff --git a/README.md b/README.md index 677a6e338b0..1e8a3059362 100644 --- a/README.md +++ b/README.md @@ -1000,7 +1000,7 @@ If you want to join the committee, please kindly apply by sending an email to te | Bash | @frol (2017/07) @bkryza (2017/08) @kenjones-cisco (2017/09) | | C | @zhemant (2018/11) @ityuhui (2019/12) @michelealbano (2020/03) | | C++ | @ravinikam (2017/07) @stkrwork (2017/07) @etherealjoy (2018/02) @martindelille (2018/03) @muttleyxd (2019/08) | -| C# | @mandrean (2017/08) @frankyjuang (2019/09) @shibayan (2020/02) | +| C# | @mandrean (2017/08) @frankyjuang (2019/09) @shibayan (2020/02) @Blackclaws (2021/03) | | Clojure | | | Dart | @swipesight (2018/09) @jaumard (2018/09) @josh-burton (2019/12) @amondnet (2019/12) @sbu-WBT (2020/12) @kuhnroyal (2020/12) @agilob (2020/12) | | Eiffel | @jvelilla (2017/09) | diff --git a/modules/openapi-generator/src/main/resources/csharp-netcore/libraries/httpclient/ApiClient.mustache b/modules/openapi-generator/src/main/resources/csharp-netcore/libraries/httpclient/ApiClient.mustache index e5d53c2807e..d775c4d9817 100644 --- a/modules/openapi-generator/src/main/resources/csharp-netcore/libraries/httpclient/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/csharp-netcore/libraries/httpclient/ApiClient.mustache @@ -339,11 +339,21 @@ namespace {{packageName}}.Client Cookies = new List() }; + // process response headers, e.g. Access-Control-Allow-Methods if (response.Headers != null) { foreach (var responseHeader in response.Headers) { + transformed.Headers.Add(responseHeader.Key, ClientUtils.ParameterToString(responseHeader.Value)); + } + } + // process response content headers, e.g. Content-Type + if (response.Content.Headers != null) + { + foreach (var responseHeader in response.Content.Headers) + { + transformed.Headers.Add(responseHeader.Key, ClientUtils.ParameterToString(responseHeader.Value)); } } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/.openapi-generator-ignore b/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/.openapi-generator-ignore index 7484ee590a3..1e0b5c7bb7f 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/.openapi-generator-ignore +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/.openapi-generator-ignore @@ -21,3 +21,4 @@ #docs/*.md # Then explicitly reverse the ignore rule for a single file: #!docs/README.md +src/Org.OpenAPITools.Test/Org.OpenAPITools.Test.csproj diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/.openapi-generator/FILES b/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/.openapi-generator/FILES index f96493ec6a8..e6b9861f332 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/.openapi-generator/FILES +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/.openapi-generator/FILES @@ -84,7 +84,6 @@ docs/UserApi.md docs/Whale.md docs/Zebra.md git_push.sh -src/Org.OpenAPITools.Test/Org.OpenAPITools.Test.csproj src/Org.OpenAPITools/Api/AnotherFakeApi.cs src/Org.OpenAPITools/Api/DefaultApi.cs src/Org.OpenAPITools/Api/FakeApi.cs diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools.Test/Api/PetApiTests.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools.Test/Api/PetApiTests.cs index 6192abece90..f64ed20e7cf 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools.Test/Api/PetApiTests.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools.Test/Api/PetApiTests.cs @@ -1,9 +1,10 @@ -/* +/* * OpenAPI Petstore * * This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ * - * The version of the OpenAPI document: 1.0.0 + * OpenAPI spec version: 1.0.0 + * * Generated by: https://github.com/openapitools/openapi-generator.git */ @@ -17,13 +18,12 @@ using Xunit; using Org.OpenAPITools.Client; using Org.OpenAPITools.Api; -// uncomment below to import models -//using Org.OpenAPITools.Model; +using Org.OpenAPITools.Model; -namespace Org.OpenAPITools.Test.Api +namespace Org.OpenAPITools.Test { /// - /// Class for testing PetApi + /// Class for testing PetApi /// /// /// This file is automatically generated by OpenAPI Generator (https://openapi-generator.tech). @@ -33,85 +33,215 @@ namespace Org.OpenAPITools.Test.Api { private PetApi instance; + private long petId = 11088; + + /// + /// Create a Pet object + /// + private Pet createPet() + { + // create pet + Pet p = new Pet(name: "Csharp test", photoUrls: new List { "http://petstore.com/csharp_test" }); + p.Id = petId; + //p.Name = "Csharp test"; + p.Status = Pet.StatusEnum.Available; + // create Category object + Category category = new Category(); + category.Id = 56; + category.Name = "sample category name2"; + List photoUrls = new List(new String[] { "sample photoUrls" }); + // create Tag object + Tag tag = new Tag(); + tag.Id = petId; + tag.Name = "csharp sample tag name1"; + List tags = new List(new Tag[] { tag }); + p.Tags = tags; + p.Category = category; + p.PhotoUrls = photoUrls; + + return p; + } + + /// + /// Convert string to byte array + /// + private byte[] GetBytes(string str) + { + byte[] bytes = new byte[str.Length * sizeof(char)]; + System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length); + return bytes; + } + public PetApiTests() - { + { instance = new PetApi(); - } - public void Dispose() - { - // Cleanup when everything is done. - } + // create pet + Pet p = createPet(); + // add pet before testing + PetApi petApi = new PetApi("http://petstore.swagger.io/v2"); + petApi.AddPet(p); + } + /// /// Test an instance of PetApi /// [Fact] public void InstanceTest() { - // TODO uncomment below to test 'IsType' PetApi - //Assert.IsType(instance); + Assert.IsType(instance); } + /// /// Test AddPet /// [Fact] public void AddPetTest() { - // TODO uncomment below to test the method and replace null with proper value - //Pet pet = null; - //instance.AddPet(pet); + // create pet + Pet p = createPet(); + instance.AddPet(p); } - + /// /// Test DeletePet /// [Fact] public void DeletePetTest() { - // TODO uncomment below to test the method and replace null with proper value - //long petId = null; - //string apiKey = null; - //instance.DeletePet(petId, apiKey); + // no need to test as it'c covered by Cleanup() already } - + /// /// Test FindPetsByStatus /// [Fact] public void FindPetsByStatusTest() { - // TODO uncomment below to test the method and replace null with proper value - //List status = null; - //var response = instance.FindPetsByStatus(status); - //Assert.IsType>(response); - } + PetApi petApi = new PetApi(); + List tagsList = new List(new String[] { "available" }); + List listPet = petApi.FindPetsByTags(tagsList); + foreach (Pet pet in listPet) // Loop through List with foreach. + { + Assert.IsType(pet); + Assert.Equal("available", pet.Tags[0].Name); + } + } + /// /// Test FindPetsByTags /// [Fact] public void FindPetsByTagsTest() { - // TODO uncomment below to test the method and replace null with proper value - //List tags = null; - //var response = instance.FindPetsByTags(tags); - //Assert.IsType>(response); + List tags = new List(new String[] { "pet" }); + var response = instance.FindPetsByTags(tags); + Assert.IsType>(response); } - + /// /// Test GetPetById /// [Fact] public void GetPetByIdTest() { - // TODO uncomment below to test the method and replace null with proper value - //long petId = null; - //var response = instance.GetPetById(petId); - //Assert.IsType(response); + // set timeout to 10 seconds + Configuration c1 = new Configuration(); + c1.Timeout = 10000; + c1.UserAgent = "TEST_USER_AGENT"; + + PetApi petApi = new PetApi(c1); + Pet response = petApi.GetPetById(petId); + Assert.IsType(response); + + Assert.Equal("Csharp test", response.Name); + Assert.Equal(Pet.StatusEnum.Available, response.Status); + + Assert.IsType>(response.Tags); + Assert.Equal(petId, response.Tags[0].Id); + Assert.Equal("csharp sample tag name1", response.Tags[0].Name); + + Assert.IsType>(response.PhotoUrls); + Assert.Equal("sample photoUrls", response.PhotoUrls[0]); + + Assert.IsType(response.Category); + Assert.Equal(56, response.Category.Id); + Assert.Equal("sample category name2", response.Category.Name); } + /// + /// Test GetPetByIdAsync + /// + [Fact] + public void TestGetPetByIdAsync() + { + PetApi petApi = new PetApi(); + var task = petApi.GetPetByIdAsync(petId); + Pet response = task.Result; + Assert.IsType(response); + + Assert.Equal("Csharp test", response.Name); + Assert.Equal(Pet.StatusEnum.Available, response.Status); + + Assert.IsType>(response.Tags); + Assert.Equal(petId, response.Tags[0].Id); + Assert.Equal("csharp sample tag name1", response.Tags[0].Name); + + Assert.IsType>(response.PhotoUrls); + Assert.Equal("sample photoUrls", response.PhotoUrls[0]); + + Assert.IsType(response.Category); + Assert.Equal(56, response.Category.Id); + Assert.Equal("sample category name2", response.Category.Name); + } + + /* a simple test for binary response. no longer in use. + [Fact] + public void TestGetByIdBinaryResponse() + { + PetApi petApi = new PetApi(c1); + Stream response = petApi.GetPetByIdBinaryResponse(petId); + Assert.IsType(response); + StreamReader reader = new StreamReader(response); + // the following will fail for sure + //Assert.Equal("someting", reader.ReadToEnd()); + } + */ + + /// + /// Test GetPetByIdWithHttpInfoAsync + /// + [Fact] + public void TestGetPetByIdWithHttpInfoAsync() + { + PetApi petApi = new PetApi(); + var task = petApi.GetPetByIdWithHttpInfoAsync(petId); + + Assert.Equal(200, (int)task.Result.StatusCode); + Assert.True(task.Result.Headers.ContainsKey("Content-Type")); + Assert.Equal("application/json", task.Result.Headers["Content-Type"][0]); + + Pet response = task.Result.Data; + Assert.IsType(response); + + Assert.Equal("Csharp test", response.Name); + Assert.Equal(Pet.StatusEnum.Available, response.Status); + + Assert.IsType>(response.Tags); + Assert.Equal(petId, response.Tags[0].Id); + Assert.Equal("csharp sample tag name1", response.Tags[0].Name); + + Assert.IsType>(response.PhotoUrls); + Assert.Equal("sample photoUrls", response.PhotoUrls[0]); + + Assert.IsType(response.Category); + Assert.Equal(56, response.Category.Id); + Assert.Equal("sample category name2", response.Category.Name); + } + /// /// Test UpdatePet /// @@ -119,37 +249,55 @@ namespace Org.OpenAPITools.Test.Api public void UpdatePetTest() { // TODO uncomment below to test the method and replace null with proper value - //Pet pet = null; - //instance.UpdatePet(pet); + // create pet + Pet p = createPet(); + instance.UpdatePet(p); + } - + /// /// Test UpdatePetWithForm /// [Fact] public void UpdatePetWithFormTest() { - // TODO uncomment below to test the method and replace null with proper value - //long petId = null; - //string name = null; - //string status = null; - //instance.UpdatePetWithForm(petId, name, status); - } + PetApi petApi = new PetApi(); + petApi.UpdatePetWithForm(petId, "new form name", "pending"); + Pet response = petApi.GetPetById(petId); + Assert.IsType(response); + Assert.IsType(response.Category); + Assert.IsType>(response.Tags); + + Assert.Equal("new form name", response.Name); + Assert.Equal(Pet.StatusEnum.Pending, response.Status); + + Assert.Equal(petId, response.Tags[0].Id); + Assert.Equal(56, response.Category.Id); + + // test optional parameter + petApi.UpdatePetWithForm(petId, "new form name2"); + Pet response2 = petApi.GetPetById(petId); + Assert.Equal("new form name2", response2.Name); + } + /// /// Test UploadFile /// - [Fact] + [Fact (Skip = "file upload not working for httpclient yet")] public void UploadFileTest() { - // TODO uncomment below to test the method and replace null with proper value - //long petId = null; - //string additionalMetadata = null; - //System.IO.Stream file = null; - //var response = instance.UploadFile(petId, additionalMetadata, file); - //Assert.IsType(response); - } + Assembly _assembly = Assembly.GetExecutingAssembly(); + Stream _imageStream = _assembly.GetManifestResourceStream("Org.OpenAPITools.Test.linux-logo.png"); + PetApi petApi = new PetApi(); + // test file upload with form parameters + petApi.UploadFile(petId, "new form name", _imageStream); + // test file upload without any form parameters + // using optional parameter syntax introduced at .net 4.0 + petApi.UploadFile(petId: petId, file: _imageStream); + } + /// /// Test UploadFileWithRequiredFile /// @@ -157,11 +305,47 @@ namespace Org.OpenAPITools.Test.Api public void UploadFileWithRequiredFileTest() { // TODO uncomment below to test the method and replace null with proper value - //long petId = null; + //long? petId = null; //System.IO.Stream requiredFile = null; //string additionalMetadata = null; //var response = instance.UploadFileWithRequiredFile(petId, requiredFile, additionalMetadata); - //Assert.IsType(response); + //Assert.IsType (response, "response is ApiResponse"); + } + + /// + /// Test status code + /// + [Fact] + public void TestStatusCodeAndHeader() + { + PetApi petApi = new PetApi(); + var response = petApi.GetPetByIdWithHttpInfo(petId); + //Assert.Equal("OK", response.StatusCode); + Assert.Equal(200, (int)response.StatusCode); + Assert.True(response.Headers.ContainsKey("Content-Type")); + Assert.Equal("application/json", response.Headers["Content-Type"][0]); + } + + /// + /// Test default header (should be deprecated) + /// + [Fact] + public void TestDefaultHeader() + { + //PetApi petApi = new PetApi(); + // commented out the warning test below as it's confirmed the warning is working as expected + // there should be a warning for using AddDefaultHeader (deprecated) below + //petApi.AddDefaultHeader ("header_key", "header_value"); + // the following should be used instead as suggested in the doc + //petApi.Configuration.AddDefaultHeader("header_key2", "header_value2"); + } + + public void Dispose() + { + // remove the pet after testing + PetApi petApi = new PetApi(); + petApi.DeletePet(petId, "test key"); } } + } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools.Test/Org.OpenAPITools.Test.csproj b/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools.Test/Org.OpenAPITools.Test.csproj index 1e13bb40fb5..dcc1208d75c 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools.Test/Org.OpenAPITools.Test.csproj +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools.Test/Org.OpenAPITools.Test.csproj @@ -17,4 +17,10 @@ + + + + + + diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools.Test/linux-logo.png b/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools.Test/linux-logo.png new file mode 100644 index 00000000000..8269538b1ac Binary files /dev/null and b/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools.Test/linux-logo.png differ diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/ApiClient.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/ApiClient.cs index 5e218effa1e..449301a307a 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/ApiClient.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/ApiClient.cs @@ -327,11 +327,21 @@ namespace Org.OpenAPITools.Client Cookies = new List() }; + // process response headers, e.g. Access-Control-Allow-Methods if (response.Headers != null) { foreach (var responseHeader in response.Headers) { + transformed.Headers.Add(responseHeader.Key, ClientUtils.ParameterToString(responseHeader.Value)); + } + } + // process response content headers, e.g. Content-Type + if (response.Content.Headers != null) + { + foreach (var responseHeader in response.Content.Headers) + { + transformed.Headers.Add(responseHeader.Key, ClientUtils.ParameterToString(responseHeader.Value)); } }