-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Use System.Text.Json's IAsyncEnumerable support #31894
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,8 +21,6 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure | |
/// </summary> | ||
public class ObjectResultExecutor : IActionResultExecutor<ObjectResult> | ||
{ | ||
private readonly AsyncEnumerableReader _asyncEnumerableReaderFactory; | ||
|
||
/// <summary> | ||
/// Creates a new <see cref="ObjectResultExecutor"/>. | ||
/// </summary> | ||
|
@@ -54,8 +52,6 @@ public ObjectResultExecutor( | |
FormatterSelector = formatterSelector; | ||
WriterFactory = writerFactory.CreateWriter; | ||
Logger = loggerFactory.CreateLogger<ObjectResultExecutor>(); | ||
var options = mvcOptions?.Value ?? throw new ArgumentNullException(nameof(mvcOptions)); | ||
_asyncEnumerableReaderFactory = new AsyncEnumerableReader(options); | ||
} | ||
|
||
/// <summary> | ||
|
@@ -103,23 +99,9 @@ public virtual Task ExecuteAsync(ActionContext context, ObjectResult result) | |
} | ||
|
||
var value = result.Value; | ||
|
||
if (value != null && _asyncEnumerableReaderFactory.TryGetReader(value.GetType(), out var reader)) | ||
{ | ||
return ExecuteAsyncEnumerable(context, result, value, reader); | ||
} | ||
|
||
return ExecuteAsyncCore(context, result, objectType, value); | ||
} | ||
|
||
private async Task ExecuteAsyncEnumerable(ActionContext context, ObjectResult result, object asyncEnumerable, Func<object, Task<ICollection>> reader) | ||
{ | ||
Log.BufferingAsyncEnumerable(Logger, asyncEnumerable); | ||
|
||
var enumerated = await reader(asyncEnumerable); | ||
await ExecuteAsyncCore(context, result, enumerated.GetType(), enumerated); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Nevermind, I see that this has been pushed down to our two other formatters. That said, this might break people creating their own formatter? Would it make sense for this to remain on the base class and have STJ formatter signal that it doesn't need it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That was one of the options I considered. The current implementation is also a bit of a whack-a-mole in that it only works for all the result types that we account for. Custom result types would have the same issue (e.g. if a user makes a XmlResult or a ProtobufResult). Given the choice of either handling it at per-formatter vs per-result, the latter seems like a better choice. I can make an announcement since this is a breaking behavior change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Result types derived from ObjectResult would have gotten this for free right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the ordinary case, yes. It's possible that there's also custom result executors that would negate it though. |
||
private Task ExecuteAsyncCore(ActionContext context, ObjectResult result, Type? objectType, object? value) | ||
{ | ||
var formatterContext = new OutputFormatterWriteContext( | ||
|
@@ -166,21 +148,7 @@ private static void InferContentTypes(ActionContext context, ObjectResult result | |
} | ||
} | ||
|
||
private static class Log | ||
{ | ||
private static readonly Action<ILogger, string?, Exception?> _bufferingAsyncEnumerable = LoggerMessage.Define<string?>( | ||
LogLevel.Debug, | ||
new EventId(1, "BufferingAsyncEnumerable"), | ||
"Buffering IAsyncEnumerable instance of type '{Type}'.", | ||
skipEnabledCheck: true); | ||
|
||
public static void BufferingAsyncEnumerable(ILogger logger, object asyncEnumerable) | ||
{ | ||
if (logger.IsEnabled(LogLevel.Debug)) | ||
{ | ||
_bufferingAsyncEnumerable(logger, asyncEnumerable.GetType().FullName, null); | ||
} | ||
} | ||
} | ||
// Removed Log. | ||
// new EventId(1, "BufferingAsyncEnumerable") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍🏾 |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this really related to this PR, or just something we need for another reason?
Could you clarify why we need it (and didn't before)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change was ported from the PR in main: https://github.com/dotnet/aspnetcore/pull/31748/files. The targeting packs in the runtime were recently renamed and this is now required if you do not reference the BlazorWebAssembly SDK (which this project does not)