Skip to content

Commit 9b910f3

Browse files
committed
fix: moves the http client for the reader to settings so it can be passed by client application
Signed-off-by: Vincent Biret <[email protected]>
1 parent cea8929 commit 9b910f3

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs

+6-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.IO;
66
using System.Linq;
7-
using System.Net.Http;
87
using System.Security;
98
using System.Text;
109
using System.Threading;
@@ -21,8 +20,6 @@ namespace Microsoft.OpenApi.Reader
2120
/// </summary>
2221
public static class OpenApiModelFactory
2322
{
24-
private static readonly HttpClient _httpClient = new();
25-
2623
/// <summary>
2724
/// Loads the input stream and parses it into an Open API document.
2825
/// </summary>
@@ -81,7 +78,8 @@ public static T Load<T>(MemoryStream input, OpenApiSpecVersion version, string f
8178
/// <returns></returns>
8279
public static async Task<ReadResult> LoadAsync(string url, OpenApiReaderSettings settings = null, CancellationToken token = default)
8380
{
84-
var (stream, format) = await RetrieveStreamAndFormatAsync(url, token).ConfigureAwait(false);
81+
settings ??= DefaultReaderSettings.Value;
82+
var (stream, format) = await RetrieveStreamAndFormatAsync(url, settings, token).ConfigureAwait(false);
8583
return await LoadAsync(stream, format, settings, token).ConfigureAwait(false);
8684
}
8785

@@ -98,7 +96,8 @@ public static async Task<ReadResult> LoadAsync(string url, OpenApiReaderSettings
9896
/// <returns>The OpenAPI element.</returns>
9997
public static async Task<T> LoadAsync<T>(string url, OpenApiSpecVersion version, OpenApiDocument openApiDocument, OpenApiReaderSettings settings = null, CancellationToken token = default) where T : IOpenApiElement
10098
{
101-
var (stream, format) = await RetrieveStreamAndFormatAsync(url, token).ConfigureAwait(false);
99+
settings ??= DefaultReaderSettings.Value;
100+
var (stream, format) = await RetrieveStreamAndFormatAsync(url, settings, token).ConfigureAwait(false);
102101
return await LoadAsync<T>(stream, version, openApiDocument, format, settings, token);
103102
}
104103

@@ -286,7 +285,7 @@ private static ReadResult InternalLoad(MemoryStream input, string format, OpenAp
286285
return readResult;
287286
}
288287

289-
private static async Task<(Stream, string)> RetrieveStreamAndFormatAsync(string url, CancellationToken token = default)
288+
private static async Task<(Stream, string)> RetrieveStreamAndFormatAsync(string url, OpenApiReaderSettings settings, CancellationToken token = default)
290289
{
291290
if (!string.IsNullOrEmpty(url))
292291
{
@@ -296,7 +295,7 @@ private static ReadResult InternalLoad(MemoryStream input, string format, OpenAp
296295
if (url.StartsWith("http", StringComparison.OrdinalIgnoreCase)
297296
|| url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
298297
{
299-
var response = await _httpClient.GetAsync(url, token).ConfigureAwait(false);
298+
var response = await settings.HttpClient.GetAsync(url, token).ConfigureAwait(false);
300299
var mediaType = response.Content.Headers.ContentType.MediaType;
301300
var contentType = mediaType.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)[0];
302301
format = contentType.Split('/').Last().Split('+').Last().Split('-').Last();

src/Microsoft.OpenApi/Reader/OpenApiReaderSettings.cs

+18
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.IO;
7+
using System.Net.Http;
78
using System.Text.Json.Nodes;
89
using Microsoft.OpenApi.Interfaces;
910
using Microsoft.OpenApi.MicrosoftExtensions;
@@ -17,6 +18,23 @@ namespace Microsoft.OpenApi.Reader
1718
/// </summary>
1819
public class OpenApiReaderSettings
1920
{
21+
private static readonly Lazy<HttpClient> httpClient = new(() => new HttpClient());
22+
private HttpClient _httpClient;
23+
/// <summary>
24+
/// HttpClient to use for making requests and retrieve documents
25+
/// </summary>
26+
public HttpClient HttpClient
27+
{
28+
get
29+
{
30+
_httpClient ??= httpClient.Value;
31+
return _httpClient;
32+
}
33+
init
34+
{
35+
_httpClient = value;
36+
}
37+
}
2038
/// <summary>
2139
/// Adds a reader for the specified format
2240
/// </summary>

test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,7 @@ namespace Microsoft.OpenApi.Reader
14971497
public Microsoft.OpenApi.Interfaces.IStreamLoader CustomExternalLoader { get; set; }
14981498
public System.Collections.Generic.List<string> DefaultContentType { get; set; }
14991499
public System.Collections.Generic.Dictionary<string, System.Func<System.Text.Json.Nodes.JsonNode, Microsoft.OpenApi.OpenApiSpecVersion, Microsoft.OpenApi.Interfaces.IOpenApiExtension>> ExtensionParsers { get; set; }
1500+
public System.Net.Http.HttpClient HttpClient { get; init; }
15001501
public bool LeaveStreamOpen { get; set; }
15011502
public bool LoadExternalRefs { get; set; }
15021503
public System.Collections.Generic.Dictionary<string, Microsoft.OpenApi.Interfaces.IOpenApiReader> Readers { get; init; }

0 commit comments

Comments
 (0)