Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Microsoft.Azure.Cosmos/src/CosmosClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,13 @@ public System.Text.Json.JsonSerializerOptions UseSystemTextJsonSerializerWithOpt
/// The default value is 'true'.
/// </summary>
internal bool EnableAsyncCacheExceptionNoSharing { get; set; } = true;

/// <summary>
/// Gets or sets the boolean flag to convert a text stream to binary and vice versa. When enabled, the request and response stream
/// would be converted to the desired target serialization type. This client option will remain internal only since the consumer of
/// this flag will be the internal components of the cosmos db ecosystem. The default value for this parameter is 'true'.
/// </summary>
internal bool EnableStreamConversationForBinaryEncoding { get; set; } = true;
Comment thread
kundadebdatta marked this conversation as resolved.
Outdated

/// <summary>
/// (Direct/TCP) Controls the amount of idle time after which unused connections are closed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,8 @@ private async Task<ResponseMessage> ProcessItemStreamAsync(
// engine, which does not support binary encoded content at the moment. For long term, since trigger operations won't
// be supported in the backend, avoiding the binary encoding in such cases, will be the ideal approach.
if (ConfigurationManager.IsBinaryEncodingEnabled()
&& !ContainerCore.IsTriggerPresentInRequestOptions(requestOptions))
&& !ContainerCore.IsTriggerPresentInRequestOptions(requestOptions)
&& this.ClientContext.ClientOptions.EnableStreamConversationForBinaryEncoding)
{
streamPayload = CosmosSerializationUtil.TrySerializeStreamToTargetFormat(
targetSerializationFormat: ContainerCore.GetTargetRequestSerializationFormat(),
Expand All @@ -957,7 +958,8 @@ private async Task<ResponseMessage> ProcessItemStreamAsync(
cancellationToken: cancellationToken);

// Convert Binary Stream to Text.
if (targetResponseSerializationFormat.HasValue
if (this.ClientContext.ClientOptions.EnableStreamConversationForBinaryEncoding
&& targetResponseSerializationFormat.HasValue
&& (requestOptions == null || !requestOptions.EnableBinaryResponseOnPointOperations)
&& responseMessage?.Content is CloneableStream outputCloneableStream)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,136 @@ public async Task CreateDropItemStreamTest(bool binaryEncodingEnabledInClient, b
{
Environment.SetEnvironmentVariable(ConfigurationManager.BinaryEncodingEnabled, null);
}
}

[TestMethod]
[Owner("dkunda")]
[DataRow(true, true, DisplayName = "Test scenario when binary encoding is enabled at client level and stream conversation for binary encoding is enabled.")]
[DataRow(true, false, DisplayName = "Test scenario when binary encoding is enabled at client level and stream conversation for binary encoding is disabled.")]
[DataRow(false, true, DisplayName = "Test scenario when binary encoding is disabled at client level and stream conversation for binary encoding is enabled.")]
[DataRow(false, false, DisplayName = "Test scenario when binary encoding is disabled at client level and stream conversation for binary encoding is disabled.")]
public async Task CreateItemStream_WithEnableBinaryResponseOptions_ShouldSkipStreamConversation(
bool binaryEncodingEnabledInClient,
bool enableStreamConversationForBinaryEncoding)
{
Cosmos.Database database = null;
Container container = null;
try
{
string databaseName = "binary-encoding-db";
string containerName = "binary-encoding-container";
if (binaryEncodingEnabledInClient)
{
Environment.SetEnvironmentVariable(ConfigurationManager.BinaryEncodingEnabled, "True");
}

(string endpoint, string authKey) = TestCommon.GetAccountInfo();
CosmosClientOptions clientOptions = new CosmosClientOptions()
{
EnableStreamConversationForBinaryEncoding = enableStreamConversationForBinaryEncoding,
};

CosmosClient cosmosClient = new (
endpoint,
authKey,
clientOptions);

DatabaseResponse dbResponse = await cosmosClient.CreateDatabaseIfNotExistsAsync(databaseName);
database = dbResponse.Database;

ContainerProperties properties = new (id: containerName, partitionKeyPath: "/pk");
container = await database.CreateContainerIfNotExistsAsync(properties);

ToDoActivity testItem = ToDoActivity.CreateRandomToDoActivity();
CosmosSerializerCore cosmosSerializer = new CosmosSerializerCore();
using (Stream stream = cosmosSerializer.ToStream<ToDoActivity>(
testItem,
canUseBinaryEncodingForPointOperations: binaryEncodingEnabledInClient))
{
if (binaryEncodingEnabledInClient)
{
AssertOnResponseSerializationBinaryType(stream);
}
else
{
AssertOnResponseSerializationTextType(stream);
}

using (ResponseMessage response = await container.CreateItemStreamAsync(
streamPayload: stream,
partitionKey: new Cosmos.PartitionKey(testItem.pk)))
{
Assert.IsNotNull(response);
Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);
Assert.IsTrue(response.Headers.RequestCharge > 0);
Assert.IsNotNull(response.Headers.ActivityId);
Assert.IsNotNull(response.Headers.ETag);
Assert.IsNotNull(response.Diagnostics);
Assert.IsTrue(!string.IsNullOrEmpty(response.Diagnostics.ToString()));
Assert.IsTrue(response.Diagnostics.GetClientElapsedTime() > TimeSpan.Zero);

if (enableStreamConversationForBinaryEncoding)
{
AssertOnResponseSerializationTextType(response.Content);
}
else
{
if (binaryEncodingEnabledInClient)
{
AssertOnResponseSerializationBinaryType(response.Content);
}
else
{
AssertOnResponseSerializationTextType(response.Content);
}
}
}
}

using (ResponseMessage response = await container.ReadItemStreamAsync(
id: testItem.id,
partitionKey: new Cosmos.PartitionKey(testItem.pk)))
{
Assert.IsNotNull(response);
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
Assert.IsTrue(response.Headers.RequestCharge > 0);
Assert.IsNotNull(response.Headers.ActivityId);
Assert.IsNotNull(response.Headers.ETag);
Assert.IsNotNull(response.Diagnostics);
Assert.IsTrue(!string.IsNullOrEmpty(response.Diagnostics.ToString()));
Assert.IsTrue(response.Diagnostics.GetClientElapsedTime() > TimeSpan.Zero);

if (enableStreamConversationForBinaryEncoding)
{
AssertOnResponseSerializationTextType(response.Content);
}
else
{
if (binaryEncodingEnabledInClient)
{
AssertOnResponseSerializationBinaryType(response.Content);
}
else
{
AssertOnResponseSerializationTextType(response.Content);
}
}
}
}
finally
{
Environment.SetEnvironmentVariable(ConfigurationManager.BinaryEncodingEnabled, null);

if (container != null)
{
await container.DeleteContainerStreamAsync();
}

if (database != null)
{
await database.DeleteAsync();
}
}
}

[TestMethod]
Expand Down
Loading