Skip to content

Commit 89cc609

Browse files
committed
Fixed inlining settings
1 parent 45968f3 commit 89cc609

File tree

20 files changed

+229
-48
lines changed

20 files changed

+229
-48
lines changed

src/Microsoft.OpenApi.Readers/OpenApiStreamReader.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4+
using System;
45
using System.IO;
56
using System.Threading.Tasks;
67
using Microsoft.OpenApi.Interfaces;
@@ -23,6 +24,12 @@ public class OpenApiStreamReader : IOpenApiReader<Stream, OpenApiDiagnostic>
2324
public OpenApiStreamReader(OpenApiReaderSettings settings = null)
2425
{
2526
_settings = settings ?? new OpenApiReaderSettings();
27+
28+
if(_settings.ReferenceResolution == ReferenceResolutionSetting.ResolveAllReferences
29+
&& _settings.BaseUrl == null)
30+
{
31+
throw new ArgumentException("BaseUrl must be provided to resolve external references.");
32+
}
2633
}
2734

2835
/// <summary>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Thanks Brian! https://brianlagunas.com/a-better-way-to-data-bind-enums-in-wpf/
2+
using System;
3+
using System.Windows.Markup;
4+
5+
namespace Microsoft.OpenApi.Workbench
6+
{
7+
public class EnumBindingSourceExtension : MarkupExtension
8+
{
9+
private Type _enumType;
10+
public Type EnumType
11+
{
12+
get { return this._enumType; }
13+
set
14+
{
15+
if (value != this._enumType)
16+
{
17+
if (null != value)
18+
{
19+
Type enumType = Nullable.GetUnderlyingType(value) ?? value;
20+
if (!enumType.IsEnum)
21+
throw new ArgumentException("Type must be for an Enum.");
22+
}
23+
24+
this._enumType = value;
25+
}
26+
}
27+
}
28+
29+
public EnumBindingSourceExtension() { }
30+
31+
public EnumBindingSourceExtension(Type enumType)
32+
{
33+
this.EnumType = enumType;
34+
}
35+
36+
public override object ProvideValue(IServiceProvider serviceProvider)
37+
{
38+
if (null == this._enumType)
39+
throw new InvalidOperationException("The EnumType must be specified.");
40+
41+
Type actualEnumType = Nullable.GetUnderlyingType(this._enumType) ?? this._enumType;
42+
Array enumValues = Enum.GetValues(actualEnumType);
43+
44+
if (actualEnumType == this._enumType)
45+
return enumValues;
46+
47+
Array tempArray = Array.CreateInstance(actualEnumType, enumValues.Length + 1);
48+
enumValues.CopyTo(tempArray, 1);
49+
return tempArray;
50+
}
51+
}
52+
53+
}

src/Microsoft.OpenApi.Workbench/MainModel.cs

Lines changed: 79 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

44
using System;
55
using System.ComponentModel;
66
using System.Diagnostics;
77
using System.Globalization;
88
using System.IO;
9+
using System.Net.Http;
910
using System.Text;
11+
using System.Threading.Tasks;
1012
using Microsoft.OpenApi.Extensions;
1113
using Microsoft.OpenApi.Models;
1214
using Microsoft.OpenApi.Readers;
@@ -23,6 +25,11 @@ public class MainModel : INotifyPropertyChanged
2325
{
2426
private string _input;
2527

28+
private bool _inlineLocal = false;
29+
private bool _inlineExternal = false;
30+
31+
private bool _resolveExternal = false;
32+
2633
private string _inputFile;
2734

2835
private string _output;
@@ -33,11 +40,7 @@ public class MainModel : INotifyPropertyChanged
3340

3441
private string _renderTime;
3542

36-
/// <summary>
37-
/// Default format.
38-
/// </summary>
39-
private bool _Inline = false;
40-
43+
4144
/// <summary>
4245
/// Default format.
4346
/// </summary>
@@ -48,6 +51,9 @@ public class MainModel : INotifyPropertyChanged
4851
/// </summary>
4952
private OpenApiSpecVersion _version = OpenApiSpecVersion.OpenApi3_0;
5053

54+
55+
private HttpClient _httpClient = new HttpClient();
56+
5157
public string Input
5258
{
5359
get => _input;
@@ -58,6 +64,16 @@ public string Input
5864
}
5965
}
6066

