Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading