-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathOpenApiReaderRegistry.cs
More file actions
48 lines (42 loc) · 1.67 KB
/
OpenApiReaderRegistry.cs
File metadata and controls
48 lines (42 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Concurrent;
using Microsoft.OpenApi.Interfaces;
namespace Microsoft.OpenApi.Reader
{
/// <summary>
/// Registry for managing different OpenAPI format providers.
/// </summary>
public static class OpenApiReaderRegistry
{
private static readonly ConcurrentDictionary<string, IOpenApiReader> _readers = new(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// Defines a default OpenAPI reader.
/// </summary>
public static readonly IOpenApiReader DefaultReader = new OpenApiJsonReader();
/// <summary>
/// Registers an IOpenApiReader for a given OpenAPI format.
/// </summary>
/// <param name="format">The OpenApi file format.</param>
/// <param name="reader">The reader instance.</param>
public static void RegisterReader(string format, IOpenApiReader reader)
{
_readers.AddOrUpdate(format, reader, (_, _) => reader);
}
/// <summary>
/// Retrieves an IOpenApiReader for a given OpenAPI format.
/// </summary>
/// <param name="format"></param>
/// <returns></returns>
/// <exception cref="NotSupportedException"></exception>
public static IOpenApiReader GetReader(string format)
{
if (_readers.TryGetValue(format, out var reader))
{
return reader;
}
throw new NotSupportedException($"Format '{format}' is not supported. Register your reader with the OpenApiReaderRegistry class.");
}
}
}