Skip to content

Commit

Permalink
Fix possible NullReferenceException on a TransactionalBatch code path (
Browse files Browse the repository at this point in the history
…#1086)

* Add missing null check on a Transactional batch code path; don't promote operation status for internal use

* Add to changelog
  • Loading branch information
abhijitpai authored and kirankumarkolli committed Dec 10, 2019
1 parent 3fa0eba commit df298c4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
20 changes: 15 additions & 5 deletions Microsoft.Azure.Cosmos/src/Batch/TransactionalBatchResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ IEnumerator IEnumerable.GetEnumerator()
internal static async Task<TransactionalBatchResponse> FromResponseMessageAsync(
ResponseMessage responseMessage,
ServerBatchRequest serverRequest,
CosmosSerializer serializer)
CosmosSerializer serializer,
bool shouldPromoteOperationStatus = true)
{
using (responseMessage)
{
Expand All @@ -230,7 +231,13 @@ internal static async Task<TransactionalBatchResponse> FromResponseMessageAsync(
if (content.ReadByte() == (int)HybridRowVersion.V1)
{
content.Position = 0;
response = await TransactionalBatchResponse.PopulateFromContentAsync(content, responseMessage, serverRequest, serializer);
response = await TransactionalBatchResponse.PopulateFromContentAsync(
content,
responseMessage,
serverRequest,
serializer,
shouldPromoteOperationStatus);

if (response == null)
{
// Convert any payload read failures as InternalServerError
Expand All @@ -247,7 +254,8 @@ internal static async Task<TransactionalBatchResponse> FromResponseMessageAsync(
}
}
}
else

if (response == null)
{
response = new TransactionalBatchResponse(
responseMessage.StatusCode,
Expand Down Expand Up @@ -317,7 +325,8 @@ private static async Task<TransactionalBatchResponse> PopulateFromContentAsync(
Stream content,
ResponseMessage responseMessage,
ServerBatchRequest serverRequest,
CosmosSerializer serializer)
CosmosSerializer serializer,
bool shouldPromoteOperationStatus)
{
List<TransactionalBatchOperationResult> results = new List<TransactionalBatchOperationResult>();

Expand Down Expand Up @@ -348,7 +357,8 @@ record =>

// Promote the operation error status as the Batch response error status if we have a MultiStatus response
// to provide users with status codes they are used to.
if ((int)responseMessage.StatusCode == (int)StatusCodes.MultiStatus)
if ((int)responseMessage.StatusCode == (int)StatusCodes.MultiStatus
&& shouldPromoteOperationStatus)
{
foreach (TransactionalBatchOperationResult result in results)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Microsoft.Azure.Cosmos.Tests
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Client.Core.Tests;
Expand Down Expand Up @@ -338,6 +339,33 @@ public async Task BatchSingleServerResponseAsync()
Assert.IsNull(batchResponse[1].ResourceStream);
}

[TestMethod]
[Owner("abpai")]
public async Task BatchJsonServerResponseAsync()
{
TestHandler testHandler = new TestHandler((request, cancellationToken) =>
{
HttpStatusCode serverStatusCode = HttpStatusCode.Gone;
Stream serverContent = new MemoryStream(Encoding.UTF8.GetBytes("{ \"random\": 1 }"));
ResponseMessage responseMessage = new ResponseMessage(serverStatusCode, requestMessage: null, errorMessage: null)
{
Content = serverContent
};
return Task.FromResult(responseMessage);
});

Container container = BatchUnitTests.GetContainer(testHandler);

TransactionalBatchResponse batchResponse = await new BatchCore((ContainerCore)container, new Cosmos.PartitionKey(BatchUnitTests.PartitionKey1))
.ReadItem("id1")
.ReadItem("id2")
.ExecuteAsync();

Assert.AreEqual(HttpStatusCode.Gone, batchResponse.StatusCode);
}

/// <summary>
/// Test to make sure IsFeedRequest is true for Batch operation
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- [#1075](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/1075) Including header size details for BadRequest with large headers

- [#1086](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/1086) Fix possible NullReferenceException on a TransactionalBatch code path


### Fixed
Expand Down

0 comments on commit df298c4

Please sign in to comment.