11// Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT license.
33
4- using System ;
54using System . IO ;
6- using System . Linq ;
7- using Microsoft . OpenApi . Exceptions ;
8- using Microsoft . OpenApi . Extensions ;
95using Microsoft . OpenApi . Interfaces ;
106using Microsoft . OpenApi . Models ;
117using Microsoft . OpenApi . Readers . Interface ;
12- using Microsoft . OpenApi . Readers . Services ;
13- using Microsoft . OpenApi . Services ;
14- using SharpYaml ;
15- using SharpYaml . Serialization ;
168
179namespace Microsoft . OpenApi . Readers
1810{
@@ -21,7 +13,7 @@ namespace Microsoft.OpenApi.Readers
2113 /// </summary>
2214 public class OpenApiStreamReader : IOpenApiReader < Stream , OpenApiDiagnostic >
2315 {
24- private OpenApiReaderSettings _settings ;
16+ private readonly OpenApiReaderSettings _settings ;
2517
2618 /// <summary>
2719 /// Create stream reader with custom settings if desired.
@@ -30,8 +22,8 @@ public class OpenApiStreamReader : IOpenApiReader<Stream, OpenApiDiagnostic>
3022 public OpenApiStreamReader ( OpenApiReaderSettings settings = null )
3123 {
3224 _settings = settings ?? new OpenApiReaderSettings ( ) ;
33-
3425 }
26+
3527 /// <summary>
3628 /// Reads the stream input and parses it into an Open API document.
3729 /// </summary>
@@ -40,68 +32,10 @@ public OpenApiStreamReader(OpenApiReaderSettings settings = null)
4032 /// <returns>Instance of newly created OpenApiDocument</returns>
4133 public OpenApiDocument Read ( Stream input , out OpenApiDiagnostic diagnostic )
4234 {
43- ParsingContext context ;
44- YamlDocument yamlDocument ;
45- diagnostic = new OpenApiDiagnostic ( ) ;
46-
47- // Parse the YAML/JSON
48- try
49- {
50- yamlDocument = LoadYamlDocument ( input ) ;
51- }
52- catch ( YamlException ex )
53- {
54- diagnostic . Errors . Add ( new OpenApiError ( $ "#char={ ex . Start . Line } ", ex . Message ) ) ;
55- return new OpenApiDocument ( ) ;
56- }
57-
58- context = new ParsingContext ( diagnostic )
59- {
60- ExtensionParsers = _settings . ExtensionParsers ,
61- BaseUrl = _settings . BaseUrl
62- } ;
63-
64- OpenApiDocument document = null ;
65-
66- try
67- {
68- // Parse the OpenAPI Document
69- document = context . Parse ( yamlDocument ) ;
70-
71- // Resolve References if requested
72- switch ( _settings . ReferenceResolution )
73- {
74- case ReferenceResolutionSetting . ResolveAllReferences :
75- throw new ArgumentException ( Properties . SRResource . CannotResolveRemoteReferencesSynchronously ) ;
76- case ReferenceResolutionSetting . ResolveLocalReferences :
77- var resolver = new OpenApiReferenceResolver ( document ) ;
78- var walker = new OpenApiWalker ( resolver ) ;
79- walker . Walk ( document ) ;
80- foreach ( var item in resolver . Errors )
81- {
82- diagnostic . Errors . Add ( item ) ;
83- }
84- break ;
85- case ReferenceResolutionSetting . DoNotResolveReferences :
86- break ;
87- }
88- }
89- catch ( OpenApiException ex )
90- {
91- diagnostic . Errors . Add ( new OpenApiError ( ex ) ) ;
92- }
93-
94- // Validate the document
95- if ( _settings . RuleSet != null && _settings . RuleSet . Rules . Count > 0 )
35+ using ( var reader = new StreamReader ( input ) )
9636 {
97- var errors = document . Validate ( _settings . RuleSet ) ;
98- foreach ( var item in errors )
99- {
100- diagnostic . Errors . Add ( item ) ;
101- }
37+ return new OpenApiTextReaderReader ( _settings ) . Read ( reader , out diagnostic ) ;
10238 }
103-
104- return document ;
10539 }
10640
10741 /// <summary>
@@ -113,66 +47,10 @@ public OpenApiDocument Read(Stream input, out OpenApiDiagnostic diagnostic)
11347 /// <returns>Instance of newly created OpenApiDocument</returns>
11448 public T ReadFragment < T > ( Stream input , OpenApiSpecVersion version , out OpenApiDiagnostic diagnostic ) where T : IOpenApiElement
11549 {
116- ParsingContext context ;
117- YamlDocument yamlDocument ;
118- diagnostic = new OpenApiDiagnostic ( ) ;
119-
120- // Parse the YAML/JSON
121- try
122- {
123- yamlDocument = LoadYamlDocument ( input ) ;
124- }
125- catch ( YamlException ex )
126- {
127- diagnostic . Errors . Add ( new OpenApiError ( $ "#line={ ex . Start . Line } ", ex . Message ) ) ;
128- return default ( T ) ;
129- }
130-
131- context = new ParsingContext ( diagnostic )
132- {
133- ExtensionParsers = _settings . ExtensionParsers
134- } ;
135-
136- IOpenApiElement element = null ;
137-
138- try
139- {
140- // Parse the OpenAPI element
141- element = context . ParseFragment < T > ( yamlDocument , version ) ;
142- }
143- catch ( OpenApiException ex )
144- {
145- diagnostic . Errors . Add ( new OpenApiError ( ex ) ) ;
146- }
147-
148- // Validate the element
149- if ( _settings . RuleSet != null && _settings . RuleSet . Rules . Count > 0 )
150- {
151- var errors = element . Validate ( _settings . RuleSet ) ;
152- foreach ( var item in errors )
153- {
154- diagnostic . Errors . Add ( item ) ;
155- }
156- }
157-
158- return ( T ) element ;
159- }
160-
161- /// <summary>
162- /// Helper method to turn streams into YamlDocument
163- /// </summary>
164- /// <param name="input">Stream containing YAML formatted text</param>
165- /// <returns>Instance of a YamlDocument</returns>
166- internal static YamlDocument LoadYamlDocument ( Stream input )
167- {
168- YamlDocument yamlDocument ;
169- using ( var streamReader = new StreamReader ( input ) )
50+ using ( var reader = new StreamReader ( input ) )
17051 {
171- var yamlStream = new YamlStream ( ) ;
172- yamlStream . Load ( streamReader ) ;
173- yamlDocument = yamlStream . Documents . First ( ) ;
52+ return new OpenApiTextReaderReader ( _settings ) . ReadFragment < T > ( reader , version , out diagnostic ) ;
17453 }
175- return yamlDocument ;
17654 }
17755 }
17856}
0 commit comments