Skip to content

Commit 5ceb174

Browse files
Merge pull request #1956 from microsoft/mk/fix-workbench-format-bug
Fix: Workbench fails because of a missing format parameter
2 parents 1338905 + 506ef1f commit 5ceb174

File tree

3 files changed

+15
-27
lines changed

3 files changed

+15
-27
lines changed

src/Microsoft.OpenApi.Workbench/MainModel.cs

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class MainModel : INotifyPropertyChanged
4949
/// </summary>
5050
private OpenApiSpecVersion _version = OpenApiSpecVersion.OpenApi3_0;
5151

52-
private HttpClient _httpClient = new();
52+
private static readonly HttpClient _httpClient = new();
5353

5454
public string Input
5555
{
@@ -166,31 +166,31 @@ public OpenApiSpecVersion Version
166166
public bool IsYaml
167167
{
168168
get => Format == OpenApiFormat.Yaml;
169-
set => Format = OpenApiFormat.Yaml;
169+
set => Format = value ? OpenApiFormat.Yaml : Format;
170170
}
171171

172172
public bool IsJson
173173
{
174174
get => Format == OpenApiFormat.Json;
175-
set => Format = OpenApiFormat.Json;
175+
set => Format = value ? OpenApiFormat.Json : Format;
176176
}
177177

178178
public bool IsV2_0
179179
{
180180
get => Version == OpenApiSpecVersion.OpenApi2_0;
181-
set => Version = OpenApiSpecVersion.OpenApi2_0;
181+
set => Version = value ? OpenApiSpecVersion.OpenApi2_0 : Version;
182182
}
183183

184184
public bool IsV3_0
185185
{
186186
get => Version == OpenApiSpecVersion.OpenApi3_0;
187-
set => Version = OpenApiSpecVersion.OpenApi3_0;
187+
set => Version = value ? OpenApiSpecVersion.OpenApi3_0 : Version;
188188
}
189189

190190
public bool IsV3_1
191191
{
192192
get => Version == OpenApiSpecVersion.OpenApi3_1;
193-
set => Version = OpenApiSpecVersion.OpenApi3_1;
193+
set => Version = value ? OpenApiSpecVersion.OpenApi3_1 : Version;
194194
}
195195

196196
/// <summary>
@@ -219,14 +219,8 @@ internal async Task ParseDocumentAsync()
219219
{
220220
if (!string.IsNullOrWhiteSpace(_inputFile))
221221
{
222-
if (_inputFile.StartsWith("http"))
223-
{
224-
stream = await _httpClient.GetStreamAsync(_inputFile);
225-
}
226-
else
227-
{
228-
stream = new FileStream(_inputFile, FileMode.Open);
229-
}
222+
stream = _inputFile.StartsWith("http") ? await _httpClient.GetStreamAsync(_inputFile)
223+
: new FileStream(_inputFile, FileMode.Open);
230224
}
231225
else
232226
{
@@ -245,20 +239,13 @@ internal async Task ParseDocumentAsync()
245239
ReferenceResolution = ResolveExternal ? ReferenceResolutionSetting.ResolveAllReferences : ReferenceResolutionSetting.ResolveLocalReferences,
246240
RuleSet = ValidationRuleSet.GetDefaultRuleSet()
247241
};
248-
if (ResolveExternal)
242+
if (ResolveExternal && !string.IsNullOrWhiteSpace(_inputFile))
249243
{
250-
if (_inputFile.StartsWith("http"))
251-
{
252-
settings.BaseUrl = new(_inputFile);
253-
}
254-
else
255-
{
256-
settings.BaseUrl = new("file://" + Path.GetDirectoryName(_inputFile) + "/");
257-
}
244+
settings.BaseUrl = _inputFile.StartsWith("http") ? new(_inputFile)
245+
: new("file://" + Path.GetDirectoryName(_inputFile) + "/");
258246
}
259247

260-
var format = OpenApiModelFactory.GetFormat(_inputFile);
261-
var readResult = await OpenApiDocument.LoadAsync(stream, format);
248+
var readResult = await OpenApiDocument.LoadAsync(stream, Format.GetDisplayName());
262249
var document = readResult.OpenApiDocument;
263250
var context = readResult.OpenApiDiagnostic;
264251

@@ -306,7 +293,6 @@ internal async Task ParseDocumentAsync()
306293
stream.Close();
307294
await stream.DisposeAsync();
308295
}
309-
310296
}
311297
}
312298

@@ -332,7 +318,7 @@ private string WriteContents(OpenApiDocument document)
332318
return new StreamReader(outputStream).ReadToEnd();
333319
}
334320

335-
private MemoryStream CreateStream(string text)
321+
private static MemoryStream CreateStream(string text)
336322
{
337323
var stream = new MemoryStream();
338324
var writer = new StreamWriter(stream);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<UseWPF>true</UseWPF>
77
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
88
<EnableWindowsTargeting>true</EnableWindowsTargeting>
9+
<NoWarn>NU1903</NoWarn>
910
</PropertyGroup>
1011
<ItemGroup>
1112
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.12.19">

test/Microsoft.OpenApi.Trimming.Tests/Microsoft.OpenApi.Trimming.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<PublishAot>true</PublishAot>
88
<TrimmerSingleWarn>false</TrimmerSingleWarn>
99
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
10+
<NoWarn>NU1903</NoWarn>
1011
<IsPackable>false</IsPackable>
1112
</PropertyGroup>
1213

0 commit comments

Comments
 (0)