From 209d5cfe92875b3593eb9461de5e877a6a8df77a Mon Sep 17 00:00:00 2001 From: Anne Thompson Date: Mon, 13 May 2024 08:32:31 -0700 Subject: [PATCH] Make RequestOptions.BufferResponse non-nullable (#43990) --- sdk/core/System.ClientModel/CHANGELOG.md | 2 +- .../api/System.ClientModel.net6.0.cs | 2 +- .../api/System.ClientModel.netstandard2.0.cs | 2 +- .../src/Options/RequestOptions.cs | 15 +++++-------- .../tests/Options/RequestOptionsTests.cs | 21 +------------------ 5 files changed, 9 insertions(+), 33 deletions(-) diff --git a/sdk/core/System.ClientModel/CHANGELOG.md b/sdk/core/System.ClientModel/CHANGELOG.md index 16ae29a238b2..5e464475e950 100644 --- a/sdk/core/System.ClientModel/CHANGELOG.md +++ b/sdk/core/System.ClientModel/CHANGELOG.md @@ -4,7 +4,7 @@ ### Features Added -- Added `BufferResponse` property to `RequestOptions` so protocol method callers can override the client value for response content buffering. +- Added `BufferResponse` property to `RequestOptions` so protocol method callers can turn off response buffering if desired. ### Breaking Changes diff --git a/sdk/core/System.ClientModel/api/System.ClientModel.net6.0.cs b/sdk/core/System.ClientModel/api/System.ClientModel.net6.0.cs index 39bd7abd6ca0..28d11b73ce27 100644 --- a/sdk/core/System.ClientModel/api/System.ClientModel.net6.0.cs +++ b/sdk/core/System.ClientModel/api/System.ClientModel.net6.0.cs @@ -239,7 +239,7 @@ public sealed override void Process(System.ClientModel.Primitives.PipelineMessag public partial class RequestOptions { public RequestOptions() { } - public bool? BufferResponse { get { throw null; } set { } } + public bool BufferResponse { get { throw null; } set { } } public System.Threading.CancellationToken CancellationToken { get { throw null; } set { } } public System.ClientModel.Primitives.ClientErrorBehaviors ErrorOptions { get { throw null; } set { } } public void AddHeader(string name, string value) { } diff --git a/sdk/core/System.ClientModel/api/System.ClientModel.netstandard2.0.cs b/sdk/core/System.ClientModel/api/System.ClientModel.netstandard2.0.cs index d4603d1fd190..4270fc544902 100644 --- a/sdk/core/System.ClientModel/api/System.ClientModel.netstandard2.0.cs +++ b/sdk/core/System.ClientModel/api/System.ClientModel.netstandard2.0.cs @@ -238,7 +238,7 @@ public sealed override void Process(System.ClientModel.Primitives.PipelineMessag public partial class RequestOptions { public RequestOptions() { } - public bool? BufferResponse { get { throw null; } set { } } + public bool BufferResponse { get { throw null; } set { } } public System.Threading.CancellationToken CancellationToken { get { throw null; } set { } } public System.ClientModel.Primitives.ClientErrorBehaviors ErrorOptions { get { throw null; } set { } } public void AddHeader(string name, string value) { } diff --git a/sdk/core/System.ClientModel/src/Options/RequestOptions.cs b/sdk/core/System.ClientModel/src/Options/RequestOptions.cs index f7649c0b8eab..b12167ee7343 100644 --- a/sdk/core/System.ClientModel/src/Options/RequestOptions.cs +++ b/sdk/core/System.ClientModel/src/Options/RequestOptions.cs @@ -16,6 +16,7 @@ public class RequestOptions private CancellationToken _cancellationToken = CancellationToken.None; private ClientErrorBehaviors _errorOptions = ClientErrorBehaviors.Default; + private bool _bufferResponse = true; private PipelinePolicy[]? _perCallPolicies; private PipelinePolicy[]? _perTryPolicies; @@ -23,8 +24,6 @@ public class RequestOptions private List? _headersUpdates; - private bool? _bufferResponse; - /// /// Initializes a new instance of the class /// @@ -64,15 +63,14 @@ public ClientErrorBehaviors ErrorOptions /// /// Gets or sets a value indicating whether the response content should be - /// buffered in-memory by the pipeline. If specified, this value will - /// override any client default. + /// buffered in-memory by the pipeline. This value defaults to true. /// /// Please note that setting this value to false will result /// in the obtained from /// holding a live network stream. /// It is the responsibility of the caller to ensure the stream is disposed. /// - public bool? BufferResponse + public bool BufferResponse { get => _bufferResponse; set @@ -174,11 +172,8 @@ protected internal virtual void Apply(PipelineMessage message) message.PerTryPolicies = _perTryPolicies; message.BeforeTransportPolicies = _beforeTransportPolicies; - // Override any BufferResponse value set on the message. - if (BufferResponse.HasValue) - { - message.BufferResponse = BufferResponse.Value; - } + // Tell transport whether or not to buffer response content. + message.BufferResponse = BufferResponse; // Apply adds and sets to request headers if applicable. if (_headersUpdates is not null) diff --git a/sdk/core/System.ClientModel/tests/Options/RequestOptionsTests.cs b/sdk/core/System.ClientModel/tests/Options/RequestOptionsTests.cs index 66b9ba2f9d1d..8099080e1a2e 100644 --- a/sdk/core/System.ClientModel/tests/Options/RequestOptionsTests.cs +++ b/sdk/core/System.ClientModel/tests/Options/RequestOptionsTests.cs @@ -164,7 +164,7 @@ public void CannotModifyOptionsAfterExplicitlyFrozen() } [Test] - public void OverridesBufferResponse() + public void SetsBufferResponse() { ClientPipeline pipeline = ClientPipeline.Create(); PipelineMessage message = pipeline.CreateMessage(); @@ -181,23 +181,4 @@ public void OverridesBufferResponse() Assert.IsFalse(message.BufferResponse); } - - [Test] - public void DoesntChangeBufferResponse() - { - RequestOptions options = new() { BufferResponse = null }; - - ClientPipeline pipeline = ClientPipeline.Create(); - PipelineMessage message = pipeline.CreateMessage(); - - message.BufferResponse = false; - message.Apply(options); - - Assert.IsFalse(message.BufferResponse); - - message.BufferResponse = true; - message.Apply(options); - - Assert.IsTrue(message.BufferResponse); - } }