Skip to content

Commit 0ff19f8

Browse files
committed
draft: removes static registry for readers
Signed-off-by: Vincent Biret <[email protected]>
1 parent 5a05d1e commit 0ff19f8

File tree

5 files changed

+17
-42
lines changed

5 files changed

+17
-42
lines changed

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,6 @@ namespace Microsoft.OpenApi.Hidi
4040
{
4141
internal static class OpenApiService
4242
{
43-
static OpenApiService()
44-
{
45-
OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader());
46-
OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yml, new OpenApiYamlReader());
47-
}
48-
4943
/// <summary>
5044
/// Implementation of the transform command
5145
/// </summary>
@@ -394,6 +388,9 @@ private static async Task<ReadResult> ParseOpenApiAsync(string openApiFile, bool
394388
new(openApiFile) :
395389
new Uri("file://" + new FileInfo(openApiFile).DirectoryName + Path.DirectorySeparatorChar)
396390
};
391+
var yamlReader = new OpenApiYamlReader();
392+
settings.Readers.Add(OpenApiConstants.Yaml, yamlReader);
393+
settings.Readers.Add(OpenApiConstants.Yml, yamlReader);
397394

398395
result = await OpenApiDocument.LoadAsync(stream, settings: settings, cancellationToken: cancellationToken).ConfigureAwait(false);
399396