67+
public bool ResolveExternal
68+
{
69+
get => _resolveExternal;
70+
set
71+
{
72+
_resolveExternal = value;
73+
OnPropertyChanged(nameof(ResolveExternal));
74+
}
75+
}
76+
6177
public string InputFile
6278
{
6379
get => _inputFile;
@@ -67,7 +83,6 @@ public string InputFile
6783
OnPropertyChanged(nameof(InputFile));
6884
}
6985
}
70-
7186
public string Output
7287
{
7388
get => _output;
@@ -119,13 +134,23 @@ public OpenApiFormat Format
119134
}
120135
}
121136

122-
public bool Inline
137+
public bool InlineLocal
138+
{
139+
get => _inlineLocal;
140+
set
141+
{
142+
_inlineLocal = value;
143+
OnPropertyChanged(nameof(InlineLocal));
144+
}
145+
}
146+
147+
public bool InlineExternal
123148
{
124-
get => _Inline;
149+
get => _inlineExternal;
125150
set
126151
{
127-
_Inline = value;
128-
OnPropertyChanged(nameof(Inline));
152+
_inlineExternal = value;
153+
OnPropertyChanged(nameof(InlineExternal));
129154
}
130155
}
131156

@@ -180,30 +205,56 @@ protected void OnPropertyChanged(string propertyName)
180205
/// The core method of the class.
181206
/// Runs the parsing and serializing.
182207
/// </summary>
183-
internal void ParseDocument()
208+
internal async Task ParseDocument()
184209
{
210+
Stream stream = null;
185211
try
186212
{
187-
Stream stream;
188-
if (!String.IsNullOrWhiteSpace(_inputFile))
213+
if (!string.IsNullOrWhiteSpace(_inputFile))
189214
{
190-
stream = new FileStream(_inputFile, FileMode.Open);
215+
if (_inputFile.StartsWith("http"))
216+
{
217+
stream = await _httpClient.GetStreamAsync(_inputFile);
218+
}
219+
else
220+
{
221+
stream = new FileStream(_inputFile, FileMode.Open);
222+
}
191223
}
192224
else
193225
{
226+
if (ResolveExternal)
227+
{
228+
throw new ArgumentException("Input file must be used to resolve external references");
229+
}
194230
stream = CreateStream(_input);
195231
}
196232

197233

198234
var stopwatch = new Stopwatch();
199235
stopwatch.Start();
200236

201-
var document = new OpenApiStreamReader(new OpenApiReaderSettings
237+
var settings = new OpenApiReaderSettings
202238
{
203-
ReferenceResolution = ReferenceResolutionSetting.ResolveLocalReferences,
239+
ReferenceResolution = ResolveExternal ? ReferenceResolutionSetting.ResolveAllReferences : ReferenceResolutionSetting.ResolveLocalReferences,
204240
RuleSet = ValidationRuleSet.GetDefaultRuleSet()
241+
};
242+
if (ResolveExternal)
243+
{
244+
if (_inputFile.StartsWith("http"))
245+
{
246+
settings.BaseUrl = new Uri(_inputFile);
247+
}
248+
else
249+
{
250+
settings.BaseUrl = new Uri("file://" + Path.GetDirectoryName(_inputFile) + "/");
251+
}
205252
}
206-
).Read(stream, out var context);
253+
var readResult = await new OpenApiStreamReader(settings
254+
).ReadAsync(stream);
255+
var document = readResult.OpenApiDocument;
256+
var context = readResult.OpenApiDiagnostic;
257+
207258
stopwatch.Stop();
208259
ParseTime = $"{stopwatch.ElapsedMilliseconds} ms";
209260

@@ -241,6 +292,14 @@ internal void ParseDocument()
241292
Output = string.Empty;
242293
Errors = "Failed to parse input: " + ex.Message;
243294
}
295+
finally {
296+
if (stream != null)
297+
{
298+
stream.Close();
299+
stream.Dispose();
300+
}
301+
302+
}
244303
}
245304

246305
/// <summary>
@@ -255,7 +314,8 @@ private string WriteContents(OpenApiDocument document)
255314
Version,
256315
Format,
257316
new OpenApiWriterSettings() {
258-
ReferenceInline = this.Inline == true ? ReferenceInlineSetting.InlineLocalReferences : ReferenceInlineSetting.DoNotInlineReferences
317+
InlineLocalReferences = InlineLocal,
318+
InlineExternalReferences = InlineExternal
259319
});
260320

261321
outputStream.Position = 0;

