From 95622832a5c2b1acb2cf727d917701572eaa7a95 Mon Sep 17 00:00:00 2001 From: Kirthi Krishnamraju Date: Mon, 9 May 2016 11:47:13 -0700 Subject: [PATCH] Fix for #967: C# generator generates code that contains compiler warnings --- .../AzureBodyDuration/DurationOperations.cs | 9 +- .../ParameterGroupingOperations.cs | 9 +- .../AcceptanceTests/BodyBoolean/BoolModel.cs | 18 ++-- .../Expected/AcceptanceTests/BodyDate/Date.cs | 18 ++-- .../AcceptanceTests/BodyDateTime/Datetime.cs | 54 ++++------- .../BodyDateTimeRfc1123/Datetimerfc1123.cs | 18 ++-- .../AcceptanceTests/BodyDuration/Duration.cs | 9 +- .../AcceptanceTests/BodyInteger/IntModel.cs | 45 ++++------ .../AcceptanceTests/BodyNumber/Number.cs | 90 +++++++------------ .../AcceptanceTests/BodyString/EnumModel.cs | 9 +- .../CompositeBoolIntClient/BoolModel.cs | 18 ++-- .../CompositeBoolIntClient/IntModel.cs | 45 ++++------ .../RequiredOptional/ExplicitModel.cs | 9 +- .../CSharp/Templates/MethodTemplate.cshtml | 15 +++- 14 files changed, 129 insertions(+), 237 deletions(-) diff --git a/AutoRest/Generators/CSharp/Azure.CSharp.Tests/Expected/AcceptanceTests/AzureBodyDuration/DurationOperations.cs b/AutoRest/Generators/CSharp/Azure.CSharp.Tests/Expected/AcceptanceTests/AzureBodyDuration/DurationOperations.cs index 1bb297f05c..0a81d725f4 100644 --- a/AutoRest/Generators/CSharp/Azure.CSharp.Tests/Expected/AcceptanceTests/AzureBodyDuration/DurationOperations.cs +++ b/AutoRest/Generators/CSharp/Azure.CSharp.Tests/Expected/AcceptanceTests/AzureBodyDuration/DurationOperations.cs @@ -262,12 +262,9 @@ internal DurationOperations(AutoRestDurationTestService client) // Serialize Request string _requestContent = null; - if(durationBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(durationBody, this.Client.SerializationSettings); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(durationBody, this.Client.SerializationSettings); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Set Credentials if (this.Client.Credentials != null) { diff --git a/AutoRest/Generators/CSharp/Azure.CSharp.Tests/Expected/AcceptanceTests/AzureParameterGrouping/ParameterGroupingOperations.cs b/AutoRest/Generators/CSharp/Azure.CSharp.Tests/Expected/AcceptanceTests/AzureParameterGrouping/ParameterGroupingOperations.cs index 7fe382cfcd..9a2bf74564 100644 --- a/AutoRest/Generators/CSharp/Azure.CSharp.Tests/Expected/AcceptanceTests/AzureParameterGrouping/ParameterGroupingOperations.cs +++ b/AutoRest/Generators/CSharp/Azure.CSharp.Tests/Expected/AcceptanceTests/AzureParameterGrouping/ParameterGroupingOperations.cs @@ -161,12 +161,9 @@ internal ParameterGroupingOperations(AutoRestParameterGroupingTestService client // Serialize Request string _requestContent = null; - if(body != null) - { - _requestContent = SafeJsonConvert.SerializeObject(body, this.Client.SerializationSettings); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(body, this.Client.SerializationSettings); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Set Credentials if (this.Client.Credentials != null) { diff --git a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/BodyBoolean/BoolModel.cs b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/BodyBoolean/BoolModel.cs index d78403311e..d287e6188c 100644 --- a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/BodyBoolean/BoolModel.cs +++ b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/BodyBoolean/BoolModel.cs @@ -217,12 +217,9 @@ public BoolModel(AutoRestBoolTestService client) // Serialize Request string _requestContent = null; - if(boolBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(boolBody, this.Client.SerializationSettings); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(boolBody, this.Client.SerializationSettings); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { @@ -446,12 +443,9 @@ public BoolModel(AutoRestBoolTestService client) // Serialize Request string _requestContent = null; - if(boolBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(boolBody, this.Client.SerializationSettings); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(boolBody, this.Client.SerializationSettings); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { diff --git a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/BodyDate/Date.cs b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/BodyDate/Date.cs index 0a4c071707..509781fe38 100644 --- a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/BodyDate/Date.cs +++ b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/BodyDate/Date.cs @@ -574,12 +574,9 @@ public Date(AutoRestDateTestService client) // Serialize Request string _requestContent = null; - if(dateBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(dateBody, new DateJsonConverter()); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(dateBody, new DateJsonConverter()); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { @@ -803,12 +800,9 @@ public Date(AutoRestDateTestService client) // Serialize Request string _requestContent = null; - if(dateBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(dateBody, new DateJsonConverter()); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(dateBody, new DateJsonConverter()); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { diff --git a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/BodyDateTime/Datetime.cs b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/BodyDateTime/Datetime.cs index 2248d4263b..a46a03a8ae 100644 --- a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/BodyDateTime/Datetime.cs +++ b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/BodyDateTime/Datetime.cs @@ -574,12 +574,9 @@ public Datetime(AutoRestDateTimeTestService client) // Serialize Request string _requestContent = null; - if(datetimeBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(datetimeBody, this.Client.SerializationSettings); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(datetimeBody, this.Client.SerializationSettings); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { @@ -923,12 +920,9 @@ public Datetime(AutoRestDateTimeTestService client) // Serialize Request string _requestContent = null; - if(datetimeBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(datetimeBody, this.Client.SerializationSettings); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(datetimeBody, this.Client.SerializationSettings); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { @@ -1274,12 +1268,9 @@ public Datetime(AutoRestDateTimeTestService client) // Serialize Request string _requestContent = null; - if(datetimeBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(datetimeBody, this.Client.SerializationSettings); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(datetimeBody, this.Client.SerializationSettings); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { @@ -1624,12 +1615,9 @@ public Datetime(AutoRestDateTimeTestService client) // Serialize Request string _requestContent = null; - if(datetimeBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(datetimeBody, this.Client.SerializationSettings); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(datetimeBody, this.Client.SerializationSettings); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { @@ -1853,12 +1841,9 @@ public Datetime(AutoRestDateTimeTestService client) // Serialize Request string _requestContent = null; - if(datetimeBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(datetimeBody, this.Client.SerializationSettings); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(datetimeBody, this.Client.SerializationSettings); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { @@ -2082,12 +2067,9 @@ public Datetime(AutoRestDateTimeTestService client) // Serialize Request string _requestContent = null; - if(datetimeBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(datetimeBody, this.Client.SerializationSettings); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(datetimeBody, this.Client.SerializationSettings); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { diff --git a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/BodyDateTimeRfc1123/Datetimerfc1123.cs b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/BodyDateTimeRfc1123/Datetimerfc1123.cs index 068aced56d..2506c533d5 100644 --- a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/BodyDateTimeRfc1123/Datetimerfc1123.cs +++ b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/BodyDateTimeRfc1123/Datetimerfc1123.cs @@ -574,12 +574,9 @@ public Datetimerfc1123(AutoRestRFC1123DateTimeTestService client) // Serialize Request string _requestContent = null; - if(datetimeBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(datetimeBody, new DateTimeRfc1123JsonConverter()); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(datetimeBody, new DateTimeRfc1123JsonConverter()); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { @@ -922,12 +919,9 @@ public Datetimerfc1123(AutoRestRFC1123DateTimeTestService client) // Serialize Request string _requestContent = null; - if(datetimeBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(datetimeBody, new DateTimeRfc1123JsonConverter()); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(datetimeBody, new DateTimeRfc1123JsonConverter()); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { diff --git a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/BodyDuration/Duration.cs b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/BodyDuration/Duration.cs index 42b50edf64..7893ed1ee8 100644 --- a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/BodyDuration/Duration.cs +++ b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/BodyDuration/Duration.cs @@ -217,12 +217,9 @@ public Duration(AutoRestDurationTestService client) // Serialize Request string _requestContent = null; - if(durationBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(durationBody, this.Client.SerializationSettings); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(durationBody, this.Client.SerializationSettings); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { diff --git a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/BodyInteger/IntModel.cs b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/BodyInteger/IntModel.cs index 9c9b57ebbb..03b6a56c68 100644 --- a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/BodyInteger/IntModel.cs +++ b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/BodyInteger/IntModel.cs @@ -812,12 +812,9 @@ public IntModel(AutoRestIntegerTestService client) // Serialize Request string _requestContent = null; - if(intBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(intBody, this.Client.SerializationSettings); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(intBody, this.Client.SerializationSettings); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { @@ -922,12 +919,9 @@ public IntModel(AutoRestIntegerTestService client) // Serialize Request string _requestContent = null; - if(intBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(intBody, this.Client.SerializationSettings); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(intBody, this.Client.SerializationSettings); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { @@ -1032,12 +1026,9 @@ public IntModel(AutoRestIntegerTestService client) // Serialize Request string _requestContent = null; - if(intBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(intBody, this.Client.SerializationSettings); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(intBody, this.Client.SerializationSettings); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { @@ -1142,12 +1133,9 @@ public IntModel(AutoRestIntegerTestService client) // Serialize Request string _requestContent = null; - if(intBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(intBody, this.Client.SerializationSettings); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(intBody, this.Client.SerializationSettings); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { @@ -1371,12 +1359,9 @@ public IntModel(AutoRestIntegerTestService client) // Serialize Request string _requestContent = null; - if(intBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(intBody, new UnixTimeJsonConverter()); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(intBody, new UnixTimeJsonConverter()); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { diff --git a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/BodyNumber/Number.cs b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/BodyNumber/Number.cs index 4b60b55820..dc98d33d86 100644 --- a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/BodyNumber/Number.cs +++ b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/BodyNumber/Number.cs @@ -574,12 +574,9 @@ public Number(AutoRestNumberTestService client) // Serialize Request string _requestContent = null; - if(numberBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(numberBody, this.Client.SerializationSettings); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(numberBody, this.Client.SerializationSettings); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { @@ -803,12 +800,9 @@ public Number(AutoRestNumberTestService client) // Serialize Request string _requestContent = null; - if(numberBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(numberBody, this.Client.SerializationSettings); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(numberBody, this.Client.SerializationSettings); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { @@ -1032,12 +1026,9 @@ public Number(AutoRestNumberTestService client) // Serialize Request string _requestContent = null; - if(numberBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(numberBody, this.Client.SerializationSettings); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(numberBody, this.Client.SerializationSettings); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { @@ -1261,12 +1252,9 @@ public Number(AutoRestNumberTestService client) // Serialize Request string _requestContent = null; - if(numberBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(numberBody, this.Client.SerializationSettings); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(numberBody, this.Client.SerializationSettings); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { @@ -1490,12 +1478,9 @@ public Number(AutoRestNumberTestService client) // Serialize Request string _requestContent = null; - if(numberBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(numberBody, this.Client.SerializationSettings); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(numberBody, this.Client.SerializationSettings); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { @@ -1719,12 +1704,9 @@ public Number(AutoRestNumberTestService client) // Serialize Request string _requestContent = null; - if(numberBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(numberBody, this.Client.SerializationSettings); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(numberBody, this.Client.SerializationSettings); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { @@ -1948,12 +1930,9 @@ public Number(AutoRestNumberTestService client) // Serialize Request string _requestContent = null; - if(numberBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(numberBody, this.Client.SerializationSettings); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(numberBody, this.Client.SerializationSettings); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { @@ -2177,12 +2156,9 @@ public Number(AutoRestNumberTestService client) // Serialize Request string _requestContent = null; - if(numberBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(numberBody, this.Client.SerializationSettings); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(numberBody, this.Client.SerializationSettings); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { @@ -2406,12 +2382,9 @@ public Number(AutoRestNumberTestService client) // Serialize Request string _requestContent = null; - if(numberBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(numberBody, this.Client.SerializationSettings); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(numberBody, this.Client.SerializationSettings); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { @@ -2635,12 +2608,9 @@ public Number(AutoRestNumberTestService client) // Serialize Request string _requestContent = null; - if(numberBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(numberBody, this.Client.SerializationSettings); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(numberBody, this.Client.SerializationSettings); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { diff --git a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/BodyString/EnumModel.cs b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/BodyString/EnumModel.cs index 8861a350c0..c1701bfb31 100644 --- a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/BodyString/EnumModel.cs +++ b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/BodyString/EnumModel.cs @@ -220,12 +220,9 @@ public EnumModel(AutoRestSwaggerBATService client) // Serialize Request string _requestContent = null; - if(stringBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(stringBody, this.Client.SerializationSettings); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(stringBody, this.Client.SerializationSettings); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { diff --git a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/CompositeBoolIntClient/BoolModel.cs b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/CompositeBoolIntClient/BoolModel.cs index 22ff99e128..9c761510fc 100644 --- a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/CompositeBoolIntClient/BoolModel.cs +++ b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/CompositeBoolIntClient/BoolModel.cs @@ -217,12 +217,9 @@ public BoolModel(CompositeBoolInt client) // Serialize Request string _requestContent = null; - if(boolBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(boolBody, this.Client.SerializationSettings); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(boolBody, this.Client.SerializationSettings); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { @@ -446,12 +443,9 @@ public BoolModel(CompositeBoolInt client) // Serialize Request string _requestContent = null; - if(boolBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(boolBody, this.Client.SerializationSettings); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(boolBody, this.Client.SerializationSettings); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { diff --git a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/CompositeBoolIntClient/IntModel.cs b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/CompositeBoolIntClient/IntModel.cs index 2047fd8608..c730d55f15 100644 --- a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/CompositeBoolIntClient/IntModel.cs +++ b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/CompositeBoolIntClient/IntModel.cs @@ -812,12 +812,9 @@ public IntModel(CompositeBoolInt client) // Serialize Request string _requestContent = null; - if(intBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(intBody, this.Client.SerializationSettings); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(intBody, this.Client.SerializationSettings); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { @@ -922,12 +919,9 @@ public IntModel(CompositeBoolInt client) // Serialize Request string _requestContent = null; - if(intBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(intBody, this.Client.SerializationSettings); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(intBody, this.Client.SerializationSettings); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { @@ -1032,12 +1026,9 @@ public IntModel(CompositeBoolInt client) // Serialize Request string _requestContent = null; - if(intBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(intBody, this.Client.SerializationSettings); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(intBody, this.Client.SerializationSettings); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { @@ -1142,12 +1133,9 @@ public IntModel(CompositeBoolInt client) // Serialize Request string _requestContent = null; - if(intBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(intBody, this.Client.SerializationSettings); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(intBody, this.Client.SerializationSettings); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { @@ -1371,12 +1359,9 @@ public IntModel(CompositeBoolInt client) // Serialize Request string _requestContent = null; - if(intBody != null) - { - _requestContent = SafeJsonConvert.SerializeObject(intBody, new UnixTimeJsonConverter()); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(intBody, new UnixTimeJsonConverter()); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { diff --git a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/RequiredOptional/ExplicitModel.cs b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/RequiredOptional/ExplicitModel.cs index f0e42c1bf7..117742e60f 100644 --- a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/RequiredOptional/ExplicitModel.cs +++ b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/RequiredOptional/ExplicitModel.cs @@ -99,12 +99,9 @@ public ExplicitModel(AutoRestRequiredOptionalTestService client) // Serialize Request string _requestContent = null; - if(bodyParameter != null) - { - _requestContent = SafeJsonConvert.SerializeObject(bodyParameter, this.Client.SerializationSettings); - _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); - } + _requestContent = SafeJsonConvert.SerializeObject(bodyParameter, this.Client.SerializationSettings); + _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request if (_shouldTrace) { diff --git a/AutoRest/Generators/CSharp/CSharp/Templates/MethodTemplate.cshtml b/AutoRest/Generators/CSharp/CSharp/Templates/MethodTemplate.cshtml index df038116b2..96077aaee3 100644 --- a/AutoRest/Generators/CSharp/CSharp/Templates/MethodTemplate.cshtml +++ b/AutoRest/Generators/CSharp/CSharp/Templates/MethodTemplate.cshtml @@ -161,12 +161,21 @@ else } else { + if (!Model.RequestBody.CanBeNull()) + { + @:_requestContent = SafeJsonConvert.SerializeObject(@(Model.RequestBody.Name), @(Model.GetSerializationSettingsReference(Model.RequestBody.Type))); + @:_httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + @:_httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("@(Model.RequestContentType)"); + } + else + { @:if(@(Model.RequestBody.Name) != null) @:{ - @:_requestContent = SafeJsonConvert.SerializeObject(@(Model.RequestBody.Name), @(Model.GetSerializationSettingsReference(Model.RequestBody.Type))); - @:_httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); - @:_httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("@(Model.RequestContentType)"); + @: _requestContent = SafeJsonConvert.SerializeObject(@(Model.RequestBody.Name), @(Model.GetSerializationSettingsReference(Model.RequestBody.Type))); + @: _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); + @: _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("@(Model.RequestContentType)"); @:} + } } } else if (Model.LogicalParameters.Where(p => p.Location == ParameterLocation.FormData).Any())