src/Microsoft.OpenApi.Workbench/MainModel.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,6 @@ protected void OnPropertyChanged(string propertyName)
211211
/// </summary>
212212
internal async Task ParseDocumentAsync()
213213
{
214-
OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader());
215-
OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yml, new OpenApiYamlReader());
216-
217214
Stream stream = null;
218215
try
219216
{
@@ -238,6 +235,9 @@ internal async Task ParseDocumentAsync()
238235
{
239236
RuleSet = ValidationRuleSet.GetDefaultRuleSet()
240237
};
238+
var yamlReader = new OpenApiYamlReader();
239+
settings.Readers.Add(OpenApiConstants.Yaml, yamlReader);
240+
settings.Readers.Add(OpenApiConstants.Yml, yamlReader);
241241
if (ResolveExternal && !string.IsNullOrWhiteSpace(_inputFile))
242242
{
243243
settings.BaseUrl = _inputFile.StartsWith("http", StringComparison.OrdinalIgnoreCase) ? new(_inputFile)

src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ public static class OpenApiModelFactory
2323
{
2424
private static readonly HttpClient _httpClient = new();
2525

26-
static OpenApiModelFactory()
27-
{
28-
OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Json, new OpenApiJsonReader());
29-
}
30-
3126
/// <summary>
3227
/// Loads the input stream and parses it into an Open API document.
3328
/// </summary>
@@ -45,6 +40,7 @@ public static ReadResult Load(MemoryStream stream,
4540
if (stream is null) throw new ArgumentNullException(nameof(stream));
4641
#endif
4742
settings ??= new OpenApiReaderSettings();
43+
settings.Readers.TryAdd(OpenApiConstants.Json, new OpenApiJsonReader());
4844

4945
// Get the format of the stream if not provided
5046
format ??= InspectStreamFormat(stream);
@@ -73,7 +69,7 @@ public static ReadResult Load(MemoryStream stream,
7369
public static T Load<T>(MemoryStream input, OpenApiSpecVersion version, string format, OpenApiDocument openApiDocument, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings settings = null) where T : IOpenApiElement
7470
{
7571
format ??= InspectStreamFormat(input);
76-
return OpenApiReaderRegistry.GetReader(format).ReadFragment<T>(input, version, openApiDocument, out diagnostic, settings);
72+
return settings.Readers[format].ReadFragment<T>(input, version, openApiDocument, out diagnostic, settings);
7773
}
7874

7975
/// <summary>
@@ -122,6 +118,7 @@ public static async Task<ReadResult> LoadAsync(Stream input, string format = nul
122118
if (input is null) throw new ArgumentNullException(nameof(input));
123119
#endif
124120
settings ??= new OpenApiReaderSettings();
121+
settings.Readers.TryAdd(OpenApiConstants.Json, new OpenApiJsonReader());
125122

126123
Stream preparedStream;
127124
if (format is null)
@@ -204,6 +201,7 @@ public static ReadResult Parse(string input,
204201
#endif
205202
format ??= InspectInputFormat(input);
206203
settings ??= new OpenApiReaderSettings();
204+
settings.Readers.TryAdd(OpenApiConstants.Json, new OpenApiJsonReader());
207205

208206
// Copy string into MemoryStream
209207
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(input));
@@ -235,6 +233,7 @@ public static T Parse<T>(string input,
235233
#endif
236234
format ??= InspectInputFormat(input);
237235
settings ??= new OpenApiReaderSettings();
236+
settings.Readers.TryAdd(OpenApiConstants.Json, new OpenApiJsonReader());
238237
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(input));
239238
return Load<T>(stream, version, format, openApiDocument, out diagnostic, settings);
240239
}
@@ -243,7 +242,7 @@ public static T Parse<T>(string input,
243242

244243
private static async Task<ReadResult> InternalLoadAsync(Stream input, string format, OpenApiReaderSettings settings, CancellationToken cancellationToken = default)
245244
{
246-
var reader = OpenApiReaderRegistry.GetReader(format);
245+
var reader = settings.Readers[format];
247246
var readResult = await reader.ReadAsync(input, settings, cancellationToken).ConfigureAwait(false);
248247

249248
if (settings?.LoadExternalRefs ?? DefaultReaderSettings.LoadExternalRefs)
@@ -283,7 +282,7 @@ private static ReadResult InternalLoad(MemoryStream input, string format, OpenAp
283282
throw new ArgumentException($"Cannot parse the stream: {nameof(input)} is empty or contains no elements.");
284283
}
285284

286-
var reader = OpenApiReaderRegistry.GetReader(format);
285+
var reader = settings.Readers[format];
287286
var readResult = reader.Read(input, settings);
288287
return readResult;
289288
}

src/Microsoft.OpenApi/Reader/OpenApiReaderRegistry.cs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT license.
33

44
using System;
5-
using System.Collections.Concurrent;
65
using Microsoft.OpenApi.Interfaces;
76

87
namespace Microsoft.OpenApi.Reader
@@ -12,37 +11,13 @@ namespace Microsoft.OpenApi.Reader
1211
/// </summary>
1312
public static class OpenApiReaderRegistry
1413
{
15-
private static readonly ConcurrentDictionary<string, IOpenApiReader> _readers = new(StringComparer.OrdinalIgnoreCase);
16-
17-
/// <summary>
18-
/// Defines a default OpenAPI reader.
19-
/// </summary>
20-
public static readonly IOpenApiReader DefaultReader = new OpenApiJsonReader();
21-
2214
/// <summary>
2315
/// Registers an IOpenApiReader for a given OpenAPI format.
2416
/// </summary>
2517
/// <param name="format">The OpenApi file format.</param>
2618
/// <param name="reader">The reader instance.</param>
2719
public static void RegisterReader(string format, IOpenApiReader reader)
2820
{
29-
_readers.AddOrUpdate(format, reader, (_, _) => reader);
30-
}
31-
32-
/// <summary>
33-
/// Retrieves an IOpenApiReader for a given OpenAPI format.
34-
/// </summary>
35-
/// <param name="format"></param>
36-
/// <returns></returns>
37-
/// <exception cref="NotSupportedException"></exception>
38-
public static IOpenApiReader GetReader(string format)
39-
{
40-
if (_readers.TryGetValue(format, out var reader))
41-
{
42-
return reader;
43-
}
44-
45-
throw new NotSupportedException($"Format '{format}' is not supported. Register your reader with the OpenApiReaderRegistry class.");
4621
}
4722
}
4823
}

src/Microsoft.OpenApi/Reader/OpenApiReaderSettings.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ namespace Microsoft.OpenApi.Reader
1616
/// </summary>
1717
public class OpenApiReaderSettings
1818
{
19+
/// <summary>
20+
/// Readers to use to parse the OpenAPI document
21+
/// </summary>
22+
public Dictionary<string, IOpenApiReader> Readers { get; init; } = new Dictionary<string, IOpenApiReader>(StringComparer.OrdinalIgnoreCase);
1923
/// <summary>
2024
/// When external references are found, load them into a shared workspace
2125
/// </summary>

0 commit comments

Comments
 (0)