-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathOpenApiV31VersionService.cs
More file actions
90 lines (81 loc) · 4.24 KB
/
OpenApiV31VersionService.cs
File metadata and controls
90 lines (81 loc) · 4.24 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Models.References;
using Microsoft.OpenApi.Reader.ParseNodes;
using Microsoft.OpenApi.Reader.V3;
namespace Microsoft.OpenApi.Reader.V31
{
/// <summary>
/// The version service for the Open API V3.1.
/// </summary>
internal class OpenApiV31VersionService : IOpenApiVersionService
{
public OpenApiDiagnostic Diagnostic { get; }
/// <summary>
/// Create Parsing Context
/// </summary>
/// <param name="diagnostic">Provide instance for diagnotic object for collecting and accessing information about the parsing.</param>
public OpenApiV31VersionService(OpenApiDiagnostic diagnostic)
{
Diagnostic = diagnostic;
}
private readonly IDictionary<Type, Func<ParseNode, OpenApiDocument, object>> _loaders = new Dictionary<Type, Func<ParseNode, OpenApiDocument, object>>
{
[typeof(OpenApiAny)] = OpenApiV31Deserializer.LoadAny,
[typeof(OpenApiCallback)] = OpenApiV31Deserializer.LoadCallback,
[typeof(OpenApiComponents)] = OpenApiV31Deserializer.LoadComponents,
[typeof(OpenApiContact)] = OpenApiV31Deserializer.LoadContact,
[typeof(OpenApiDiscriminator)] = OpenApiV3Deserializer.LoadDiscriminator,
[typeof(OpenApiEncoding)] = OpenApiV31Deserializer.LoadEncoding,
[typeof(OpenApiExample)] = OpenApiV31Deserializer.LoadExample,
[typeof(OpenApiExternalDocs)] = OpenApiV31Deserializer.LoadExternalDocs,
[typeof(OpenApiHeader)] = OpenApiV31Deserializer.LoadHeader,
[typeof(OpenApiInfo)] = OpenApiV31Deserializer.LoadInfo,
[typeof(OpenApiLicense)] = OpenApiV31Deserializer.LoadLicense,
[typeof(OpenApiLink)] = OpenApiV31Deserializer.LoadLink,
[typeof(OpenApiMediaType)] = OpenApiV31Deserializer.LoadMediaType,
[typeof(OpenApiOAuthFlow)] = OpenApiV31Deserializer.LoadOAuthFlow,
[typeof(OpenApiOAuthFlows)] = OpenApiV31Deserializer.LoadOAuthFlows,
[typeof(OpenApiOperation)] = OpenApiV31Deserializer.LoadOperation,
[typeof(OpenApiParameter)] = OpenApiV31Deserializer.LoadParameter,
[typeof(OpenApiPathItem)] = OpenApiV31Deserializer.LoadPathItem,
[typeof(OpenApiPaths)] = OpenApiV31Deserializer.LoadPaths,
[typeof(OpenApiRequestBody)] = OpenApiV31Deserializer.LoadRequestBody,
[typeof(OpenApiResponse)] = OpenApiV31Deserializer.LoadResponse,
[typeof(OpenApiResponses)] = OpenApiV31Deserializer.LoadResponses,
[typeof(OpenApiSchema)] = OpenApiV31Deserializer.LoadSchema,
[typeof(OpenApiSecurityRequirement)] = OpenApiV31Deserializer.LoadSecurityRequirement,
[typeof(OpenApiSecurityScheme)] = OpenApiV31Deserializer.LoadSecurityScheme,
[typeof(OpenApiServer)] = OpenApiV31Deserializer.LoadServer,
[typeof(OpenApiServerVariable)] = OpenApiV31Deserializer.LoadServerVariable,
[typeof(OpenApiTag)] = OpenApiV31Deserializer.LoadTag,
[typeof(OpenApiXml)] = OpenApiV31Deserializer.LoadXml,
[typeof(OpenApiSchemaReference)] = OpenApiV31Deserializer.LoadMapping
};
public OpenApiDocument LoadDocument(RootNode rootNode)
{
return OpenApiV31Deserializer.LoadOpenApi(rootNode);
}
public T LoadElement<T>(ParseNode node, OpenApiDocument doc) where T : IOpenApiElement
{
return (T)_loaders[typeof(T)](node, doc);
}
/// <inheritdoc />
public string? GetReferenceScalarValues(MapNode mapNode, string scalarValue)
{
if (mapNode.Any(static x => !"$ref".Equals(x.Name, StringComparison.OrdinalIgnoreCase)))
{
var valueNode = mapNode.Where(x => x.Name.Equals(scalarValue))
.Select(static x => x.Value).OfType<ValueNode>().FirstOrDefault();
return valueNode?.GetScalarValue();
}
return null;
}
}
}