@@ -24,14 +24,47 @@ namespace Microsoft.OpenApi.Reader
2424 /// </summary>
2525 public class OpenApiJsonReader : IOpenApiReader
2626 {
27+
2728 /// <summary>
28- /// Reads the stream input and parses it into an Open API document.
29+ /// Reads the memory stream input and parses it into an Open API document.
30+ /// </summary>
31+ /// <param name="input">TextReader containing OpenAPI description to parse.</param>
32+ /// <param name="settings">The Reader settings to be used during parsing.</param>
33+ /// <returns></returns>
34+ public ReadResult Read ( MemoryStream input ,
35+ OpenApiReaderSettings settings = null )
36+ {
37+ JsonNode jsonNode ;
38+ var diagnostic = new OpenApiDiagnostic ( ) ;
39+ settings ??= new OpenApiReaderSettings ( ) ;
40+
41+ // Parse the JSON text in the TextReader into JsonNodes
42+ try
43+ {
44+ jsonNode = JsonNode . Parse ( input ) ;
45+ }
46+ catch ( JsonException ex )
47+ {
48+ diagnostic . Errors . Add ( new OpenApiError ( $ "#line={ ex . LineNumber } ", $ "Please provide the correct format, { ex . Message } ") ) ;
49+ return new ReadResult
50+ {
51+ OpenApiDocument = null ,
52+ OpenApiDiagnostic = diagnostic
53+ } ;
54+ }
55+
56+ return Read ( jsonNode , settings ) ;
57+ }
58+
59+
60+ /// <summary>
61+ /// Reads the stream input asynchronously and parses it into an Open API document.
2962 /// </summary>
3063 /// <param name="input">TextReader containing OpenAPI description to parse.</param>
3164 /// <param name="settings">The Reader settings to be used during parsing.</param>
3265 /// <param name="cancellationToken">Propagates notifications that operations should be cancelled.</param>
3366 /// <returns></returns>
34- public async Task < ReadResult > ReadAsync ( TextReader input ,
67+ public async Task < ReadResult > ReadAsync ( Stream input ,
3568 OpenApiReaderSettings settings = null ,
3669 CancellationToken cancellationToken = default )
3770 {
@@ -42,7 +75,7 @@ public async Task<ReadResult> ReadAsync(TextReader input,
4275 // Parse the JSON text in the TextReader into JsonNodes
4376 try
4477 {
45- jsonNode = LoadJsonNodes ( input ) ;
78+ jsonNode = await JsonNode . ParseAsync ( input ) ; ;
4679 }
4780 catch ( JsonException ex )
4881 {
@@ -54,7 +87,7 @@ public async Task<ReadResult> ReadAsync(TextReader input,
5487 } ;
5588 }
5689
57- return await ReadAsync ( jsonNode , settings , cancellationToken : cancellationToken ) ;
90+ return Read ( jsonNode , settings ) ;
5891 }
5992
6093 /// <summary>
@@ -63,12 +96,10 @@ public async Task<ReadResult> ReadAsync(TextReader input,
6396 /// <param name="jsonNode">The JsonNode input.</param>
6497 /// <param name="settings">The Reader settings to be used during parsing.</param>
6598 /// <param name="format">The OpenAPI format.</param>
66- /// <param name="cancellationToken">Propagates notifications that operations should be cancelled.</param>
6799 /// <returns></returns>
68- public async Task < ReadResult > ReadAsync ( JsonNode jsonNode ,
100+ public ReadResult Read ( JsonNode jsonNode ,
69101 OpenApiReaderSettings settings ,
70- string format = null ,
71- CancellationToken cancellationToken = default )
102+ string format = null )
72103 {
73104 var diagnostic = new OpenApiDiagnostic ( ) ;
74105 var context = new ParsingContext ( diagnostic )
@@ -84,16 +115,16 @@ public async Task<ReadResult> ReadAsync(JsonNode jsonNode,
84115 // Parse the OpenAPI Document
85116 document = context . Parse ( jsonNode ) ;
86117
87- if ( settings . LoadExternalRefs )
88- {
89- var diagnosticExternalRefs = await LoadExternalRefsAsync ( document , cancellationToken , settings , format ) ;
90- // Merge diagnostics of external reference
91- if ( diagnosticExternalRefs != null )
92- {
93- diagnostic . Errors . AddRange ( diagnosticExternalRefs . Errors ) ;
94- diagnostic . Warnings . AddRange ( diagnosticExternalRefs . Warnings ) ;
95- }
96- }
118+ // if (settings.LoadExternalRefs)
119+ // {
120+ // var diagnosticExternalRefs = await LoadExternalRefsAsync(document, cancellationToken, settings, format);
121+ // // Merge diagnostics of external reference
122+ // if (diagnosticExternalRefs != null)
123+ // {
124+ // diagnostic.Errors.AddRange(diagnosticExternalRefs.Errors);
125+ // diagnostic.Warnings.AddRange(diagnosticExternalRefs.Warnings);
126+ // }
127+ // }
97128
98129 document . SetReferenceHostDocument ( ) ;
99130 }
@@ -124,7 +155,7 @@ public async Task<ReadResult> ReadAsync(JsonNode jsonNode,
124155 }
125156
126157 /// <inheritdoc/>
127- public T ReadFragment < T > ( TextReader input ,
158+ public T ReadFragment < T > ( MemoryStream input ,
128159 OpenApiSpecVersion version ,
129160 out OpenApiDiagnostic diagnostic ,
130161 OpenApiReaderSettings settings = null ) where T : IOpenApiElement
@@ -134,7 +165,7 @@ public T ReadFragment<T>(TextReader input,
134165 // Parse the JSON
135166 try
136167 {
137- jsonNode = LoadJsonNodes ( input ) ;
168+ jsonNode = JsonNode . Parse ( input ) ;
138169 }
139170 catch ( JsonException ex )
140171 {
@@ -183,12 +214,6 @@ public T ReadFragment<T>(JsonNode input,
183214 return ( T ) element ;
184215 }
185216
186- private JsonNode LoadJsonNodes ( TextReader input )
187- {
188- var nodes = JsonNode . Parse ( input . ReadToEnd ( ) ) ;
189- return nodes ;
190- }
191-
192217 private async Task < OpenApiDiagnostic > LoadExternalRefsAsync ( OpenApiDocument document , CancellationToken cancellationToken , OpenApiReaderSettings settings , string format = null )
193218 {
194219 // Create workspace for all documents to live in.
0 commit comments