-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathMainModel.cs
338 lines (289 loc) · 9.56 KB
/
MainModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Reader;
using Microsoft.OpenApi.Readers;
using Microsoft.OpenApi.Services;
using Microsoft.OpenApi.Validations;
namespace Microsoft.OpenApi.Workbench
{
/// <summary>
/// Main model of the Workbench tool.
/// </summary>
public class MainModel : INotifyPropertyChanged
{
private string _input;
private bool _inlineLocal;
private bool _inlineExternal;
private bool _resolveExternal;
private string _inputFile;
private string _output;
private string _errors;
private string _parseTime;
private string _renderTime;
/// <summary>
/// Default format.
/// </summary>
private OpenApiFormat _format = OpenApiFormat.Yaml;
/// <summary>
/// Default version.
/// </summary>
private OpenApiSpecVersion _version = OpenApiSpecVersion.OpenApi3_0;
private static readonly HttpClient _httpClient = new();
public string Input
{
get => _input;
set
{
_input = value;
OnPropertyChanged(nameof(Input));
}
}
public bool ResolveExternal
{
get => _resolveExternal;
set
{
_resolveExternal = value;
OnPropertyChanged(nameof(ResolveExternal));
}
}
public string InputFile
{
get => _inputFile;
set
{
_inputFile = value;
OnPropertyChanged(nameof(InputFile));
}
}
public string Output
{
get => _output;
set
{
_output = value;
OnPropertyChanged(nameof(Output));
}
}
public string Errors
{
get => _errors;
set
{
_errors = value;
OnPropertyChanged(nameof(Errors));
}
}
public string ParseTime
{
get => _parseTime;
set
{
_parseTime = value;
OnPropertyChanged(nameof(ParseTime));
}
}
public string RenderTime
{
get => _renderTime;
set
{
_renderTime = value;
OnPropertyChanged(nameof(RenderTime));
}
}
public OpenApiFormat Format
{
get => _format;
set
{
_format = value;
OnPropertyChanged(nameof(IsYaml));
OnPropertyChanged(nameof(IsJson));
}
}
public bool InlineLocal
{
get => _inlineLocal;
set
{
_inlineLocal = value;
OnPropertyChanged(nameof(InlineLocal));
}
}
public bool InlineExternal
{
get => _inlineExternal;
set
{
_inlineExternal = value;
OnPropertyChanged(nameof(InlineExternal));
}
}
public OpenApiSpecVersion Version
{
get => _version;
set
{
_version = value;
OnPropertyChanged(nameof(IsV2_0));
OnPropertyChanged(nameof(IsV3_0));
OnPropertyChanged(nameof(IsV3_1));
}
}
public bool IsYaml
{
get => Format == OpenApiFormat.Yaml;
set => Format = value ? OpenApiFormat.Yaml : Format;
}
public bool IsJson
{
get => Format == OpenApiFormat.Json;
set => Format = value ? OpenApiFormat.Json : Format;
}
public bool IsV2_0
{
get => Version == OpenApiSpecVersion.OpenApi2_0;
set => Version = value ? OpenApiSpecVersion.OpenApi2_0 : Version;
}
public bool IsV3_0
{
get => Version == OpenApiSpecVersion.OpenApi3_0;
set => Version = value ? OpenApiSpecVersion.OpenApi3_0 : Version;
}
public bool IsV3_1
{
get => Version == OpenApiSpecVersion.OpenApi3_1;
set => Version = value ? OpenApiSpecVersion.OpenApi3_1 : Version;
}
/// <summary>
/// Handling method when the property with given name has changed.
/// </summary>
protected void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new(propertyName));
}
}
/// <summary>
/// The core method of the class.
/// Runs the parsing and serializing.
/// </summary>
internal async Task ParseDocumentAsync()
{
OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader());
OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yml, new OpenApiYamlReader());
Stream stream = null;
try
{
if (!string.IsNullOrWhiteSpace(_inputFile))
{
stream = _inputFile.StartsWith("http", StringComparison.OrdinalIgnoreCase) ? await _httpClient.GetStreamAsync(_inputFile)
: new FileStream(_inputFile, FileMode.Open);
}
else
{
if (ResolveExternal)
{
throw new ArgumentException("Input file must be used to resolve external references");
}
stream = CreateStream(_input);
}
var stopwatch = new Stopwatch();
stopwatch.Start();
var settings = new OpenApiReaderSettings
{
ReferenceResolution = ResolveExternal ? ReferenceResolutionSetting.ResolveAllReferences : ReferenceResolutionSetting.ResolveLocalReferences,
RuleSet = ValidationRuleSet.GetDefaultRuleSet()
};
if (ResolveExternal && !string.IsNullOrWhiteSpace(_inputFile))
{
settings.BaseUrl = _inputFile.StartsWith("http", StringComparison.OrdinalIgnoreCase) ? new(_inputFile)
: new("file://" + Path.GetDirectoryName(_inputFile) + "/");
}
var readResult = await OpenApiDocument.LoadAsync(stream, Format.GetDisplayName());
var document = readResult.Document;
var context = readResult.Diagnostic;
stopwatch.Stop();
ParseTime = $"{stopwatch.ElapsedMilliseconds} ms";
if (context.Errors.Count == 0)
{
Errors = "OK";
}
else
{
var errorReport = new StringBuilder();
foreach (var error in context.Errors)
{
errorReport.AppendLine(error.ToString());
}
Errors = errorReport.ToString();
}
stopwatch.Reset();
stopwatch.Start();
Output = await WriteContentsAsync(document);
stopwatch.Stop();
RenderTime = $"{stopwatch.ElapsedMilliseconds} ms";
var statsVisitor = new StatsVisitor();
var walker = new OpenApiWalker(statsVisitor);
walker.Walk(document);
Errors += Environment.NewLine + "Statistics:" + Environment.NewLine + statsVisitor.GetStatisticsReport();
}
catch (Exception ex)
{
Output = string.Empty;
Errors = "Failed to parse input: " + ex.Message;
}
finally
{
if (stream != null)
{
stream.Close();
await stream.DisposeAsync();
}
}
}
/// <summary>
/// Write content from the given document based on the format and version set in this class.
/// </summary>
private async Task<string> WriteContentsAsync(OpenApiDocument document)
{
var outputStream = new MemoryStream();
await document.SerializeAsync(
outputStream,
Version,
Format,
(Writers.OpenApiWriterSettings)new()
{
InlineLocalReferences = InlineLocal,
InlineExternalReferences = InlineExternal
});
outputStream.Position = 0;
return await new StreamReader(outputStream).ReadToEndAsync();
}
private static MemoryStream CreateStream(string text)
{
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(text);
writer.Flush();
stream.Position = 0;
return stream;
}
/// <summary>
/// Property changed event handler.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
}
}