microsoft / OpenAPI.NET

The OpenAPI.NET SDK contains a useful object model for OpenAPI documents in .NET along with common serializers to extract raw OpenAPI JSON and YAML documents from the model.
MIT License
1.36k stars 229 forks source link

ASP.NET Core 3 support #421

Open jchannon opened 4 years ago

jchannon commented 4 years ago

When calling document.SerializeAsJson(context.Response.Body, OpenApiSpecVersion.OpenApi3_0); you will get an error:

There has been an error
System.InvalidOperationException: Synchronous operations are disallowed. Call WriteAsync or set AllowSynchronousIO to true.

This is a new feature in ASP.NET Core 3 that only allows async writing to the stream

jchannon commented 4 years ago

I've started work on sending a PR.

It's turning into a mammoth PR as extension methods come off extension methods etc

Basically I'm adding Async method suffixes which seems to touch everything

daniel-munch-cko commented 4 years ago

Hi, we're using the lib in the same context and have the same error on ASP.NET Core 3 - Obviously we can allow synchronous IO (AllowSynchronousIO = true), but on the long run we'd like to disable this.

Any chance the linked PR gets some traction? Happy to give a helping hand regarding tests etc. if needed!

Thanks, Daniel

darrelmiller commented 4 years ago

I'm going to try and take a look this week.

KalleOlaviNiemitalo commented 4 years ago

How about synchronously serializing the OpenAPI document into a StringBuilder or MemoryStream and then asynchronously writing that to the response stream? It would not require any changes in Microsoft.OpenAPI, and you could cache the serialized form and write it to multiple response streams. (The asynchronous writes would also support CancellationToken but I guess that is not important because you wouldn't want to keep the response stream open after canceling a write.)

darrelmiller commented 3 years ago

In 2.0 we will be converting all the write methods over to Async methods. This will allow for chunk encoded writing of a response from a web server. This will reduce memory usage when generating large OpenAPI descriptions. As a short term fix we will do what Kalle suggested and write into a buffer than then stream the full buffer.

We will solve the read problem by doing the same, buffering the incoming stream if it is unbuffered. This will increase memory usage. However, it turns out that SharpYaml is very slow in comparison to reading JSON using System.Text.JSON. So, I am planning to investigate the use of an alternate read mechanism that will use System.Text.Json if the input is JSON. This will provide a fast path for large JSON documents.

darrelmiller commented 5 months ago

We need to re-evaluate this one we have completed the work for moving over to reading JSON directly via System.Text.Json. It may be that we can do streaming reads in JSON but not in YAML.