Skip to content

Commit

Permalink
Merge pull request #1956 from microsoft/mk/fix-workbench-format-bug
Browse files Browse the repository at this point in the history
Fix: Workbench fails because of a missing format parameter
  • Loading branch information
MaggieKimani1 authored Nov 26, 2024
2 parents 1338905 + 506ef1f commit 5ceb174
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 27 deletions.
40 changes: 13 additions & 27 deletions src/Microsoft.OpenApi.Workbench/MainModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class MainModel : INotifyPropertyChanged
/// </summary>
private OpenApiSpecVersion _version = OpenApiSpecVersion.OpenApi3_0;

private HttpClient _httpClient = new();
private static readonly HttpClient _httpClient = new();

public string Input
{
Expand Down Expand Up @@ -166,31 +166,31 @@ public OpenApiSpecVersion Version
public bool IsYaml
{
get => Format == OpenApiFormat.Yaml;
set => Format = OpenApiFormat.Yaml;
set => Format = value ? OpenApiFormat.Yaml : Format;
}

public bool IsJson
{
get => Format == OpenApiFormat.Json;
set => Format = OpenApiFormat.Json;
set => Format = value ? OpenApiFormat.Json : Format;
}

public bool IsV2_0
{
get => Version == OpenApiSpecVersion.OpenApi2_0;
set => Version = OpenApiSpecVersion.OpenApi2_0;
set => Version = value ? OpenApiSpecVersion.OpenApi2_0 : Version;
}

public bool IsV3_0
{
get => Version == OpenApiSpecVersion.OpenApi3_0;
set => Version = OpenApiSpecVersion.OpenApi3_0;
set => Version = value ? OpenApiSpecVersion.OpenApi3_0 : Version;
}

public bool IsV3_1
{
get => Version == OpenApiSpecVersion.OpenApi3_1;
set => Version = OpenApiSpecVersion.OpenApi3_1;
set => Version = value ? OpenApiSpecVersion.OpenApi3_1 : Version;
}

/// <summary>
Expand Down Expand Up @@ -219,14 +219,8 @@ internal async Task ParseDocumentAsync()
{
if (!string.IsNullOrWhiteSpace(_inputFile))
{
if (_inputFile.StartsWith("http"))
{
stream = await _httpClient.GetStreamAsync(_inputFile);
}
else
{
stream = new FileStream(_inputFile, FileMode.Open);
}
stream = _inputFile.StartsWith("http") ? await _httpClient.GetStreamAsync(_inputFile)
: new FileStream(_inputFile, FileMode.Open);
}
else
{
Expand All @@ -245,20 +239,13 @@ internal async Task ParseDocumentAsync()
ReferenceResolution = ResolveExternal ? ReferenceResolutionSetting.ResolveAllReferences : ReferenceResolutionSetting.ResolveLocalReferences,
RuleSet = ValidationRuleSet.GetDefaultRuleSet()
};
if (ResolveExternal)
if (ResolveExternal && !string.IsNullOrWhiteSpace(_inputFile))
{
if (_inputFile.StartsWith("http"))
{
settings.BaseUrl = new(_inputFile);
}
else
{
settings.BaseUrl = new("file://" + Path.GetDirectoryName(_inputFile) + "/");
}
settings.BaseUrl = _inputFile.StartsWith("http") ? new(_inputFile)
: new("file://" + Path.GetDirectoryName(_inputFile) + "/");
}

var format = OpenApiModelFactory.GetFormat(_inputFile);
var readResult = await OpenApiDocument.LoadAsync(stream, format);
var readResult = await OpenApiDocument.LoadAsync(stream, Format.GetDisplayName());
var document = readResult.OpenApiDocument;
var context = readResult.OpenApiDiagnostic;

Expand Down Expand Up @@ -306,7 +293,6 @@ internal async Task ParseDocumentAsync()
stream.Close();
await stream.DisposeAsync();
}

}
}

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

private MemoryStream CreateStream(string text)
private static MemoryStream CreateStream(string text)
{
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<UseWPF>true</UseWPF>
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
<EnableWindowsTargeting>true</EnableWindowsTargeting>
<NoWarn>NU1903</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.12.19">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<PublishAot>true</PublishAot>
<TrimmerSingleWarn>false</TrimmerSingleWarn>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<NoWarn>NU1903</NoWarn>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down

0 comments on commit 5ceb174

Please sign in to comment.