Skip to content

Commit

Permalink
Make RequestOptions.BufferResponse non-nullable (#43990)
Browse files Browse the repository at this point in the history
  • Loading branch information
annelo-msft authored May 13, 2024
1 parent fffa7d7 commit 209d5cf
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 33 deletions.
2 changes: 1 addition & 1 deletion sdk/core/System.ClientModel/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
Expand Down
15 changes: 5 additions & 10 deletions sdk/core/System.ClientModel/src/Options/RequestOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ public class RequestOptions

private CancellationToken _cancellationToken = CancellationToken.None;
private ClientErrorBehaviors _errorOptions = ClientErrorBehaviors.Default;
private bool _bufferResponse = true;

private PipelinePolicy[]? _perCallPolicies;
private PipelinePolicy[]? _perTryPolicies;
private PipelinePolicy[]? _beforeTransportPolicies;

private List<HeadersUpdate>? _headersUpdates;

private bool? _bufferResponse;

/// <summary>
/// Initializes a new instance of the <see cref="RequestOptions"/> class
/// </summary>
Expand Down Expand Up @@ -64,15 +63,14 @@ public ClientErrorBehaviors ErrorOptions

/// <summary>
/// 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 <c>true</c>.
/// </summary>
/// <remarks>Please note that setting this value to <c>false</c> will result
/// in the <see cref="PipelineResponse.ContentStream"/> obtained from
/// <see cref="ClientResult.GetRawResponse"/> holding a live network stream.
/// It is the responsibility of the caller to ensure the stream is disposed.
/// </remarks>
public bool? BufferResponse
public bool BufferResponse
{
get => _bufferResponse;
set
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 1 addition & 20 deletions sdk/core/System.ClientModel/tests/Options/RequestOptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public void CannotModifyOptionsAfterExplicitlyFrozen()
}

[Test]
public void OverridesBufferResponse()
public void SetsBufferResponse()
{
ClientPipeline pipeline = ClientPipeline.Create();
PipelineMessage message = pipeline.CreateMessage();
Expand All @@ -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);
}
}

0 comments on commit 209d5cf

Please sign in to comment.