src/Microsoft.OpenApi.Workbench/MainWindow.xaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
55
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
66
xmlns:local="clr-namespace:Microsoft.OpenApi.Workbench"
7+
xmlns:writers="clr-namespace:Microsoft.OpenApi.Writers;assembly=Microsoft.OpenApi"
78
mc:Ignorable="d"
89
Title="OpenAPI Workbench" Height="600" Width="800">
910
<Grid>
@@ -42,7 +43,9 @@
4243
<RadioButton GroupName="Format" Content="V3.0.1" Padding="5" Height="24" VerticalAlignment="Top" IsChecked="{Binding IsV3_0}" />
4344
<RadioButton GroupName="Format" Content="V2.0" Padding="5" Height="24" VerticalAlignment="Top" IsChecked="{Binding IsV2_0}" />
4445
</StackPanel>
45-
<CheckBox Name="Inline" Content="Inline" IsChecked="{Binding Inline}" />
46+
<CheckBox Name="InlineLocal" Content="Inline Local" IsChecked="{Binding InlineLocal}" />
47+
<CheckBox Name="ResolveExternal" Content="Resolve External" IsChecked="{Binding ResolveExternal}" />
48+
<CheckBox Name="InlineExternal" Content="Inline External" IsChecked="{Binding InlineExternal}" />
4649
</StackPanel>
4750
</StackPanel>
4851
<TextBox x:Name="txtErrors" Margin="10,10,10,10" TextWrapping="Wrap" Text="{Binding Errors}" Background="{DynamicResource {x:Static SystemColors.ControlLightBrushKey}}" DockPanel.Dock="Top" MaxHeight="100" ScrollViewer.VerticalScrollBarVisibility="Auto" IsReadOnlyCaretVisible="True" IsManipulationEnabled="True" />

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4+
using System;
45
using System.Windows;
56

67
namespace Microsoft.OpenApi.Workbench
@@ -18,9 +19,15 @@ public MainWindow()
1819
DataContext = _mainModel;
1920
}
2021

21-
private void Button_Click(object sender, RoutedEventArgs e)
22+
private async void Button_Click(object sender, RoutedEventArgs e)
2223
{
23-
_mainModel.ParseDocument();
24+
try
25+
{
26+
await _mainModel.ParseDocument();
27+
} catch (Exception ex)
28+
{
29+
_mainModel.Errors = ex.Message;
30+
}
2431
}
2532
}
2633
}

src/Microsoft.OpenApi.Workbench/Microsoft.OpenApi.Workbench.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
<Generator>MSBuild:Compile</Generator>
5959
<SubType>Designer</SubType>
6060
</ApplicationDefinition>
61+
<Compile Include="EnumBindingSourceExtension.cs" />
6162
<Compile Include="StatsVisitor.cs" />
6263
<Page Include="MainWindow.xaml">
6364
<Generator>MSBuild:Compile</Generator>

src/Microsoft.OpenApi/Models/OpenApiCallback.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void SerializeAsV3(IOpenApiWriter writer)
7070
throw Error.ArgumentNull(nameof(writer));
7171
}
7272

73-
if (Reference != null && writer.GetSettings().ReferenceInline != ReferenceInlineSetting.InlineLocalReferences)
73+
if (Reference != null && !writer.GetSettings().ShouldInlineReference(Reference))
7474
{
7575
Reference.SerializeAsV3(writer);
7676
return;

src/Microsoft.OpenApi/Models/OpenApiComponents.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public void SerializeAsV3(IOpenApiWriter writer)
8080

8181
// If references have been inlined we don't need the to render the components section
8282
// however if they have cycles, then we will need a component rendered
83-
if (writer.GetSettings().ReferenceInline != ReferenceInlineSetting.DoNotInlineReferences)
83+
if (writer.GetSettings().InlineLocalReferences)
8484
{
8585
var loops = writer.GetSettings().LoopDetector.Loops;
8686
writer.WriteStartObject();

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public void SerializeAsV2(IOpenApiWriter writer)
135135

136136
// If references have been inlined we don't need the to render the components section
137137
// however if they have cycles, then we will need a component rendered
138-
if (writer.GetSettings().ReferenceInline != ReferenceInlineSetting.DoNotInlineReferences)
138+
if (writer.GetSettings().InlineLocalReferences)
139139
{
140140
var loops = writer.GetSettings().LoopDetector.Loops;
141141

src/Microsoft.OpenApi/Models/OpenApiExample.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void SerializeAsV3(IOpenApiWriter writer)
6464
throw Error.ArgumentNull(nameof(writer));
6565
}
6666

67-
if (Reference != null && writer.GetSettings().ReferenceInline != ReferenceInlineSetting.InlineLocalReferences)
67+
if (Reference != null && !writer.GetSettings().ShouldInlineReference(Reference))
6868
{
6969
Reference.SerializeAsV3(writer);
7070
return;

0 commit comments

Comments
 (0)