OpenAPI.NET icon indicating copy to clipboard operation
OpenAPI.NET copied to clipboard

OpenAPIStreamReader fail to read : Reading is not allowed after reader was completed.

Open DCollart opened this issue 5 years ago • 2 comments

I tried to follow this code showd in the main page :

var stream = await httpClient.GetStreamAsync("master/examples/v3.0/petstore.yaml");
// Read V3 as YAML
var openApiDocument = new OpenApiStreamReader().Read(stream, out var diagnostic);

My code is more or less the same :

var stream = await _client.GetStreamAsync("/swagger/v1/swagger.json");

 var openApiDocument = new OpenApiStreamReader(new OpenApiReaderSettings() { });
var temp = openApiDocument.Read(stream, out var diagnostic);

And I get this error System.InvalidOperationException: 'Reading is not allowed after reader was completed.'

The only difference is that I'm expecting a JSON and not a YAML but I didn't find some options to change that and the error doesn't seems related to this.

Microsoft.OpenApi : 1.2.3 Microsoft.OpenApi.Readers : 1.2.3

Thank you :-)

DCollart avatar Oct 01 '20 15:10 DCollart

The underlying YAML parser should happily consume the JSON file without any different setting. JSON is a compatible subset of YAML.

I will try and repro.

darrelmiller avatar Oct 11 '20 15:10 darrelmiller

try this, it is more robust than async concept...

private void GetServiceJSONDocument()
{
	Microsoft.OpenApi.Models.OpenApiDocument JsonDoc = new Microsoft.OpenApi.Models.OpenApiDocument();
	HttpWebResponse wresponse = GetServiceAPIRequest(false, SiteAPIURL).GetResponse() as HttpWebResponse;
	JsonDoc = new OpenApiStreamReader().Read(wresponse.GetResponseStream(), out var diagnostic); 
}

private static HttpWebRequest GetServiceAPIRequest(bool isPOST, string atvURL)
{
	HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(atvURL);
	webRequest.ServicePoint.Expect100Continue = false;
	webRequest.ContentType = atvURL.ToLower().IndexOf(".json") >= 0  ? "application/json;charset=\"utf-8\"" : "text/xml;charset=\"utf-8\"";
	webRequest.Accept = "text/xml";
	webRequest.Headers.Add("SOAPAction", ActiveWSDLRequest);
	webRequest.Method = !isPOST ? "GET" : "POST";
	return webRequest;
}

biryazilim avatar Mar 23 '21 13:03 biryazilim