Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable buffering for stream responses #1060

Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions src/AutoRest.CSharp/Generation/Writers/RestClientWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ private void WriteRequestCreation(CodeWriter writer, RestClientMethod clientMeth
writer.Line($"var {message:D} = {PipelineField}.CreateMessage();");
writer.Line($"var {request:D} = {message}.Request;");
var method = clientMethod.Request.HttpMethod;
if (!clientMethod.BufferResponse)
{
writer.Line($"{message}.BufferResponse = false;");
}
writer.Line($"{request}.Method = {typeof(RequestMethod)}.{method.ToRequestMethodName()};");

writer.Line($"var {uri:D} = new RawRequestUriBuilder();");
Expand Down
2 changes: 2 additions & 0 deletions src/AutoRest.CSharp/Input/CodeModelPartials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public string[] Formats
}

public string? HeaderCollectionPrefix => TryGetValue("x-ms-header-collection-prefix", out object? value) ? value?.ToString() : null;

public bool? BufferResponse => TryGetValue("x-csharp-buffer-response", out object? value) && value != null ? (bool?)Convert.ToBoolean(value) : null;
}

internal partial class ServiceResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace AutoRest.CSharp.Output.Models.Requests
{
internal class RestClientMethod
{
public RestClientMethod(string name, string? description, CSharpType? returnType, Request request, Parameter[] parameters, Response[] responses, ResponseHeaderGroupType? headerModel)
public RestClientMethod(string name, string? description, CSharpType? returnType, Request request, Parameter[] parameters, Response[] responses, ResponseHeaderGroupType? headerModel, bool bufferResponse)
{
Name = name;
Request = request;
Expand All @@ -18,6 +18,7 @@ public RestClientMethod(string name, string? description, CSharpType? returnType
Description = description;
ReturnType = returnType;
HeaderModel = headerModel;
BufferResponse = bufferResponse;
}

public string Name { get; }
Expand All @@ -26,6 +27,7 @@ public RestClientMethod(string name, string? description, CSharpType? returnType
public Parameter[] Parameters { get; }
public Response[] Responses { get; }
public ResponseHeaderGroupType? HeaderModel { get; }
public bool BufferResponse { get; }
public CSharpType? ReturnType { get; }
}
}
13 changes: 11 additions & 2 deletions src/AutoRest.CSharp/Output/Models/RestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -380,14 +380,22 @@ private RestClientMethod BuildMethod(Operation operation, HttpRequest httpReques
};
}

bool isStreamOnlyResponse = clientResponse.Count == 1 &&
clientResponse[0].ResponseBody is StreamResponseBody;

// Don't buffer stream-only responses
bool bufferResponse =
operation.Extensions?.BufferResponse ?? !isStreamOnlyResponse;

return new RestClientMethod(
operationName,
BuilderHelpers.EscapeXmlDescription(operation.Language.Default.Description),
responseType,
request,
OrderParameters(methodParameters.Values),
clientResponse.ToArray(),
responseHeaderModel
responseHeaderModel,
bufferResponse
);
}

Expand Down Expand Up @@ -521,7 +529,8 @@ private static RestClientMethod BuildNextPageMethod(RestClientMethod method, Ope
request,
parameters,
responses,
method.HeaderModel);
method.HeaderModel,
bufferResponse: true);
}

public RestClientMethod? GetNextOperationMethod(ServiceRequest request)
Expand Down
3 changes: 3 additions & 0 deletions test/AutoRest.TestServer.Tests/body-file.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public Task FileStreamVeryLarge() => Test(async (host, pipeline) =>
}

Assert.AreEqual(3000 * 1024 * 1024L, total);
Assert.False(stream.CanSeek);
await result.Value.DisposeAsync().ConfigureAwait(false);
}, ignoreScenario: false, useSimplePipeline: true);

Expand All @@ -58,6 +59,7 @@ public Task FileStreamVeryLarge_Sync() => Test((host, pipeline) =>
var result = new FilesClient(ClientDiagnostics, pipeline, host).GetFileLarge();
var buffer = new byte[2 * 1024 * 1024L];
var stream = result.Value;

long total = 0;
var count = stream.Read(buffer, 0, buffer.Length);
while (count > 0)
Expand All @@ -67,6 +69,7 @@ public Task FileStreamVeryLarge_Sync() => Test((host, pipeline) =>
}

Assert.AreEqual(3000 * 1024 * 1024L, total);
Assert.False(stream.CanSeek);
result.Value.Dispose();
}, ignoreScenario: false, useSimplePipeline: true);
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.