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

ODataOutputFormatter fix - Make WriteResponseBodyAsync fully async #1363

Open
wants to merge 1 commit into
base: main
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
10 changes: 6 additions & 4 deletions src/Microsoft.AspNetCore.OData/Formatter/ODataOutputFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public override void WriteResponseHeaders(OutputFormatterWriteContext context)
}

/// <inheritdoc/>
public override Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
{
if (context == null)
{
Expand All @@ -239,15 +239,17 @@ public override Task WriteResponseBodyAsync(OutputFormatterWriteContext context,
// However, OData lib doesn't provide the method to overwrite/copyto stream
// So, Here's the workaround
Stream objStream = context.Object as Stream;
return CopyStreamAsync(objStream, response);
await CopyStreamAsync(objStream, response).ConfigureAwait(false);

return;
}

Uri baseAddress = GetBaseAddressInternal(request);
MediaTypeHeaderValue contentType = GetContentType(response.Headers[HeaderNames.ContentType].FirstOrDefault());

IODataSerializerProvider serializerProvider = request.GetRouteServices().GetRequiredService<IODataSerializerProvider>();

return ODataOutputFormatterHelper.WriteToStreamAsync(
await ODataOutputFormatterHelper.WriteToStreamAsync(
type,
context.Object,
request.GetModel(),
Expand All @@ -256,7 +258,7 @@ public override Task WriteResponseBodyAsync(OutputFormatterWriteContext context,
contentType,
request,
request.Headers,
serializerProvider);
serializerProvider).ConfigureAwait(false);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.AspNetCore.OData.Extensions;
Expand Down Expand Up @@ -174,18 +175,18 @@ public void WriteResponseHeadersODataOutputFormatter_Throws_IfRequestIsNull()

#region WriteResponseBodyAsync
[Fact]
public void WriteResponseBodyAsyncODataOutputFormatter_ThrowsArgumentNull_Context()
public async Task WriteResponseBodyAsyncODataOutputFormatter_ThrowsArgumentNull_Context()
{
// Arrange & Act
ODataOutputFormatter formatter = new ODataOutputFormatter(new[] { ODataPayloadKind.Resource });
Encoding encoding = Encoding.UTF8;

// Assert
ExceptionAssert.ThrowsArgumentNull(() => formatter.WriteResponseBodyAsync(context: null, encoding), "context");
await ExceptionAssert.ThrowsArgumentNullAsync(() => formatter.WriteResponseBodyAsync(context: null, encoding), "context");
}

[Fact]
public void WriteResponseBodyAsyncODataOutputFormatter_ThrowsArgumentNull_ObjectType()
public async Task WriteResponseBodyAsyncODataOutputFormatter_ThrowsArgumentNull_ObjectType()
{
// Arrange & Act
ODataOutputFormatter formatter = new ODataOutputFormatter(new[] { ODataPayloadKind.Resource });
Expand All @@ -196,11 +197,11 @@ public void WriteResponseBodyAsyncODataOutputFormatter_ThrowsArgumentNull_Object
null);

// Assert
ExceptionAssert.ThrowsArgumentNull(() => formatter.WriteResponseBodyAsync(context, Encoding.UTF8), "type");
await ExceptionAssert.ThrowsArgumentNullAsync(() => formatter.WriteResponseBodyAsync(context, Encoding.UTF8), "type");
}

[Fact]
public void WriteResponseBodyAsyncODataOutputFormatter_Throws_IfRequestIsNull()
public async Task WriteResponseBodyAsyncODataOutputFormatter_Throws_IfRequestIsNull()
{
// Arrange & Act
ODataOutputFormatter formatter = new ODataOutputFormatter(new[] { ODataPayloadKind.Resource });
Expand All @@ -212,7 +213,7 @@ public void WriteResponseBodyAsyncODataOutputFormatter_Throws_IfRequestIsNull()
6);

// Assert
ExceptionAssert.Throws<InvalidOperationException>(
await ExceptionAssert.ThrowsAsync<InvalidOperationException>(
() => formatter.WriteResponseBodyAsync(context, Encoding.UTF8),
"The OData formatter requires an attached request in order to serialize.");
}
Expand Down