diff --git a/AutoRest/Generators/CSharp/Azure.CSharp.Tests/Expected/AcceptanceTests/AzureSpecials/XMsClientRequestIdOperations.cs b/AutoRest/Generators/CSharp/Azure.CSharp.Tests/Expected/AcceptanceTests/AzureSpecials/XMsClientRequestIdOperations.cs index ed11a032ba673..e9da082942643 100644 --- a/AutoRest/Generators/CSharp/Azure.CSharp.Tests/Expected/AcceptanceTests/AzureSpecials/XMsClientRequestIdOperations.cs +++ b/AutoRest/Generators/CSharp/Azure.CSharp.Tests/Expected/AcceptanceTests/AzureSpecials/XMsClientRequestIdOperations.cs @@ -143,7 +143,12 @@ public async Task GetWithHttpMessagesAsync(Dictionary Head200WithHttpMessagesAsync(Dictionar if ((int)_statusCode != 200) { var ex = new CloudException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_httpResponse.Headers.Contains("x-ms-request-id")) @@ -264,7 +269,12 @@ public async Task Head204WithHttpMessagesAsync(Dictionar if ((int)_statusCode != 204) { var ex = new CloudException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_httpResponse.Headers.Contains("x-ms-request-id")) @@ -387,7 +397,12 @@ public async Task Head404WithHttpMessagesAsync(Dictionar if ((int)_statusCode != 204) { var ex = new CloudException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_httpResponse.Headers.Contains("x-ms-request-id")) diff --git a/AutoRest/Generators/CSharp/CSharp.Tests/AcceptanceTests.cs b/AutoRest/Generators/CSharp/CSharp.Tests/AcceptanceTests.cs index 06eb37287fa8d..3d85cb86b2cda 100644 --- a/AutoRest/Generators/CSharp/CSharp.Tests/AcceptanceTests.cs +++ b/AutoRest/Generators/CSharp/CSharp.Tests/AcceptanceTests.cs @@ -1807,6 +1807,9 @@ private static void TestSuccessStatusCodes(AutoRestHttpInfrastructureTestService var ex2 = Assert.Throws(() => client.HttpFailure.GetNoModelError()); Assert.Equal("{\"message\":\"NoErrorModel\",\"status\":400}", ex2.Response.Content); + var ex3 = Assert.Throws(() => client.HttpFailure.GetNoModelEmpty()); + Assert.Equal(string.Empty, ex3.Response.Content); + client.HttpSuccess.Head200(); Assert.True(client.HttpSuccess.Get200()); client.HttpSuccess.Put200(true); diff --git a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/Http/HttpFailure.cs b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/Http/HttpFailure.cs index 7057db1bdc4b9..80d5c3eb65b40 100644 --- a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/Http/HttpFailure.cs +++ b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/Http/HttpFailure.cs @@ -246,7 +246,130 @@ public HttpFailure(AutoRestHttpInfrastructureTestService client) if ((int)_statusCode != 200) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } + ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); + ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); + if (_shouldTrace) + { + ServiceClientTracing.Error(_invocationId, ex); + } + _httpRequest.Dispose(); + if (_httpResponse != null) + { + _httpResponse.Dispose(); + } + throw ex; + } + // Create Result + var _result = new HttpOperationResponse(); + _result.Request = _httpRequest; + _result.Response = _httpResponse; + // Deserialize Response + if ((int)_statusCode == 200) + { _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + try + { + _result.Body = SafeJsonConvert.DeserializeObject(_responseContent, this.Client.DeserializationSettings); + } + catch (JsonException ex) + { + _httpRequest.Dispose(); + if (_httpResponse != null) + { + _httpResponse.Dispose(); + } + throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); + } + } + if (_shouldTrace) + { + ServiceClientTracing.Exit(_invocationId, _result); + } + return _result; + } + + /// + /// Get empty response from server + /// + /// + /// Headers that will be added to request. + /// + /// + /// The cancellation token. + /// + /// + /// Thrown when the operation returned an invalid status code + /// + /// + /// Thrown when unable to deserialize the response + /// + /// + /// A response object containing the response body and response headers. + /// + public async Task> GetNoModelEmptyWithHttpMessagesAsync(Dictionary> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken)) + { + // Tracing + bool _shouldTrace = ServiceClientTracing.IsEnabled; + string _invocationId = null; + if (_shouldTrace) + { + _invocationId = ServiceClientTracing.NextInvocationId.ToString(); + Dictionary tracingParameters = new Dictionary(); + tracingParameters.Add("cancellationToken", cancellationToken); + ServiceClientTracing.Enter(_invocationId, this, "GetNoModelEmpty", tracingParameters); + } + // Construct URL + var _baseUrl = this.Client.BaseUri.AbsoluteUri; + var _url = new Uri(new Uri(_baseUrl + (_baseUrl.EndsWith("/") ? "" : "/")), "http/failure/nomodel/empty").ToString(); + // Create HTTP transport objects + HttpRequestMessage _httpRequest = new HttpRequestMessage(); + HttpResponseMessage _httpResponse = null; + _httpRequest.Method = new HttpMethod("GET"); + _httpRequest.RequestUri = new Uri(_url); + // Set Headers + if (customHeaders != null) + { + foreach(var _header in customHeaders) + { + if (_httpRequest.Headers.Contains(_header.Key)) + { + _httpRequest.Headers.Remove(_header.Key); + } + _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); + } + } + + // Serialize Request + string _requestContent = null; + // Send Request + if (_shouldTrace) + { + ServiceClientTracing.SendRequest(_invocationId, _httpRequest); + } + cancellationToken.ThrowIfCancellationRequested(); + _httpResponse = await this.Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); + if (_shouldTrace) + { + ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); + } + HttpStatusCode _statusCode = _httpResponse.StatusCode; + cancellationToken.ThrowIfCancellationRequested(); + string _responseContent = null; + if ((int)_statusCode != 200) + { + var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) diff --git a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/Http/HttpFailureExtensions.cs b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/Http/HttpFailureExtensions.cs index 0ebcf0e1377c5..3dcf590ba4a37 100644 --- a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/Http/HttpFailureExtensions.cs +++ b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/Http/HttpFailureExtensions.cs @@ -77,5 +77,33 @@ public static partial class HttpFailureExtensions } } + /// + /// Get empty response from server + /// + /// + /// The operations group for this extension method. + /// + public static bool? GetNoModelEmpty(this IHttpFailure operations) + { + return Task.Factory.StartNew(s => ((IHttpFailure)s).GetNoModelEmptyAsync(), operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult(); + } + + /// + /// Get empty response from server + /// + /// + /// The operations group for this extension method. + /// + /// + /// The cancellation token. + /// + public static async Task GetNoModelEmptyAsync(this IHttpFailure operations, CancellationToken cancellationToken = default(CancellationToken)) + { + using (var _result = await operations.GetNoModelEmptyWithHttpMessagesAsync(null, cancellationToken).ConfigureAwait(false)) + { + return _result.Body; + } + } + } } diff --git a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/Http/IHttpFailure.cs b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/Http/IHttpFailure.cs index afdaf191a94fd..36a5c4c362dd2 100644 --- a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/Http/IHttpFailure.cs +++ b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/Http/IHttpFailure.cs @@ -53,5 +53,21 @@ public partial interface IHttpFailure /// Thrown when unable to deserialize the response /// Task> GetNoModelErrorWithHttpMessagesAsync(Dictionary> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken)); + /// + /// Get empty response from server + /// + /// + /// The headers that will be added to request. + /// + /// + /// The cancellation token. + /// + /// + /// Thrown when the operation returned an invalid status code + /// + /// + /// Thrown when unable to deserialize the response + /// + Task> GetNoModelEmptyWithHttpMessagesAsync(Dictionary> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken)); } } diff --git a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/Http/MultipleResponses.cs b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/Http/MultipleResponses.cs index 6101aab0f58fe..ffc0540b4fe50 100644 --- a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/Http/MultipleResponses.cs +++ b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/Http/MultipleResponses.cs @@ -2133,7 +2133,12 @@ public async Task Get202None204NoneDefaultNone202InvalidW if ((int)_statusCode != 202 && (int)_statusCode != 204) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -2225,7 +2230,12 @@ public async Task Get202None204NoneDefaultNone204NoneWith if ((int)_statusCode != 202 && (int)_statusCode != 204) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -2317,7 +2327,12 @@ public async Task Get202None204NoneDefaultNone400NoneWith if ((int)_statusCode != 202 && (int)_statusCode != 204) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -2409,7 +2424,12 @@ public async Task Get202None204NoneDefaultNone400InvalidW if ((int)_statusCode != 202 && (int)_statusCode != 204) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -2973,7 +2993,12 @@ public async Task GetDefaultNone200InvalidWithHttpMessage if (!_httpResponse.IsSuccessStatusCode) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -3065,7 +3090,12 @@ public async Task GetDefaultNone200NoneWithHttpMessagesAs if (!_httpResponse.IsSuccessStatusCode) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -3157,7 +3187,12 @@ public async Task GetDefaultNone400InvalidWithHttpMessage if (!_httpResponse.IsSuccessStatusCode) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -3249,7 +3284,12 @@ public async Task GetDefaultNone400NoneWithHttpMessagesAs if (!_httpResponse.IsSuccessStatusCode) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -3345,7 +3385,12 @@ public async Task> Get200ModelA200NoneWithHttpMessagesA if ((int)_statusCode != 200) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -3458,7 +3503,12 @@ public async Task> Get200ModelA200ValidWithHttpMessages if ((int)_statusCode != 200) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -3571,7 +3621,12 @@ public async Task> Get200ModelA200InvalidWithHttpMessag if ((int)_statusCode != 200) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -3685,7 +3740,12 @@ public async Task> Get200ModelA400NoneWithHttpMessagesA if ((int)_statusCode != 200) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -3798,7 +3858,12 @@ public async Task> Get200ModelA400ValidWithHttpMessages if ((int)_statusCode != 200) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -3911,7 +3976,12 @@ public async Task> Get200ModelA400InvalidWithHttpMessag if ((int)_statusCode != 200) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -4024,7 +4094,12 @@ public async Task> Get200ModelA202ValidWithHttpMessages if ((int)_statusCode != 200) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) diff --git a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/ParameterFlattening/AvailabilitySets.cs b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/ParameterFlattening/AvailabilitySets.cs index 23dda14e73971..578ae0a4c868b 100644 --- a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/ParameterFlattening/AvailabilitySets.cs +++ b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/ParameterFlattening/AvailabilitySets.cs @@ -164,7 +164,12 @@ public async Task UpdateWithHttpMessagesAsync(string reso if ((int)_statusCode != 200) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) diff --git a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/Validation/AutoRestValidationTest.cs b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/Validation/AutoRestValidationTest.cs index f8e5b3cf7e419..d6b3ced937417 100644 --- a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/Validation/AutoRestValidationTest.cs +++ b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/Validation/AutoRestValidationTest.cs @@ -630,7 +630,12 @@ public async Task GetWithConstantInPathWithHttpMessagesAs if ((int)_statusCode != 200) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -738,7 +743,12 @@ public async Task> PostWithConstantInBodyWithHttp if ((int)_statusCode != 200) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) diff --git a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/Additional.Properties/Get.cs b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/Additional.Properties/Get.cs index b267924ae2711..8b6877522b265 100644 --- a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/Additional.Properties/Get.cs +++ b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/Additional.Properties/Get.cs @@ -139,7 +139,12 @@ public async Task>> PetsWithHttpMessagesAsync(i if ((int)_statusCode != 200) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) diff --git a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/Additional.Properties/Post.cs b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/Additional.Properties/Post.cs index dced68c50c487..e3de70a95d2a1 100644 --- a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/Additional.Properties/Post.cs +++ b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/Additional.Properties/Post.cs @@ -132,7 +132,12 @@ public async Task PetsWithHttpMessagesAsync(Pet pet, Dict if ((int)_statusCode != 200) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) diff --git a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/Additional.Properties/Put.cs b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/Additional.Properties/Put.cs index 551085676fbb9..acc11b8e52f3b 100644 --- a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/Additional.Properties/Put.cs +++ b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/Additional.Properties/Put.cs @@ -132,7 +132,12 @@ public async Task PetsWithHttpMessagesAsync(Pet pet, Dict if ((int)_statusCode != 200) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) diff --git a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/Internal.Ctors/Pets.cs b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/Internal.Ctors/Pets.cs index 21a439ec8e36c..d4dbf5bd4000a 100644 --- a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/Internal.Ctors/Pets.cs +++ b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/Internal.Ctors/Pets.cs @@ -117,7 +117,12 @@ public async Task GetWithHttpMessagesAsync(Dictionary> AddPetWithHttpMessagesAsync(Pet bo if ((int)_statusCode != 405 && (int)_statusCode != 200) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -516,7 +521,12 @@ public async Task UpdatePetWithHttpMessagesAsync(Pet body if ((int)_statusCode != 400 && (int)_statusCode != 404 && (int)_statusCode != 405) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -640,7 +650,12 @@ public async Task>> FindPetsByStatusWithHttpMes if ((int)_statusCode != 200 && (int)_statusCode != 400) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -783,7 +798,12 @@ public async Task>> FindPetsByTagsWithHttpMessa if ((int)_statusCode != 200 && (int)_statusCode != 400) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -910,7 +930,12 @@ public async Task> GetPetByIdWithHttpMessagesAsync(lo if ((int)_statusCode != 200 && (int)_statusCode != 400 && (int)_statusCode != 404) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -1076,7 +1101,12 @@ public async Task UpdatePetWithFormWithHttpMessagesAsync( if ((int)_statusCode != 405) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -1190,7 +1220,12 @@ public async Task DeletePetWithHttpMessagesAsync(long pet if ((int)_statusCode != 400) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -1294,7 +1329,12 @@ public async Task DeletePetWithHttpMessagesAsync(long pet if ((int)_statusCode != 200) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -1430,7 +1470,12 @@ public async Task> PlaceOrderWithHttpMessagesAsync( if ((int)_statusCode != 200 && (int)_statusCode != 400) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -1576,7 +1621,12 @@ public async Task> GetOrderByIdWithHttpMessagesAsyn if ((int)_statusCode != 200 && (int)_statusCode != 400 && (int)_statusCode != 404) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -1715,7 +1765,12 @@ public async Task DeleteOrderWithHttpMessagesAsync(string if ((int)_statusCode != 400 && (int)_statusCode != 404) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -1833,7 +1888,12 @@ public async Task CreateUserWithHttpMessagesAsync(User bo if (!_httpResponse.IsSuccessStatusCode) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -1948,7 +2008,12 @@ public async Task CreateUsersWithArrayInputWithHttpMessag if (!_httpResponse.IsSuccessStatusCode) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -2063,7 +2128,12 @@ public async Task CreateUsersWithListInputWithHttpMessage if (!_httpResponse.IsSuccessStatusCode) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -2196,7 +2266,12 @@ public async Task CreateUsersWithListInputWithHttpMessage if ((int)_statusCode != 200 && (int)_statusCode != 400) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -2325,7 +2400,12 @@ public async Task LogoutUserWithHttpMessagesAsync(Diction if (!_httpResponse.IsSuccessStatusCode) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -2438,7 +2518,12 @@ public async Task> GetUserByNameWithHttpMessagesAsyn if ((int)_statusCode != 200 && (int)_statusCode != 400 && (int)_statusCode != 404) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -2583,7 +2668,12 @@ public async Task UpdateUserWithHttpMessagesAsync(string if ((int)_statusCode != 400 && (int)_statusCode != 404) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -2696,7 +2786,12 @@ public async Task DeleteUserWithHttpMessagesAsync(string if ((int)_statusCode != 400 && (int)_statusCode != 404) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) diff --git a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/PetstoreV2AllSync/SwaggerPetstoreV2.cs b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/PetstoreV2AllSync/SwaggerPetstoreV2.cs index b584445c4e219..2d18554f82c83 100644 --- a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/PetstoreV2AllSync/SwaggerPetstoreV2.cs +++ b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/PetstoreV2AllSync/SwaggerPetstoreV2.cs @@ -246,7 +246,12 @@ public async Task> AddPetWithHttpMessagesAsync(Pet bo if ((int)_statusCode != 405 && (int)_statusCode != 200) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -377,7 +382,12 @@ public async Task UpdatePetWithHttpMessagesAsync(Pet body if ((int)_statusCode != 400 && (int)_statusCode != 404 && (int)_statusCode != 405) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -495,7 +505,12 @@ public async Task>> FindPetsByStatusWithHttpMes if ((int)_statusCode != 200 && (int)_statusCode != 400) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -632,7 +647,12 @@ public async Task>> FindPetsByTagsWithHttpMessa if ((int)_statusCode != 200 && (int)_statusCode != 400) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -753,7 +773,12 @@ public async Task> GetPetByIdWithHttpMessagesAsync(lo if ((int)_statusCode != 200 && (int)_statusCode != 400 && (int)_statusCode != 404) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -913,7 +938,12 @@ public async Task UpdatePetWithFormWithHttpMessagesAsync( if ((int)_statusCode != 405) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -1021,7 +1051,12 @@ public async Task DeletePetWithHttpMessagesAsync(long pet if ((int)_statusCode != 400) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -1119,7 +1154,12 @@ public async Task DeletePetWithHttpMessagesAsync(long pet if ((int)_statusCode != 200) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -1249,7 +1289,12 @@ public async Task> PlaceOrderWithHttpMessagesAsync( if ((int)_statusCode != 200 && (int)_statusCode != 400) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -1389,7 +1434,12 @@ public async Task> GetOrderByIdWithHttpMessagesAsyn if ((int)_statusCode != 200 && (int)_statusCode != 400 && (int)_statusCode != 404) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -1522,7 +1572,12 @@ public async Task DeleteOrderWithHttpMessagesAsync(string if ((int)_statusCode != 400 && (int)_statusCode != 404) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -1634,7 +1689,12 @@ public async Task CreateUserWithHttpMessagesAsync(User bo if (!_httpResponse.IsSuccessStatusCode) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -1743,7 +1803,12 @@ public async Task CreateUsersWithArrayInputWithHttpMessag if (!_httpResponse.IsSuccessStatusCode) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -1852,7 +1917,12 @@ public async Task CreateUsersWithListInputWithHttpMessage if (!_httpResponse.IsSuccessStatusCode) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -1979,7 +2049,12 @@ public async Task CreateUsersWithListInputWithHttpMessage if ((int)_statusCode != 200 && (int)_statusCode != 400) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -2102,7 +2177,12 @@ public async Task LogoutUserWithHttpMessagesAsync(Diction if (!_httpResponse.IsSuccessStatusCode) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -2209,7 +2289,12 @@ public async Task> GetUserByNameWithHttpMessagesAsyn if ((int)_statusCode != 200 && (int)_statusCode != 400 && (int)_statusCode != 404) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -2348,7 +2433,12 @@ public async Task UpdateUserWithHttpMessagesAsync(string if ((int)_statusCode != 400 && (int)_statusCode != 404) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -2455,7 +2545,12 @@ public async Task DeleteUserWithHttpMessagesAsync(string if ((int)_statusCode != 400 && (int)_statusCode != 404) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) diff --git a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/PetstoreV2NoSync/SwaggerPetstoreV2.cs b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/PetstoreV2NoSync/SwaggerPetstoreV2.cs index 17052bd6753f5..d428a8efe174e 100644 --- a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/PetstoreV2NoSync/SwaggerPetstoreV2.cs +++ b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/PetstoreV2NoSync/SwaggerPetstoreV2.cs @@ -246,7 +246,12 @@ public async Task> AddPetWithHttpMessagesAsync(Pet bo if ((int)_statusCode != 405 && (int)_statusCode != 200) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -377,7 +382,12 @@ public async Task UpdatePetWithHttpMessagesAsync(Pet body if ((int)_statusCode != 400 && (int)_statusCode != 404 && (int)_statusCode != 405) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -495,7 +505,12 @@ public async Task>> FindPetsByStatusWithHttpMes if ((int)_statusCode != 200 && (int)_statusCode != 400) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -632,7 +647,12 @@ public async Task>> FindPetsByTagsWithHttpMessa if ((int)_statusCode != 200 && (int)_statusCode != 400) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -753,7 +773,12 @@ public async Task> GetPetByIdWithHttpMessagesAsync(lo if ((int)_statusCode != 200 && (int)_statusCode != 400 && (int)_statusCode != 404) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -913,7 +938,12 @@ public async Task UpdatePetWithFormWithHttpMessagesAsync( if ((int)_statusCode != 405) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -1021,7 +1051,12 @@ public async Task DeletePetWithHttpMessagesAsync(long pet if ((int)_statusCode != 400) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -1119,7 +1154,12 @@ public async Task DeletePetWithHttpMessagesAsync(long pet if ((int)_statusCode != 200) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -1249,7 +1289,12 @@ public async Task> PlaceOrderWithHttpMessagesAsync( if ((int)_statusCode != 200 && (int)_statusCode != 400) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -1389,7 +1434,12 @@ public async Task> GetOrderByIdWithHttpMessagesAsyn if ((int)_statusCode != 200 && (int)_statusCode != 400 && (int)_statusCode != 404) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -1522,7 +1572,12 @@ public async Task DeleteOrderWithHttpMessagesAsync(string if ((int)_statusCode != 400 && (int)_statusCode != 404) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -1634,7 +1689,12 @@ public async Task CreateUserWithHttpMessagesAsync(User bo if (!_httpResponse.IsSuccessStatusCode) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -1743,7 +1803,12 @@ public async Task CreateUsersWithArrayInputWithHttpMessag if (!_httpResponse.IsSuccessStatusCode) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -1852,7 +1917,12 @@ public async Task CreateUsersWithListInputWithHttpMessage if (!_httpResponse.IsSuccessStatusCode) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -1979,7 +2049,12 @@ public async Task CreateUsersWithListInputWithHttpMessage if ((int)_statusCode != 200 && (int)_statusCode != 400) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -2102,7 +2177,12 @@ public async Task LogoutUserWithHttpMessagesAsync(Diction if (!_httpResponse.IsSuccessStatusCode) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -2209,7 +2289,12 @@ public async Task> GetUserByNameWithHttpMessagesAsyn if ((int)_statusCode != 200 && (int)_statusCode != 400 && (int)_statusCode != 404) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -2348,7 +2433,12 @@ public async Task UpdateUserWithHttpMessagesAsync(string if ((int)_statusCode != 400 && (int)_statusCode != 404) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) @@ -2455,7 +2545,12 @@ public async Task DeleteUserWithHttpMessagesAsync(string if ((int)_statusCode != 400 && (int)_statusCode != 404) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); - _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (_httpResponse.Content != null) { + _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else { + _responseContent = string.Empty; + } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) diff --git a/AutoRest/Generators/CSharp/CSharp/Templates/MethodTemplate.cshtml b/AutoRest/Generators/CSharp/CSharp/Templates/MethodTemplate.cshtml index 7e272a09e4c3a..9147381b6a493 100644 --- a/AutoRest/Generators/CSharp/CSharp/Templates/MethodTemplate.cshtml +++ b/AutoRest/Generators/CSharp/CSharp/Templates/MethodTemplate.cshtml @@ -299,7 +299,12 @@ else else { //If not defined by default model, read content as string - @:_responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + @:if (_httpResponse.Content != null) { + @:_responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + @:} + @:else { + @:_responseContent = string.Empty; + @:} } ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); diff --git a/AutoRest/TestServer/server/routes/httpResponses.js b/AutoRest/TestServer/server/routes/httpResponses.js index 1e7c5c3b313b2..33e4a0fa335d2 100644 --- a/AutoRest/TestServer/server/routes/httpResponses.js +++ b/AutoRest/TestServer/server/routes/httpResponses.js @@ -418,6 +418,7 @@ var httpResponses = function(coverage, optionalCoverage) { coverage['ResponsesScenarioH202NonMatchingModel'] = 0; coverage['ResponsesScenarioEmptyErrorBody'] = 0; coverage['ResponsesScenarioNoModelErrorBody'] = 0; + optionalCoverage['ResponsesScenarioNoModelEmptyBody'] = 0; // For handling a specific null content scenario in C# var updateScenarioCoverage = function(scenario, method) { if (method.toLowerCase() === 'options') { optionalCoverage[scenario]++ @@ -463,6 +464,11 @@ var httpResponses = function(coverage, optionalCoverage) { coverage['ResponsesScenarioNoModelErrorBody']++; utils.send400(res, next, 'NoErrorModel'); }); + router.all('/failure/nomodel/empty', function(req, res, next) { + // TODO: cover this scenario + coverage['ResponsesScenarioNoModelEmptyBody']++; + res.status(400).end(); + }); router.all('/redirect/:code', function(req, res, next) { var scenario = getHttpScenarioName("Redirect", req.method, req.params.code); if (scenario !== null) { diff --git a/AutoRest/TestServer/swagger/httpInfrastructure.json b/AutoRest/TestServer/swagger/httpInfrastructure.json index b1b19b8064001..1a884f64938e6 100644 --- a/AutoRest/TestServer/swagger/httpInfrastructure.json +++ b/AutoRest/TestServer/swagger/httpInfrastructure.json @@ -52,7 +52,26 @@ "type": "boolean", "enum": [ true ] } - }, + } + } + } + }, + "/http/failure/nomodel/empty": { + "get": { + "operationId": "httpFailure_getNoModelEmpty", + "description": "Get empty response from server", + "tags": [ + "HttpFailure Operations" + ], + "responses": { + "200": { + "description": "Successfully returns 200 error code with the get request", + "schema": { + "description": "Simple boolean value true", + "type": "boolean", + "enum": [ true ] + } + } } } },