-
Notifications
You must be signed in to change notification settings - Fork 252
/
Copy pathOpenApiOperationDeserializer.cs
217 lines (191 loc) · 8.82 KB
/
OpenApiOperationDeserializer.cs
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System.Collections.Generic;
using System.Linq;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Reader.ParseNodes;
using Microsoft.OpenApi.Models.References;
using Microsoft.OpenApi.Models.Interfaces;
using System;
namespace Microsoft.OpenApi.Reader.V2
{
/// <summary>
/// Class containing logic to deserialize Open API V2 document into
/// runtime Open API object model.
/// </summary>
internal static partial class OpenApiV2Deserializer
{
private static readonly FixedFieldMap<OpenApiOperation> _operationFixedFields =
new()
{
{
"tags", (o, n, doc) => o.Tags = n.CreateSimpleList(
(valueNode, doc) =>
LoadTagByReference(
valueNode.GetScalarValue(), doc), doc)
},
{
"summary",
(o, n, _) => o.Summary = n.GetScalarValue()
},
{
"description",
(o, n, _) => o.Description = n.GetScalarValue()
},
{
"externalDocs",
(o, n, t) => o.ExternalDocs = LoadExternalDocs(n, t)
},
{
"operationId",
(o, n, _) => o.OperationId = n.GetScalarValue()
},
{
"parameters",
(o, n, t) => o.Parameters = n.CreateList(LoadParameter, t)
},
{
"consumes", (_, n, doc) => {
var consumes = n.CreateSimpleList((s, p) => s.GetScalarValue(), doc);
if (consumes.Count > 0) {
n.Context.SetTempStorage(TempStorageKeys.OperationConsumes,consumes);
}
}
},
{
"produces", (_, n, doc) => {
var produces = n.CreateSimpleList((s, p) => s.GetScalarValue(), doc);
if (produces.Count > 0) {
n.Context.SetTempStorage(TempStorageKeys.OperationProduces, produces);
}
}
},
{
"responses",
(o, n, t) => o.Responses = LoadResponses(n, t)
},
{
"deprecated",
(o, n, _) => o.Deprecated = bool.Parse(n.GetScalarValue())
},
{
"security",
(o, n, t) => o.Security = n.CreateList(LoadSecurityRequirement, t)
},
};
private static readonly PatternFieldMap<OpenApiOperation> _operationPatternFields =
new()
{
{s => s.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix, StringComparison.OrdinalIgnoreCase), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
};
private static readonly FixedFieldMap<OpenApiResponses> _responsesFixedFields = new();
private static readonly PatternFieldMap<OpenApiResponses> _responsesPatternFields =
new()
{
{s => !s.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix, StringComparison.OrdinalIgnoreCase), (o, p, n, t) => o.Add(p, LoadResponse(n, t))},
{s => s.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix, StringComparison.OrdinalIgnoreCase), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
};
internal static OpenApiOperation LoadOperation(ParseNode node, OpenApiDocument hostDocument)
{
// Reset these temp storage parameters for each operation.
node.Context.SetTempStorage(TempStorageKeys.BodyParameter, null);
node.Context.SetTempStorage(TempStorageKeys.FormParameters, null);
node.Context.SetTempStorage(TempStorageKeys.OperationProduces, null);
node.Context.SetTempStorage(TempStorageKeys.OperationConsumes, null);
var mapNode = node.CheckMapNode("Operation");
var operation = new OpenApiOperation();
ParseMap(mapNode, operation, _operationFixedFields, _operationPatternFields, doc: hostDocument);
// Build request body based on information determined while parsing OpenApiOperation
var bodyParameter = node.Context.GetFromTempStorage<OpenApiParameter>(TempStorageKeys.BodyParameter);
if (bodyParameter != null)
{
operation.RequestBody = CreateRequestBody(node.Context, bodyParameter);
}
else
{
var formParameters = node.Context.GetFromTempStorage<List<OpenApiParameter>>(TempStorageKeys.FormParameters);
if (formParameters != null)
{
operation.RequestBody = CreateFormBody(node.Context, formParameters);
}
}
foreach (var response in operation.Responses.Values.OfType<OpenApiResponse>())
{
ProcessProduces(node.CheckMapNode("responses"), response, node.Context);
}
// Reset so that it's not picked up later
node.Context.SetTempStorage(TempStorageKeys.OperationProduces, null);
return operation;
}
public static OpenApiResponses LoadResponses(ParseNode node, OpenApiDocument hostDocument)
{
var mapNode = node.CheckMapNode("Responses");
var domainObject = new OpenApiResponses();
ParseMap(mapNode, domainObject, _responsesFixedFields, _responsesPatternFields, doc:hostDocument);
return domainObject;
}
private static OpenApiRequestBody CreateFormBody(ParsingContext context, List<OpenApiParameter> formParameters)
{
var mediaType = new OpenApiMediaType
{
Schema = new OpenApiSchema()
{
Properties = formParameters.ToDictionary(
k => k.Name,
v =>
{
var schema = new OpenApiSchema(v.Schema)
{
Description = v.Description,
Extensions = v.Extensions
};
return (IOpenApiSchema)schema;
}),
Required = new HashSet<string>(formParameters.Where(static p => p.Required).Select(static p => p.Name), StringComparer.Ordinal)
}
};
var consumes = context.GetFromTempStorage<List<string>>(TempStorageKeys.OperationConsumes) ??
context.GetFromTempStorage<List<string>>(TempStorageKeys.GlobalConsumes) ??
new List<string> { "application/x-www-form-urlencoded" };
var formBody = new OpenApiRequestBody
{
Content = consumes.ToDictionary(
k => k,
_ => mediaType)
};
foreach (var value in formBody.Content.Values.Where(static x => x.Schema is not null && x.Schema.Properties.Any() && x.Schema.Type == null).Select(static x => x.Schema).OfType<OpenApiSchema>())
value.Type = JsonSchemaType.Object;
return formBody;
}
internal static IOpenApiRequestBody CreateRequestBody(
ParsingContext context,
IOpenApiParameter bodyParameter)
{
var consumes = context.GetFromTempStorage<List<string>>(TempStorageKeys.OperationConsumes) ??
context.GetFromTempStorage<List<string>>(TempStorageKeys.GlobalConsumes) ??
new List<string> { "application/json" };
var requestBody = new OpenApiRequestBody
{
Description = bodyParameter.Description,
Required = bodyParameter.Required,
Content = consumes.ToDictionary(
k => k,
_ => new OpenApiMediaType
{
Schema = bodyParameter.Schema,
Examples = bodyParameter.Examples
}),
Extensions = bodyParameter.Extensions
};
requestBody.Extensions[OpenApiConstants.BodyName] = new OpenApiAny(bodyParameter.Name);
return requestBody;
}
private static OpenApiTagReference LoadTagByReference(
string tagName, OpenApiDocument hostDocument)
{
return new OpenApiTagReference(tagName, hostDocument);
}
}
}