forked from microsoft/OpenAPI.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenApiOperationDeserializer.cs
More file actions
117 lines (109 loc) · 4.26 KB
/
OpenApiOperationDeserializer.cs
File metadata and controls
117 lines (109 loc) · 4.26 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Models.References;
using Microsoft.OpenApi.Reader.ParseNodes;
namespace Microsoft.OpenApi.Reader.V3
{
/// <summary>
/// Class containing logic to deserialize Open API V3 document into
/// runtime Open API object model.
/// </summary>
internal static partial class OpenApiV3Deserializer
{
private static readonly FixedFieldMap<OpenApiOperation> _operationFixedFields =
new()
{
{
"tags", (o, n, doc) => {
if (n.CreateSimpleList(
(valueNode, doc) =>
{
var val = valueNode.GetScalarValue();
if (string.IsNullOrEmpty(val))
return null; // Avoid exception on empty tag, we'll remove these from the list further on
return LoadTagByReference(val , doc);
},
doc)
// Filter out empty tags instead of excepting on them
.OfType<OpenApiTagReference>().ToList() is {Count: > 0} tags)
{
o.Tags = new HashSet<OpenApiTagReference>(tags, OpenApiTagComparer.Instance);
}
}
},
{
"summary",
(o, n, _) => o.Summary = n.GetScalarValue()
},
{
"description",
(o, n, _) => o.Description = n.GetScalarValue()
},
{
"externalDocs",
(o, n, doc) => o.ExternalDocs = LoadExternalDocs(n, doc)
},
{
"operationId",
(o, n, _) => o.OperationId = n.GetScalarValue()
},
{
"parameters",
(o, n, t) => o.Parameters = n.CreateList(LoadParameter, t)
},
{
"requestBody",
(o, n, t) => o.RequestBody = LoadRequestBody(n, t)
},
{
"responses",
(o, n, t) => o.Responses = LoadResponses(n, t)
},
{
"callbacks",
(o, n, t) => o.Callbacks = n.CreateMap(LoadCallback, t)
},
{
"deprecated",
(o, n, _) =>
{
var deprecated = n.GetScalarValue();
if (deprecated != null)
{
o.Deprecated = bool.Parse(deprecated);
}
}
},
{
"security",
(o, n, t) => o.Security = n.CreateList(LoadSecurityRequirement, t)
},
{
"servers",
(o, n, t) => o.Servers = n.CreateList(LoadServer, t)
},
};
private static readonly PatternFieldMap<OpenApiOperation> _operationPatternFields =
new()
{
{s => s.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix, StringComparison.OrdinalIgnoreCase), (o, p, n, _) => o.AddExtension(p, LoadExtension(p,n))},
};
internal static OpenApiOperation LoadOperation(ParseNode node, OpenApiDocument hostDocument)
{
var mapNode = node.CheckMapNode("Operation");
var operation = new OpenApiOperation();
ParseMap(mapNode, operation, _operationFixedFields, _operationPatternFields, hostDocument);
return operation;
}
private static OpenApiTagReference LoadTagByReference(
string tagName, OpenApiDocument? hostDocument)
{
return new OpenApiTagReference(tagName, hostDocument);
}
}
}