Skip to content

Commit ef6eba9

Browse files
authored
Merge pull request #241 from Microsoft/dm/perf
Performance enhancements to validation and reference resolution
2 parents f324f3a + b4a290c commit ef6eba9

25 files changed

+542
-175
lines changed

Microsoft.OpenApi.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{E546B92F-20A
2323
EndProject
2424
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{6357D7FD-2DE4-4900-ADB9-ABC37052040A}"
2525
EndProject
26-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.OpenApi.SmokeTests", "test\Microsoft.OpenApi.SmokeTests\Microsoft.OpenApi.SmokeTests.csproj", "{AD79B61D-88CF-497C-9ED5-41AE3867C5AC}"
26+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.OpenApi.SmokeTests", "test\Microsoft.OpenApi.SmokeTests\Microsoft.OpenApi.SmokeTests.csproj", "{AD79B61D-88CF-497C-9ED5-41AE3867C5AC}"
2727
EndProject
2828
Global
2929
GlobalSection(SolutionConfigurationPlatforms) = preSolution

src/Microsoft.OpenApi.Readers/OpenApiReaderSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class OpenApiReaderSettings
4747
/// <summary>
4848
/// Rules to use for validating OpenAPI specification. If none are provided a default set of rules are applied.
4949
/// </summary>
50-
public ValidationRuleSet RuleSet { get; set; }
50+
public ValidationRuleSet RuleSet { get; set; } = ValidationRuleSet.GetDefaultRuleSet();
5151

5252
}
5353
}

src/Microsoft.OpenApi.Readers/OpenApiStreamReader.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,14 @@ public OpenApiDocument Read(Stream input, out OpenApiDiagnostic diagnostic)
9090
}
9191

9292
// Validate the document
93-
var errors = document.Validate(_settings.RuleSet);
94-
foreach (var item in errors)
93+
if (_settings.RuleSet != null && _settings.RuleSet.Rules.Count > 0)
9594
{
96-
diagnostic.Errors.Add(item);
97-
}
95+
var errors = document.Validate(_settings.RuleSet);
96+
foreach (var item in errors)
97+
{
98+
diagnostic.Errors.Add(item);
99+
}
100+
}
98101

99102
return document;
100103
}

src/Microsoft.OpenApi.Readers/Services/OpenApiReferenceResolver.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ public override void Visit(OpenApiSecurityRequirement securityRequirement)
106106
{
107107
foreach (var scheme in securityRequirement.Keys.ToList())
108108
{
109-
ResolveObject(scheme, (resolvedScheme) => {
109+
ResolveObject(scheme, (resolvedScheme) =>
110+
{
110111
// If scheme was unresolved
111112
// copy Scopes and remove old unresolved scheme
112113
var scopes = securityRequirement[scheme];

src/Microsoft.OpenApi.Workbench/MainModel.cs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
using Microsoft.OpenApi.Extensions;
1010
using Microsoft.OpenApi.Models;
1111
using Microsoft.OpenApi.Readers;
12+
using Microsoft.OpenApi.Services;
13+
using Microsoft.OpenApi.Validations;
1214

1315
namespace Microsoft.OpenApi.Workbench
1416
{
@@ -19,6 +21,8 @@ public class MainModel : INotifyPropertyChanged
1921
{
2022
private string _input;
2123

24+
private string _inputFile;
25+
2226
private string _output;
2327

2428
private string _errors;
@@ -47,6 +51,16 @@ public string Input
4751
}
4852
}
4953

54+
public string InputFile
55+
{
56+
get => _inputFile;
57+
set
58+
{
59+
_inputFile = value;
60+
OnPropertyChanged(nameof(InputFile));
61+
}
62+
}
63+
5064
public string Output
5165
{
5266
get => _output;
@@ -149,16 +163,30 @@ protected void OnPropertyChanged(string propertyName)
149163
/// The core method of the class.
150164
/// Runs the parsing and serializing.
151165
/// </summary>
152-
internal void Validate()
166+
internal void ParseDocument()
153167
{
154168
try
155169
{
156-
var stream = CreateStream(_input);
170+
Stream stream;
171+
if (!String.IsNullOrWhiteSpace(_inputFile))
172+
{
173+
stream = new FileStream(_inputFile, FileMode.Open);
174+
}
175+
else
176+
{
177+
stream = CreateStream(_input);
178+
}
179+
157180

158181
var stopwatch = new Stopwatch();
159182
stopwatch.Start();
160183

161-
var document = new OpenApiStreamReader().Read(stream, out var context);
184+
var document = new OpenApiStreamReader(new OpenApiReaderSettings
185+
{
186+
ReferenceResolution = ReferenceResolutionSetting.ResolveLocalReferences,
187+
RuleSet = ValidationRuleSet.GetDefaultRuleSet()
188+
}
189+
).Read(stream, out var context);
162190
stopwatch.Stop();
163191
ParseTime = $"{stopwatch.ElapsedMilliseconds} ms";
164192

@@ -184,6 +212,12 @@ internal void Validate()
184212
stopwatch.Stop();
185213

186214
RenderTime = $"{stopwatch.ElapsedMilliseconds} ms";
215+
216+
var statsVisitor = new StatsVisitor();
217+
var walker = new OpenApiWalker(statsVisitor);
218+
walker.Walk(document);
219+
220+
Errors += Environment.NewLine + "Statistics:" + Environment.NewLine + statsVisitor.GetStatisticsReport();
187221
}
188222
catch (Exception ex)
189223
{

src/Microsoft.OpenApi.Workbench/MainWindow.xaml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,24 @@
1111
<ColumnDefinition/>
1212
<ColumnDefinition />
1313
</Grid.ColumnDefinitions>
14-
<TextBox x:Name="txtInput" Text="{Binding Input}" VerticalScrollBarVisibility="Auto" Margin="10" TextWrapping="Wrap" AcceptsReturn="True" FontFamily="Consolas" />
15-
<DockPanel Grid.Column="1" Margin="0,10" >
14+
<DockPanel Grid.Column="0" >
15+
<StackPanel DockPanel.Dock="Top">
16+
<Label>Input File:</Label>
17+
<TextBox x:Name="txtInputFile"
18+
Text="{Binding InputFile}"
19+
Margin="10"
20+
TextWrapping="Wrap" AcceptsReturn="false" FontFamily="Consolas" />
21+
<Label>Input Content:</Label>
22+
</StackPanel>
23+
<TextBox x:Name="txtInput" Text="{Binding Input}"
24+
VerticalScrollBarVisibility="Auto"
25+
Margin="10"
26+
TextWrapping="Wrap"
27+
AcceptsReturn="True"
28+
FontFamily="Consolas"
29+
VerticalContentAlignment="Stretch" />
30+
</DockPanel>
31+
<DockPanel Grid.Column="1" Margin="0,10" >
1632
<StackPanel Orientation="Horizontal" DockPanel.Dock="Top">
1733
<Button Content="Convert" VerticalAlignment="Center" HorizontalAlignment="Left" Height="20" Margin="10" Width="131" Click="Button_Click" Grid.Column="1"/>
1834
<StackPanel Orientation="Vertical" DockPanel.Dock="Top">

src/Microsoft.OpenApi.Workbench/MainWindow.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public MainWindow()
2020

2121
private void Button_Click(object sender, RoutedEventArgs e)
2222
{
23-
_mainModel.Validate();
23+
_mainModel.ParseDocument();
2424
}
2525
}
2626
}

0 commit comments

Comments
 (0)