-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathOpenApiParameterDeserializer.cs
324 lines (303 loc) · 11.7 KB
/
OpenApiParameterDeserializer.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Models.Interfaces;
using Microsoft.OpenApi.Models.References;
using Microsoft.OpenApi.Reader.ParseNodes;
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<OpenApiParameter> _parameterFixedFields =
new()
{
{
"name",
(o, n, t) => o.Name = n.GetScalarValue()
},
{
"in",
ProcessIn
},
{
"description",
(o, n, t) => o.Description = n.GetScalarValue()
},
{
"required",
(o, n, t) =>
{
var required = n.GetScalarValue();
if (required != null)
{
o.Required = bool.Parse(required);
}
}
},
{
"deprecated",
(o, n, t) =>
{
var deprecated = n.GetScalarValue();
if (deprecated != null)
{
o.Deprecated = bool.Parse(deprecated);
}
}
},
{
"allowEmptyValue",
(o, n, t) =>
{
var allowEmptyValue = n.GetScalarValue();
if (allowEmptyValue != null)
{
o.AllowEmptyValue = bool.Parse(allowEmptyValue);
}
}
},
{
"type",
(o, n, t) =>
{
var type = n.GetScalarValue();
if (type != null)
{
var schema = GetOrCreateSchema(o);
schema.Type = type.ToJsonSchemaType();
if ("file".Equals(type, StringComparison.OrdinalIgnoreCase))
{
schema.Format = "binary";
}
}
}
},
{
"items",
(o, n, t) => GetOrCreateSchema(o).Items = LoadSchema(n, t)
},
{
"collectionFormat",
(o, n, t) =>
{
var collectionFormat = n.GetScalarValue();
if (collectionFormat != null)
{
LoadStyle(o, collectionFormat);
}
}
},
{
"format",
(o, n, t) => GetOrCreateSchema(o).Format = n.GetScalarValue()
},
{
"minimum",
(o, n, t) =>
{
var min = n.GetScalarValue();
if (min != null)
{
GetOrCreateSchema(o).Minimum = ParserHelper.ParseDecimalWithFallbackOnOverflow(min, decimal.MinValue);
}
}
},
{
"maximum",
(o, n, t) =>
{
var max = n.GetScalarValue();
if (max != null)
{
GetOrCreateSchema(o).Maximum = ParserHelper.ParseDecimalWithFallbackOnOverflow(max, decimal.MaxValue);
}
}
},
{
"maxLength",
(o, n, t) =>
{
var maxLength = n.GetScalarValue();
if (maxLength != null)
{
GetOrCreateSchema(o).MaxLength = int.Parse(maxLength, CultureInfo.InvariantCulture);
}
}
},
{
"minLength",
(o, n, t) =>
{
var minLength = n.GetScalarValue();
if (minLength != null)
{
GetOrCreateSchema(o).MinLength = int.Parse(minLength, CultureInfo.InvariantCulture);
}
}
},
{
"readOnly",
(o, n, t) =>
{
var readOnly = n.GetScalarValue();
if (readOnly != null)
{
GetOrCreateSchema(o).ReadOnly = bool.Parse(readOnly);
}
}
},
{
"default",
(o, n, t) => GetOrCreateSchema(o).Default = n.CreateAny()
},
{
"pattern",
(o, n, t) => GetOrCreateSchema(o).Pattern = n.GetScalarValue()
},
{
"enum",
(o, n, t) => GetOrCreateSchema(o).Enum = n.CreateListOfAny()
},
{
"schema",
(o, n, t) => o.Schema = LoadSchema(n, t)
},
{
"x-examples",
LoadParameterExamplesExtension
},
};
private static readonly PatternFieldMap<OpenApiParameter> _parameterPatternFields =
new()
{
{s => s.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix, StringComparison.OrdinalIgnoreCase) && !s.Equals(OpenApiConstants.ExamplesExtension, StringComparison.OrdinalIgnoreCase),
(o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
};
private static void LoadStyle(OpenApiParameter p, string v)
{
switch (v)
{
case "csv":
if (p.In == ParameterLocation.Query)
{
p.Style = ParameterStyle.Form;
}
else
{
p.Style = ParameterStyle.Simple;
}
return;
case "ssv":
p.Style = ParameterStyle.SpaceDelimited;
return;
case "pipes":
p.Style = ParameterStyle.PipeDelimited;
return;
case "tsv":
throw new NotSupportedException();
case "multi":
p.Style = ParameterStyle.Form;
p.Explode = true;
return;
}
}
private static void LoadParameterExamplesExtension(OpenApiParameter parameter, ParseNode node, OpenApiDocument? hostDocument)
{
var examples = LoadExamplesExtension(node);
node.Context.SetTempStorage(TempStorageKeys.Examples, examples, parameter);
}
private static OpenApiSchema GetOrCreateSchema(OpenApiParameter p)
{
return p.Schema switch {
OpenApiSchema schema => schema,
_ => (OpenApiSchema)(p.Schema = new OpenApiSchema()),
};
}
private static void ProcessIn(OpenApiParameter o, ParseNode n, OpenApiDocument hostDocument)
{
var value = n.GetScalarValue();
switch (value)
{
case "body":
n.Context.SetTempStorage(TempStorageKeys.ParameterIsBodyOrFormData, true);
n.Context.SetTempStorage(TempStorageKeys.BodyParameter, o);
break;
case "formData":
n.Context.SetTempStorage(TempStorageKeys.ParameterIsBodyOrFormData, true);
var formParameters = n.Context.GetFromTempStorage<List<OpenApiParameter>>("formParameters");
if (formParameters == null)
{
formParameters = new();
n.Context.SetTempStorage("formParameters", formParameters);
}
formParameters.Add(o);
break;
case "query":
case "header":
case "path":
if (value.TryGetEnumFromDisplayName<ParameterLocation>(out var _in))
{
o.In = _in;
}
break;
default:
o.In = null;
break;
}
}
public static IOpenApiParameter? LoadParameter(ParseNode node, OpenApiDocument hostDocument)
{
return LoadParameter(node, false, hostDocument);
}
public static IOpenApiParameter? LoadParameter(ParseNode node, bool loadRequestBody, OpenApiDocument hostDocument)
{
// Reset the local variables every time this method is called.
node.Context.SetTempStorage(TempStorageKeys.ParameterIsBodyOrFormData, false);
var mapNode = node.CheckMapNode("parameter");
var pointer = mapNode.GetReferencePointer();
if (pointer != null)
{
var reference = GetReferenceIdAndExternalResource(pointer);
return new OpenApiParameterReference(reference.Item1, hostDocument, reference.Item2);
}
var parameter = new OpenApiParameter();
ParseMap(mapNode, parameter, _parameterFixedFields, _parameterPatternFields, doc: hostDocument);
var schema = node.Context.GetFromTempStorage<IOpenApiSchema>("schema");
if (schema != null)
{
parameter.Schema = schema;
node.Context.SetTempStorage("schema", null);
}
// load examples from storage and add them to the parameter
var examples = node.Context.GetFromTempStorage<Dictionary<string, IOpenApiExample>>(TempStorageKeys.Examples, parameter);
if (examples != null)
{
parameter.Examples = examples;
node.Context.SetTempStorage("examples", null);
}
var isBodyOrFormData = false;
var paramData = node.Context.GetFromTempStorage<object>(TempStorageKeys.ParameterIsBodyOrFormData);
if (paramData is bool boolValue)
{
isBodyOrFormData = boolValue;
}
if (isBodyOrFormData && !loadRequestBody)
{
return null; // Don't include Form or Body parameters when normal parameters are loaded.
}
if (loadRequestBody && !isBodyOrFormData)
{
return null; // Don't include non-Body or non-Form parameters when request bodies are loaded.
}
return parameter;
}
}
}