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

Write the HTTP response only when all output has been formatted #858

Open
wants to merge 4 commits into
base: release-8.x
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using Microsoft.AspNetCore.OData.Formatter.Value;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add a test for this functionality ?

using Microsoft.AspNetCore.OData.Query;
using Microsoft.AspNetCore.OData.Routing;
using Microsoft.IO;
using Microsoft.Net.Http.Headers;
using Microsoft.OData;
using Microsoft.OData.Edm;
Expand All @@ -31,6 +32,8 @@ namespace Microsoft.AspNetCore.OData.Formatter
{
internal static class ODataOutputFormatterHelper
{
private static readonly Lazy<RecyclableMemoryStreamManager> recycleableMemoryStreamManager = new Lazy<RecyclableMemoryStreamManager>();

public static ODataSerializerContext BuildSerializerContext(HttpRequest request)
{
if (request == null)
Expand Down Expand Up @@ -68,18 +71,19 @@ internal static async Task WriteToStreamAsync(
ODataPath path = request.ODataFeature().Path;
IEdmNavigationSource targetNavigationSource = path.GetNavigationSource();
HttpResponse response = request.HttpContext.Response;
MemoryStream responseBody = recycleableMemoryStreamManager.Value.GetStream(nameof(ODataOutputFormatterHelper.WriteToStreamAsync));

// serialize a response
string preferHeader = RequestPreferenceHelpers.GetRequestPreferHeader(requestHeaders);
string annotationFilter = null;
if (!string.IsNullOrEmpty(preferHeader))
{
ODataMessageWrapper messageWrapper = ODataMessageWrapperHelper.Create(response.Body, response.Headers);
ODataMessageWrapper messageWrapper = ODataMessageWrapperHelper.Create(responseBody, response.Headers);
messageWrapper.SetHeader(RequestPreferenceHelpers.PreferHeaderName, preferHeader);
annotationFilter = messageWrapper.PreferHeader().AnnotationFilter;
}

IODataResponseMessageAsync responseMessage = ODataMessageWrapperHelper.Create(new StreamWrapper(response.Body), response.Headers, request.GetRouteServices());
IODataResponseMessageAsync responseMessage = ODataMessageWrapperHelper.Create(new StreamWrapper(responseBody), response.Headers, request.GetRouteServices());
if (annotationFilter != null)
{
responseMessage.PreferenceAppliedHeader().AnnotationFilter = annotationFilter;
Expand Down Expand Up @@ -131,6 +135,7 @@ internal static async Task WriteToStreamAsync(
metadataLevel = ODataMediaTypes.GetMetadataLevel(contentType.MediaType.ToString(), parameters);
}

using (responseBody)
using (ODataMessageWriter messageWriter = new ODataMessageWriter(responseMessage, writerSettings, model))
{
ODataSerializerContext writeContext = BuildSerializerContext(request);
Expand All @@ -150,6 +155,8 @@ internal static async Task WriteToStreamAsync(
}

await serializer.WriteObjectAsync(value, type, messageWriter, writeContext).ConfigureAwait(false);
responseBody.Position = 0;
await responseBody.CopyToAsync(response.Body);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="2.3.2" />
<PackageReference Include="Microsoft.OData.ModelBuilder" Version="1.0.8" />
<PackageReference Include="Microsoft.OData.Core" Version="7.12.5" />
<PackageReference Include="Microsoft.OData.Edm" Version="7.12.5" />
Expand Down