diff --git a/Microsoft.Azure.Cosmos/src/Batch/BatchCore.cs b/Microsoft.Azure.Cosmos/src/Batch/BatchCore.cs index 64020497a5..345fcaa1e5 100644 --- a/Microsoft.Azure.Cosmos/src/Batch/BatchCore.cs +++ b/Microsoft.Azure.Cosmos/src/Batch/BatchCore.cs @@ -48,8 +48,7 @@ public override TransactionalBatch CreateItem( operationIndex: this.operations.Count, resource: item, requestOptions: requestOptions, - containerCore: this.container, - cosmosClientContext: this.container.ClientContext)); + containerCore: this.container)); return this; } @@ -68,8 +67,7 @@ public override TransactionalBatch CreateItemStream( operationIndex: this.operations.Count, resourceStream: streamPayload, requestOptions: requestOptions, - containerCore: this.container, - cosmosClientContext: this.container.ClientContext)); + containerCore: this.container)); return this; } @@ -88,8 +86,7 @@ public override TransactionalBatch ReadItem( operationIndex: this.operations.Count, id: id, requestOptions: requestOptions, - containerCore: this.container, - cosmosClientContext: this.container.ClientContext)); + containerCore: this.container)); return this; } @@ -108,8 +105,7 @@ public override TransactionalBatch UpsertItem( operationIndex: this.operations.Count, resource: item, requestOptions: requestOptions, - containerCore: this.container, - cosmosClientContext: this.container.ClientContext)); + containerCore: this.container)); return this; } @@ -128,8 +124,7 @@ public override TransactionalBatch UpsertItemStream( operationIndex: this.operations.Count, resourceStream: streamPayload, requestOptions: requestOptions, - containerCore: this.container, - cosmosClientContext: this.container.ClientContext)); + containerCore: this.container)); return this; } @@ -155,8 +150,7 @@ public override TransactionalBatch ReplaceItem( id: id, resource: item, requestOptions: requestOptions, - containerCore: this.container, - cosmosClientContext: this.container.ClientContext)); + containerCore: this.container)); return this; } @@ -182,8 +176,7 @@ public override TransactionalBatch ReplaceItemStream( id: id, resourceStream: streamPayload, requestOptions: requestOptions, - containerCore: this.container, - cosmosClientContext: this.container.ClientContext)); + containerCore: this.container)); return this; } @@ -202,8 +195,7 @@ public override TransactionalBatch DeleteItem( operationIndex: this.operations.Count, id: id, requestOptions: requestOptions, - containerCore: this.container, - cosmosClientContext: this.container.ClientContext)); + containerCore: this.container)); return this; } @@ -261,8 +253,7 @@ public virtual TransactionalBatch PatchItemStream( id: id, resourceStream: patchStream, requestOptions: requestOptions, - containerCore: this.container, - cosmosClientContext: this.container.ClientContext)); + containerCore: this.container)); return this; } @@ -301,8 +292,7 @@ TransactionalBatch PatchItem( id: id, resource: patchOperations, requestOptions: requestOptions, - containerCore: this.container, - cosmosClientContext: this.container.ClientContext)); + containerCore: this.container)); return this; } diff --git a/Microsoft.Azure.Cosmos/src/Batch/ItemBatchOperation.cs b/Microsoft.Azure.Cosmos/src/Batch/ItemBatchOperation.cs index 3423249c2f..437567f70f 100644 --- a/Microsoft.Azure.Cosmos/src/Batch/ItemBatchOperation.cs +++ b/Microsoft.Azure.Cosmos/src/Batch/ItemBatchOperation.cs @@ -53,8 +53,7 @@ public ItemBatchOperation( ContainerInternal containerCore, string id = null, Stream resourceStream = null, - TransactionalBatchItemRequestOptions requestOptions = null, - CosmosClientContext cosmosClientContext = null) + TransactionalBatchItemRequestOptions requestOptions = null) { this.OperationType = operationType; this.OperationIndex = operationIndex; @@ -63,7 +62,7 @@ public ItemBatchOperation( this.ResourceStream = resourceStream; this.RequestOptions = requestOptions; this.DiagnosticsContext = null; - this.ClientContext = cosmosClientContext; + this.ClientContext = containerCore.ClientContext; } public PartitionKey? PartitionKey { get; internal set; } @@ -174,18 +173,6 @@ internal static Result WriteOperation(ref RowWriter writer, TypeArgument typeArg return r; } } - - if (ItemRequestOptions.ShouldSetNoContentHeader( - options.EnableContentResponseOnWrite, - options.EnableContentResponseOnRead, - operation.OperationType)) - { - r = writer.WriteBool("minimalReturnPreference", true); - if (r != Result.Success) - { - return r; - } - } if (options.IfMatchEtag != null) { @@ -258,10 +245,10 @@ internal static Result WriteOperation(ref RowWriter writer, TypeArgument typeArg } } - // If EnableContentResponse is set at Client level and not at Indivisual Item Level - if (operation.ClientContext != null - && RequestInvokerHandler.ShouldSetClientLevelNoContentResponseHeaders(operation.RequestOptions, - operation.ClientContext.ClientOptions, operation.OperationType, ResourceType.Document)) + if (RequestInvokerHandler.ShouldSetNoContentResponseHeaders(operation.RequestOptions, + operation.ClientContext?.ClientOptions, + operation.OperationType, + ResourceType.Document)) { r = writer.WriteBool("minimalReturnPreference", true); if (r != Result.Success) @@ -402,9 +389,8 @@ public ItemBatchOperation( T resource, ContainerInternal containerCore, string id = null, - TransactionalBatchItemRequestOptions requestOptions = null, - CosmosClientContext cosmosClientContext = null) - : base(operationType, operationIndex, containerCore: containerCore, id: id, requestOptions: requestOptions, cosmosClientContext: cosmosClientContext) + TransactionalBatchItemRequestOptions requestOptions = null) + : base(operationType, operationIndex, containerCore: containerCore, id: id, requestOptions: requestOptions) { this.Resource = resource; } diff --git a/Microsoft.Azure.Cosmos/src/Batch/TransactionalBatchItemRequestOptions.cs b/Microsoft.Azure.Cosmos/src/Batch/TransactionalBatchItemRequestOptions.cs index 0fcec01d41..4f36b7b239 100644 --- a/Microsoft.Azure.Cosmos/src/Batch/TransactionalBatchItemRequestOptions.cs +++ b/Microsoft.Azure.Cosmos/src/Batch/TransactionalBatchItemRequestOptions.cs @@ -29,17 +29,6 @@ public class TransactionalBatchItemRequestOptions : RequestOptions /// public bool? EnableContentResponseOnWrite { get; set; } - /// - /// Gets or sets the boolean to only return the headers and status code in - /// the Cosmos DB response for read item operations like ReadItem - /// This removes the resource from the response. This reduces networking and CPU load by not sending - /// the resource back over the network and serializing it on the client. - /// - /// - /// This is optimal for workloads where the returned resource is not used. - /// - internal bool? EnableContentResponseOnRead { get; set; } - internal static TransactionalBatchItemRequestOptions FromItemRequestOptions(ItemRequestOptions itemRequestOptions) { if (itemRequestOptions == null) @@ -55,7 +44,6 @@ internal static TransactionalBatchItemRequestOptions FromItemRequestOptions(Item IfNoneMatchEtag = itemRequestOptions.IfNoneMatchEtag, Properties = itemRequestOptions.Properties, EnableContentResponseOnWrite = itemRequestOptions.EnableContentResponseOnWrite, - EnableContentResponseOnRead = itemRequestOptions.EnableContentResponseOnRead, IsEffectivePartitionKeyRouting = itemRequestOptions.IsEffectivePartitionKeyRouting }; return batchItemRequestOptions; diff --git a/Microsoft.Azure.Cosmos/src/Handler/RequestInvokerHandler.cs b/Microsoft.Azure.Cosmos/src/Handler/RequestInvokerHandler.cs index 321871ce57..1ab43a72b9 100644 --- a/Microsoft.Azure.Cosmos/src/Handler/RequestInvokerHandler.cs +++ b/Microsoft.Azure.Cosmos/src/Handler/RequestInvokerHandler.cs @@ -56,8 +56,7 @@ public override async Task SendAsync( } // Adds the NoContent header if not already added based on Client Level flag - if (RequestInvokerHandler.ShouldSetClientLevelNoContentResponseHeaders(request.RequestOptions, this.client.ClientOptions, - request.OperationType, request.ResourceType)) + if (RequestInvokerHandler.ShouldSetNoContentResponseHeaders(request.RequestOptions, this.client.ClientOptions, request.OperationType, request.ResourceType)) { request.Headers.Add(HttpConstants.HttpHeaders.Prefer, HttpConstants.HttpHeaderValues.PreferReturnMinimal); } @@ -389,7 +388,7 @@ private async Task ValidateAndSetConsistencyLevelAsync(RequestMessage requestMes } } - internal static bool ShouldSetClientLevelNoContentResponseHeaders(RequestOptions requestOptions, + internal static bool ShouldSetNoContentResponseHeaders(RequestOptions requestOptions, CosmosClientOptions clientOptions, OperationType operationType, ResourceType resourceType) @@ -399,22 +398,52 @@ internal static bool ShouldSetClientLevelNoContentResponseHeaders(RequestOptions return false; } - if (requestOptions == null - || ((requestOptions is ItemRequestOptions itemRequestOptions) && (!itemRequestOptions.EnableContentResponseOnWrite.HasValue)) - || ((requestOptions is TransactionalBatchItemRequestOptions batchRequestOptions) && (!batchRequestOptions.EnableContentResponseOnWrite.HasValue))) + if (requestOptions == null) { return RequestInvokerHandler.IsClientNoResponseSet(clientOptions, operationType); } + if (requestOptions is ItemRequestOptions itemRequestOptions) + { + if (itemRequestOptions.EnableContentResponseOnWrite.HasValue) + { + return IsItemNoRepsonseSet(itemRequestOptions.EnableContentResponseOnWrite.Value, operationType); + } + else + { + return IsClientNoResponseSet(clientOptions, operationType); + } + } + + if (requestOptions is TransactionalBatchItemRequestOptions batchRequestOptions) + { + if (batchRequestOptions.EnableContentResponseOnWrite.HasValue) + { + return IsItemNoRepsonseSet(batchRequestOptions.EnableContentResponseOnWrite.Value, operationType); + } + else + { + return IsClientNoResponseSet(clientOptions, operationType); + } + } + return false; } + private static bool IsItemNoRepsonseSet(bool enableContentResponseOnWrite, OperationType operationType) + { + return !enableContentResponseOnWrite && + (operationType == OperationType.Create || + operationType == OperationType.Replace || + operationType == OperationType.Upsert || + operationType == OperationType.Patch); + } + private static bool IsClientNoResponseSet(CosmosClientOptions clientOptions, OperationType operationType) { return clientOptions != null - && ItemRequestOptions.ShouldSetNoContentHeader(enableContentResponseOnWrite: clientOptions.EnableContentResponseOnWrite, - enableContentResponseOnRead: null, - operationType: operationType); + && clientOptions.EnableContentResponseOnWrite.HasValue + && RequestInvokerHandler.IsItemNoRepsonseSet(clientOptions.EnableContentResponseOnWrite.Value, operationType); } } } diff --git a/Microsoft.Azure.Cosmos/src/RequestOptions/ItemRequestOptions.cs b/Microsoft.Azure.Cosmos/src/RequestOptions/ItemRequestOptions.cs index ebd8393fb0..2a6e827bc4 100644 --- a/Microsoft.Azure.Cosmos/src/RequestOptions/ItemRequestOptions.cs +++ b/Microsoft.Azure.Cosmos/src/RequestOptions/ItemRequestOptions.cs @@ -118,27 +118,6 @@ public ConsistencyLevel? ConsistencyLevel /// public bool? EnableContentResponseOnWrite { get; set; } - /// - /// Gets or sets the boolean to only return the headers and status code in - /// the Cosmos DB response for read item operations like ReadItemAsync - /// This removes the resource from the response. This reduces networking and CPU load by not sending - /// the resource back over the network and serializing it on the client. - /// - /// - /// - /// (tests, new PartitionKey(test.status), requestOptions); - /// Assert.AreEqual(HttpStatusCode.Created, itemResponse.StatusCode); - /// Assert.IsNull(itemResponse.Resource); - /// ]]> - /// - /// - /// - /// This is optimal for workloads where the returned resource is not used. - /// - internal bool? EnableContentResponseOnRead { get; set; } - /// /// Fill the CosmosRequestMessage headers with the set properties /// @@ -163,36 +142,7 @@ internal override void PopulateRequestOptions(RequestMessage request) } RequestOptions.SetSessionToken(request, this.SessionToken); - - if (ItemRequestOptions.ShouldSetNoContentHeader( - this.EnableContentResponseOnWrite, - this.EnableContentResponseOnRead, - request.OperationType)) - { - request.Headers.Add(HttpConstants.HttpHeaders.Prefer, HttpConstants.HttpHeaderValues.PreferReturnMinimal); - } - base.PopulateRequestOptions(request); } - - internal static bool ShouldSetNoContentHeader( - bool? enableContentResponseOnWrite, - bool? enableContentResponseOnRead, - OperationType operationType) - { - if (enableContentResponseOnRead.HasValue && - !enableContentResponseOnRead.Value && - operationType == OperationType.Read) - { - return true; - } - - return enableContentResponseOnWrite.HasValue && - !enableContentResponseOnWrite.Value && - (operationType == OperationType.Create || - operationType == OperationType.Replace || - operationType == OperationType.Upsert || - operationType == OperationType.Patch); - } } } \ No newline at end of file diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosContentResponseTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosContentResponseTests.cs index 233a19e7bd..9f89041d6c 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosContentResponseTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosContentResponseTests.cs @@ -61,20 +61,6 @@ await this.Validate( this.ValidateItemResponse); } - [TestMethod] - public async Task ItemReadNoResponseTest() - { - ItemRequestOptions requestOptions = new ItemRequestOptions() - { - EnableContentResponseOnRead = false - }; - - await this.Validate( - requestOptions, - this.ValidateItemResponse, - this.ValidateItemNoContentResponse); - } - private async Task Validate( ItemRequestOptions requestOptions, Action> ValidateWrite, @@ -138,20 +124,6 @@ await this.ValidateItemStream( this.ValidateItemStreamResponse); } - [TestMethod] - public async Task ItemStreamReadNoResponseTest() - { - ItemRequestOptions requestOptions = new ItemRequestOptions() - { - EnableContentResponseOnRead = false - }; - - await this.ValidateItemStream( - requestOptions, - this.ValidateItemStreamResponse, - this.ValidateItemStreamNoContentResponse); - } - private async Task ValidateItemStream( ItemRequestOptions requestOptions, Action ValidateWrite, diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Contracts/DotNetSDKAPI.json b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Contracts/DotNetSDKAPI.json index 8be83578a9..66d1b7b898 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Contracts/DotNetSDKAPI.json +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Contracts/DotNetSDKAPI.json @@ -1574,6 +1574,18 @@ "Attributes": [], "MethodInfo": "System.Nullable`1[Microsoft.Azure.Cosmos.PortReuseMode] PortReuseMode;CanRead:True;CanWrite:True;System.Nullable`1[Microsoft.Azure.Cosmos.PortReuseMode] get_PortReuseMode();IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;Void set_PortReuseMode(System.Nullable`1[Microsoft.Azure.Cosmos.PortReuseMode]);IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;" }, + "System.Nullable`1[System.Boolean] EnableContentResponseOnWrite": { + "Type": "Property", + "Attributes": [], + "MethodInfo": "System.Nullable`1[System.Boolean] EnableContentResponseOnWrite;CanRead:True;CanWrite:True;System.Nullable`1[System.Boolean] get_EnableContentResponseOnWrite();IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;Void set_EnableContentResponseOnWrite(System.Nullable`1[System.Boolean]);IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;" + }, + "System.Nullable`1[System.Boolean] get_EnableContentResponseOnWrite()[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]": { + "Type": "Method", + "Attributes": [ + "CompilerGeneratedAttribute" + ], + "MethodInfo": "System.Nullable`1[System.Boolean] get_EnableContentResponseOnWrite();IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;" + }, "System.Nullable`1[System.Int32] get_MaxRequestsPerTcpConnection()": { "Type": "Method", "Attributes": [], @@ -1719,6 +1731,13 @@ ], "MethodInfo": "Void set_ConsistencyLevel(System.Nullable`1[Microsoft.Azure.Cosmos.ConsistencyLevel]);IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;" }, + "Void set_EnableContentResponseOnWrite(System.Nullable`1[System.Boolean])[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]": { + "Type": "Method", + "Attributes": [ + "CompilerGeneratedAttribute" + ], + "MethodInfo": "Void set_EnableContentResponseOnWrite(System.Nullable`1[System.Boolean]);IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;" + }, "Void set_EnableTcpConnectionEndpointRediscovery(Boolean)[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]": { "Type": "Method", "Attributes": [ @@ -2855,6 +2874,11 @@ "Attributes": [], "MethodInfo": "Microsoft.Azure.Cosmos.Fluent.CosmosClientBuilder WithConsistencyLevel(Microsoft.Azure.Cosmos.ConsistencyLevel);IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;" }, + "Microsoft.Azure.Cosmos.Fluent.CosmosClientBuilder WithContentResponseOnWriteEnabled(Boolean)": { + "Type": "Method", + "Attributes": [], + "MethodInfo": "Microsoft.Azure.Cosmos.Fluent.CosmosClientBuilder WithContentResponseOnWriteEnabled(Boolean);IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;" + }, "Microsoft.Azure.Cosmos.Fluent.CosmosClientBuilder WithCustomSerializer(Microsoft.Azure.Cosmos.CosmosSerializer)": { "Type": "Method", "Attributes": [],