Skip to content

Commit

Permalink
Remove threading package and disable warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
MaggieKimani1 committed Oct 8, 2024
1 parent 9c64bea commit d04b22b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
5 changes: 2 additions & 3 deletions src/Microsoft.OpenApi/Microsoft.OpenApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
<PropertyGroup Condition="'$(TF_BUILD)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Threading" Version="17.11.20" />
<PackageReference Include="System.Text.Json" Version="8.0.4" />
<ItemGroup>
<PackageReference Include="System.Text.Json" Version="8.0.4" />
</ItemGroup>

<ItemGroup>
Expand Down
32 changes: 20 additions & 12 deletions src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using System.Threading.Tasks;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models;
using Microsoft.VisualStudio.Threading;

namespace Microsoft.OpenApi.Reader
{
Expand All @@ -20,8 +19,6 @@ namespace Microsoft.OpenApi.Reader
public static class OpenApiModelFactory
{
private static readonly HttpClient _httpClient = new();
private static readonly JoinableTaskContext _joinableTaskContext = new();
private static readonly JoinableTaskFactory _joinableTaskFactory = new(_joinableTaskContext);

static OpenApiModelFactory()
{
Expand All @@ -36,7 +33,9 @@ static OpenApiModelFactory()
/// <returns>An OpenAPI document instance.</returns>
public static ReadResult Load(string url, OpenApiReaderSettings settings = null)
{
return _joinableTaskFactory.Run(async () => await LoadAsync(url, settings));
#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits
return LoadAsync(url, settings).GetAwaiter().GetResult();
#pragma warning restore VSTHRD002 // Avoid problematic synchronous waits
}

/// <summary>
Expand All @@ -52,9 +51,10 @@ public static ReadResult Load(Stream stream,
{
settings ??= new OpenApiReaderSettings();

// Run the async method synchronously using JoinableTaskFactory
var result = _joinableTaskFactory.Run(async () => await LoadAsync(stream, format, settings));

#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits
var result = LoadAsync(stream, format, settings).GetAwaiter().GetResult();
#pragma warning restore VSTHRD002 // Avoid problematic synchronous waits

if (!settings.LeaveStreamOpen)
{
stream.Dispose();
Expand All @@ -74,8 +74,9 @@ public static ReadResult Load(TextReader input,
string format,
OpenApiReaderSettings settings = null)
{
// Run the async method synchronously using JoinableTaskFactory
var result = _joinableTaskFactory.Run(async () => await LoadAsync(input, format, settings));
#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits
var result = LoadAsync(input, format, settings).GetAwaiter().GetResult();
#pragma warning restore VSTHRD002 // Avoid problematic synchronous waits
return result;
}

Expand Down Expand Up @@ -153,7 +154,9 @@ public static ReadResult Parse(string input,
settings ??= new OpenApiReaderSettings();
using var reader = new StringReader(input);

return _joinableTaskFactory.Run(async () => await ParseAsync(input, reader, format, settings));
#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits
return ParseAsync(input, reader, format, settings).GetAwaiter().GetResult();
#pragma warning restore VSTHRD002 // Avoid problematic synchronous waits
}

/// <summary>
Expand Down Expand Up @@ -208,7 +211,9 @@ public static T Load<T>(string url, OpenApiSpecVersion version, out OpenApiDiagn
var format = GetFormat(url);
settings ??= new OpenApiReaderSettings();

var stream = _joinableTaskFactory.Run(async () => await GetStreamAsync(url));
#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits
var stream = GetStreamAsync(url).GetAwaiter().GetResult();
#pragma warning restore VSTHRD002 // Avoid problematic synchronous waits

return Load<T>(stream, version, format, out diagnostic, settings);
}
Expand Down Expand Up @@ -253,7 +258,10 @@ private static string GetContentType(string url)
{
if (!string.IsNullOrEmpty(url))
{
var response = _joinableTaskFactory.Run(async () => await _httpClient.GetAsync(url));
#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits
var response = _httpClient.GetAsync(url).GetAwaiter().GetResult();
#pragma warning restore VSTHRD002 // Avoid problematic synchronous waits

var mediaType = response.Content.Headers.ContentType.MediaType;
return mediaType.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).First();
}
Expand Down

0 comments on commit d04b22b

Please sign in to comment.