-
Notifications
You must be signed in to change notification settings - Fork 232
/
OpenApiAnyConverter.cs
270 lines (236 loc) · 9.76 KB
/
OpenApiAnyConverter.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Globalization;
using System.Linq;
using System.Text;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
namespace Microsoft.OpenApi.Readers.ParseNodes
{
internal static class OpenApiAnyConverter
{
/// <summary>
/// Converts the <see cref="OpenApiString"/>s in the given <see cref="IOpenApiAny"/>
/// into the appropriate <see cref="IOpenApiPrimitive"/> type based on the given <see cref="OpenApiSchema"/>.
/// For those strings that the schema does not specify the type for, convert them into
/// the most specific type based on the value.
/// </summary>
public static IOpenApiAny GetSpecificOpenApiAny(IOpenApiAny openApiAny, OpenApiSchema schema = null)
{
if (openApiAny is OpenApiArray openApiArray)
{
var newArray = new OpenApiArray();
foreach (var element in openApiArray)
{
newArray.Add(GetSpecificOpenApiAny(element, schema?.Items));
}
return newArray;
}
if (openApiAny is OpenApiObject openApiObject)
{
var newObject = new OpenApiObject();
foreach (var key in openApiObject.Keys.ToList())
{
if (schema?.Properties != null && schema.Properties.TryGetValue(key, out var property))
{
newObject[key] = GetSpecificOpenApiAny(openApiObject[key], property);
}
else
{
newObject[key] = GetSpecificOpenApiAny(openApiObject[key], schema?.AdditionalProperties);
}
}
return newObject;
}
if (openApiAny is not OpenApiString apiString)
{
return openApiAny;
}
var value = apiString.Value;
var type = schema?.Type;
var format = schema?.Format;
if (apiString.IsExplicit())
{
// More narrow type detection for explicit strings, only check types that are passed as strings
if (schema == null)
{
if (DateTimeOffset.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.None, out var dateTimeValue))
{
// if the time component is exactly midnight(00:00:00) meaning no time has elapsed, return a date-only value
return dateTimeValue.TimeOfDay == TimeSpan.Zero ? new OpenApiDate(dateTimeValue.Date)
: new OpenApiDateTime(dateTimeValue);
}
}
else if (type == "string")
{
if (format == "byte")
{
try
{
return new OpenApiByte(Convert.FromBase64String(value));
}
catch (FormatException)
{ }
}
if (format == "binary")
{
try
{
return new OpenApiBinary(Encoding.UTF8.GetBytes(value));
}
catch (EncoderFallbackException)
{ }
}
if (format == "date")
{
if (DateTimeOffset.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.None, out var dateValue))
{
return new OpenApiDate(dateValue.Date);
}
}
if (format == "date-time")
{
if (DateTimeOffset.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.None, out var dateTimeValue))
{
return new OpenApiDateTime(dateTimeValue);
}
}
if (format == "password")
{
return new OpenApiPassword(value);
}
}
return apiString;
}
if (value is null or "null")
{
return new OpenApiNull();
}
if (schema?.Type == null)
{
if (value == "true")
{
return new OpenApiBoolean(true);
}
if (value == "false")
{
return new OpenApiBoolean(false);
}
if (int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intValue))
{
return new OpenApiInteger(intValue);
}
if (long.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var longValue))
{
return new OpenApiLong(longValue);
}
if (double.TryParse(value, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out var doubleValue))
{
return new OpenApiDouble(doubleValue);
}
if (DateTimeOffset.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.None, out var dateTimeValue))
{
return new OpenApiDateTime(dateTimeValue);
}
}
else
{
if (type == "integer" && format == "int32")
{
if (int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intValue))
{
return new OpenApiInteger(intValue);
}
}
if (type == "integer" && format == "int64")
{
if (long.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var longValue))
{
return new OpenApiLong(longValue);
}
}
if (type == "integer")
{
if (int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intValue))
{
return new OpenApiInteger(intValue);
}
}
if (type == "number" && format == "float")
{
if (float.TryParse(value, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out var floatValue))
{
return new OpenApiFloat(floatValue);
}
}
if (type == "number" && format == "double")
{
if (double.TryParse(value, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out var doubleValue))
{
return new OpenApiDouble(doubleValue);
}
}
if (type == "number")
{
if (double.TryParse(value, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out var doubleValue))
{
return new OpenApiDouble(doubleValue);
}
}
if (type == "string" && format == "byte")
{
try
{
return new OpenApiByte(Convert.FromBase64String(value));
}
catch (FormatException)
{ }
}
// binary
if (type == "string" && format == "binary")
{
try
{
return new OpenApiBinary(Encoding.UTF8.GetBytes(value));
}
catch (EncoderFallbackException)
{ }
}
if (type == "string" && format == "date")
{
if (DateTimeOffset.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.None, out var dateValue))
{
return new OpenApiDate(dateValue.Date);
}
}
if (type == "string" && format == "date-time")
{
if (DateTimeOffset.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.None, out var dateTimeValue))
{
return new OpenApiDateTime(dateTimeValue);
}
}
if (type == "string" && format == "password")
{
return new OpenApiPassword(value);
}
if (type == "string")
{
return apiString;
}
if (type == "boolean")
{
if (bool.TryParse(value, out var booleanValue))
{
return new OpenApiBoolean(booleanValue);
}
}
}
// If data conflicts with the given type, return a string.
// This converter is used in the parser, so it does not perform any validations,
// but the validator can be used to validate whether the data and given type conflicts.
return apiString;
}
}
}