-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathOpenApiParameterDeserializer.cs
243 lines (223 loc) · 9.11 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
// 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) => o.Required = bool.Parse(n.GetScalarValue())
},
{
"deprecated",
(o, n, t) => o.Deprecated = bool.Parse(n.GetScalarValue())
},
{
"allowEmptyValue",
(o, n, t) => o.AllowEmptyValue = bool.Parse(n.GetScalarValue())
},
{
"type",
(o, n, t) => GetOrCreateSchema(o).Type = n.GetScalarValue().ToJsonSchemaType()
},
{
"items",
(o, n, t) => GetOrCreateSchema(o).Items = LoadSchema(n, t)
},
{
"collectionFormat",
(o, n, t) => LoadStyle(o, n.GetScalarValue())
},
{
"format",
(o, n, t) => GetOrCreateSchema(o).Format = n.GetScalarValue()
},
{
"minimum",
(o, n, t) => GetOrCreateSchema(o).Minimum = ParserHelper.ParseDecimalWithFallbackOnOverflow(n.GetScalarValue(), decimal.MinValue)
},
{
"maximum",
(o, n, t) => GetOrCreateSchema(o).Maximum = ParserHelper.ParseDecimalWithFallbackOnOverflow(n.GetScalarValue(), decimal.MaxValue)
},
{
"maxLength",
(o, n, t) => GetOrCreateSchema(o).MaxLength = int.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture)
},
{
"minLength",
(o, n, t) => GetOrCreateSchema(o).MinLength = int.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture)
},
{
"readOnly",
(o, n, t) => GetOrCreateSchema(o).ReadOnly = bool.Parse(n.GetScalarValue())
},
{
"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 = (bool)node.Context.GetFromTempStorage<object>(TempStorageKeys.ParameterIsBodyOrFormData);
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;
}
}
}