Q: OpenAPI for streamed JSON request body & asking about IAsyncEnumerable<T> input support #62391
-
Hi ASP.NET Core team, Scenario:I need to accept very large JSON arrays. To avoid buffering the whole payload I read That (hopefully) keeps memory usage low, but because the action has no [HttpPost]
[EndpointSummary("...")]
[EndpointDescription("...")]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(int))]
[ProducesResponseType(StatusCodes.Status401Unauthorized, Type = typeof(string))]
[ProducesResponseType(StatusCodes.Status400BadRequest, Type = typeof(string))]
public async Task<ActionResult<int>> Post(CancellationToken cancellationToken)
{
var processedCount = 0;
var jsonOptions = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
};
try
{
// The array can be a VERY LARGE array.
await foreach (var data in JsonSerializer.DeserializeAsyncEnumerable<Data>(Request.Body, jsonOptions, cancellationToken))
{
if (data == null)
{
continue;
}
// send data to SQS / background worker here
processedCount++;
}
return processedCount;
}
catch
{
return BadRequest("Invalid request body");
}
} Questions
public async Task<ActionResult<int>> Post([FromBody] IAsyncEnumerable<Data> items, CancellationToken ct) If this is (or will soon be) supported?
Related issues:
Any guidance or pointers to docs would be greatly appreciated. Thanks for your time and for all the great work on ASP.NET Core! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
If you are using .NET 9 and the built-in OpenAPI document generation, you could use an operation transformer to add the request body on that operation. The operation transformer will run for all operations and you'll have to filter to just the one you want to update.
I think manual streaming is still the right (only?) approach.
Just the one you've already encountered -- you need to fix the OpenAPI metadata to get an accurate API description. |
Beta Was this translation helpful? Give feedback.
If you are using .NET 9 and the built-in OpenAPI document generation, you could use an operation transformer to add the request body on that operation. The operation transformer will run for all operations and you'll have to filter to just the one you want to update.
I think manual streaming is still the right (only?) approach.
Just the one you've already encountered -- you need to fix the OpenAPI metadata to get an accurate API desc…