1
1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
2
// Licensed under the MIT license.
3
3
4
- using System ;
5
4
using System . IO ;
6
- using System . Linq ;
7
- using Microsoft . OpenApi . Exceptions ;
8
- using Microsoft . OpenApi . Extensions ;
9
5
using Microsoft . OpenApi . Interfaces ;
10
6
using Microsoft . OpenApi . Models ;
11
7
using Microsoft . OpenApi . Readers . Interface ;
12
- using Microsoft . OpenApi . Readers . Services ;
13
- using Microsoft . OpenApi . Services ;
14
- using SharpYaml ;
15
- using SharpYaml . Serialization ;
16
8
17
9
namespace Microsoft . OpenApi . Readers
18
10
{
@@ -21,7 +13,7 @@ namespace Microsoft.OpenApi.Readers
21
13
/// </summary>
22
14
public class OpenApiStreamReader : IOpenApiReader < Stream , OpenApiDiagnostic >
23
15
{
24
- private OpenApiReaderSettings _settings ;
16
+ private readonly OpenApiReaderSettings _settings ;
25
17
26
18
/// <summary>
27
19
/// Create stream reader with custom settings if desired.
@@ -30,8 +22,8 @@ public class OpenApiStreamReader : IOpenApiReader<Stream, OpenApiDiagnostic>
30
22
public OpenApiStreamReader ( OpenApiReaderSettings settings = null )
31
23
{
32
24
_settings = settings ?? new OpenApiReaderSettings ( ) ;
33
-
34
25
}
26
+
35
27
/// <summary>
36
28
/// Reads the stream input and parses it into an Open API document.
37
29
/// </summary>
@@ -40,68 +32,10 @@ public OpenApiStreamReader(OpenApiReaderSettings settings = null)
40
32
/// <returns>Instance of newly created OpenApiDocument</returns>
41
33
public OpenApiDocument Read ( Stream input , out OpenApiDiagnostic diagnostic )
42
34
{
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 ) )
96
36
{
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 ) ;
102
38
}
103
-
104
- return document ;
105
39
}
106
40
107
41
/// <summary>
@@ -113,66 +47,10 @@ public OpenApiDocument Read(Stream input, out OpenApiDiagnostic diagnostic)
113
47
/// <returns>Instance of newly created OpenApiDocument</returns>
114
48
public T ReadFragment < T > ( Stream input , OpenApiSpecVersion version , out OpenApiDiagnostic diagnostic ) where T : IOpenApiElement
115
49
{
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 ) )
170
51
{
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 ) ;
174
53
}
175
- return yamlDocument ;
176
54
}
177
55
}
178
56
}
0 commit comments