-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathOpenApiSchemaExtensions.cs
299 lines (280 loc) · 16.5 KB
/
OpenApiSchemaExtensions.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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
namespace Kiota.Builder.Extensions;
public static class OpenApiSchemaExtensions
{
private static readonly Func<OpenApiSchema, IList<OpenApiSchema>> classNamesFlattener = x =>
(x.AnyOf ?? Enumerable.Empty<OpenApiSchema>()).Union(x.AllOf).Union(x.OneOf).ToList();
public static IEnumerable<string> GetSchemaNames(this OpenApiSchema schema, bool directOnly = false)
{
if (schema == null)
return [];
if (!directOnly && schema.Items != null)
return schema.Items.GetSchemaNames();
if (!string.IsNullOrEmpty(schema.Reference?.Id))
return [schema.Reference.Id.Split('/')[^1].Split('.')[^1]];
if (!directOnly && schema.AnyOf.Any())
return schema.AnyOf.FlattenIfRequired(classNamesFlattener);
if (!directOnly && schema.AllOf.Any())
return schema.AllOf.FlattenIfRequired(classNamesFlattener);
if (!directOnly && schema.OneOf.Any())
return schema.OneOf.FlattenIfRequired(classNamesFlattener);
return [];
}
internal static IEnumerable<OpenApiSchema> FlattenSchemaIfRequired(this IList<OpenApiSchema> schemas, Func<OpenApiSchema, IList<OpenApiSchema>> subsequentGetter)
{
if (schemas is null) return [];
return schemas.Count == 1 && !schemas[0].HasAnyProperty() && string.IsNullOrEmpty(schemas[0].Reference?.Id) ?
schemas.FlattenEmptyEntries(subsequentGetter, 1) :
schemas;
}
private static IEnumerable<string> FlattenIfRequired(this IList<OpenApiSchema> schemas, Func<OpenApiSchema, IList<OpenApiSchema>> subsequentGetter)
{
return schemas.FlattenSchemaIfRequired(subsequentGetter).SelectMany(static x => x.GetSchemaNames());
}
public static string GetSchemaName(this OpenApiSchema schema, bool directOnly = false)
{
return schema.GetSchemaNames(directOnly).LastOrDefault()?.TrimStart('$') ?? string.Empty;// OData $ref
}
public static bool IsReferencedSchema(this OpenApiSchema schema)
{
var isReference = schema?.Reference != null;
if (isReference && schema!.Reference.IsExternal)
throw new NotSupportedException("External references are not supported in this version of Kiota. While Kiota awaits on OpenAPI.Net to support inlining external references, you can use https://www.nuget.org/packages/Microsoft.OpenApi.Hidi to generate an OpenAPI description with inlined external references and then use this new reference with Kiota.");
return isReference;
}
public static bool IsArray(this OpenApiSchema? schema)
{
return "array".Equals(schema?.Type, StringComparison.OrdinalIgnoreCase) && schema.Items != null &&
(schema.Items.IsComposedEnum() ||
schema.Items.IsEnum() ||
schema.Items.IsSemanticallyMeaningful() ||
FlattenEmptyEntries([schema.Items], static x => x.AnyOf.Union(x.AllOf).Union(x.OneOf).ToList(), 1).FirstOrDefault() is OpenApiSchema flat && flat.IsSemanticallyMeaningful());
}
public static bool IsObjectType(this OpenApiSchema? schema)
{
return "object".Equals(schema?.Type, StringComparison.OrdinalIgnoreCase);
}
public static bool HasAnyProperty(this OpenApiSchema? schema)
{
return schema?.Properties is { Count: > 0 };
}
public static bool IsInclusiveUnion(this OpenApiSchema? schema)
{
return schema?.AnyOf?.Count(static x => IsSemanticallyMeaningful(x, true)) > 1;
// so we don't consider any of object/nullable as a union type
}
public static bool IsInherited(this OpenApiSchema? schema)
{
if (schema is null) return false;
var meaningfulMemberSchemas = schema.AllOf.FlattenSchemaIfRequired(static x => x.AllOf).Where(static x => x.IsSemanticallyMeaningful(ignoreEnums: true, ignoreArrays: true, ignoreType: true)).ToArray();
var isRootSchemaMeaningful = schema.IsSemanticallyMeaningful(ignoreEnums: true, ignoreArrays: true, ignoreType: true);
return meaningfulMemberSchemas.Count(static x => !string.IsNullOrEmpty(x.Reference?.Id)) == 1 &&
(meaningfulMemberSchemas.Count(static x => string.IsNullOrEmpty(x.Reference?.Id)) == 1 ||
isRootSchemaMeaningful);
}
internal static OpenApiSchema? MergeAllOfSchemaEntries(this OpenApiSchema? schema, HashSet<OpenApiSchema>? schemasToExclude = default, Func<OpenApiSchema, bool>? filter = default)
{
return schema.MergeIntersectionSchemaEntries(schemasToExclude, true, filter);
}
internal static OpenApiSchema? MergeIntersectionSchemaEntries(this OpenApiSchema? schema, HashSet<OpenApiSchema>? schemasToExclude = default, bool overrideIntersection = false, Func<OpenApiSchema, bool>? filter = default)
{
if (schema is null) return null;
if (!schema.IsIntersection() && !overrideIntersection) return schema;
var result = new OpenApiSchema(schema);
result.AllOf.Clear();
var meaningfulSchemas = schema.AllOf
.Where(x => (x.IsSemanticallyMeaningful() || x.AllOf.Any()) && (filter == null || filter(x)))
.Select(x => MergeIntersectionSchemaEntries(x, schemasToExclude, overrideIntersection, filter))
.Where(x => x is not null && (schemasToExclude is null || !schemasToExclude.Contains(x)))
.OfType<OpenApiSchema>()
.ToArray();
var entriesToMerge = meaningfulSchemas.FlattenEmptyEntries(static x => x.AllOf).Union(meaningfulSchemas).ToArray();
if (entriesToMerge.Select(static x => x.Discriminator).OfType<OpenApiDiscriminator>().FirstOrDefault() is OpenApiDiscriminator discriminator)
if (result.Discriminator is null)
result.Discriminator = discriminator;
else if (string.IsNullOrEmpty(result.Discriminator.PropertyName) && !string.IsNullOrEmpty(discriminator.PropertyName))
result.Discriminator.PropertyName = discriminator.PropertyName;
else if (discriminator.Mapping?.Any() ?? false)
result.Discriminator.Mapping = discriminator.Mapping.ToDictionary(static x => x.Key, static x => x.Value);
foreach (var propertyToMerge in entriesToMerge.SelectMany(static x => x.Properties))
{
result.Properties.TryAdd(propertyToMerge.Key, propertyToMerge.Value);
}
return result;
}
public static bool IsIntersection(this OpenApiSchema? schema)
{
var meaningfulSchemas = schema?.AllOf?.Where(static x => x.IsSemanticallyMeaningful()).ToArray();
return meaningfulSchemas?.Count(static x => !string.IsNullOrEmpty(x.Reference?.Id)) > 1 || meaningfulSchemas?.Count(static x => string.IsNullOrEmpty(x.Reference?.Id)) > 1;
}
public static bool IsExclusiveUnion(this OpenApiSchema? schema)
{
return schema?.OneOf?.Count(static x => IsSemanticallyMeaningful(x, true)) > 1;
// so we don't consider one of object/nullable as a union type
}
private static readonly HashSet<string> oDataTypes = new(StringComparer.OrdinalIgnoreCase) {
"number",
"integer",
};
private static bool IsODataPrimitiveTypeBackwardCompatible(this OpenApiSchema schema)
{
return schema.IsExclusiveUnion() &&
schema.OneOf.Count == 3 &&
schema.OneOf.Count(static x => x.Enum?.Any() ?? false) == 1 &&
schema.OneOf.Count(static x => oDataTypes.Contains(x.Type)) == 1 &&
schema.OneOf.Count(static x => "string".Equals(x.Type, StringComparison.OrdinalIgnoreCase)) == 1
||
schema.IsInclusiveUnion() &&
schema.AnyOf.Count == 3 &&
schema.AnyOf.Count(static x => x.Enum?.Any() ?? false) == 1 &&
schema.AnyOf.Count(static x => oDataTypes.Contains(x.Type)) == 1 &&
schema.AnyOf.Count(static x => "string".Equals(x.Type, StringComparison.OrdinalIgnoreCase)) == 1;
}
public static bool IsODataPrimitiveType(this OpenApiSchema schema)
{
return schema.IsExclusiveUnion() &&
schema.OneOf.Count == 3 &&
schema.OneOf.Count(static x => "string".Equals(x.Type, StringComparison.OrdinalIgnoreCase) && (x.Enum?.Any() ?? false)) == 1 &&
schema.OneOf.Count(static x => oDataTypes.Contains(x.Type)) == 1 &&
schema.OneOf.Count(static x => "string".Equals(x.Type, StringComparison.OrdinalIgnoreCase)) == 2
||
schema.IsInclusiveUnion() &&
schema.AnyOf.Count == 3 &&
schema.AnyOf.Count(static x => "string".Equals(x.Type, StringComparison.OrdinalIgnoreCase) && (x.Enum?.Any() ?? false)) == 1 &&
schema.AnyOf.Count(static x => oDataTypes.Contains(x.Type)) == 1 &&
schema.AnyOf.Count(static x => "string".Equals(x.Type, StringComparison.OrdinalIgnoreCase)) == 2
||
schema.IsODataPrimitiveTypeBackwardCompatible();
}
public static bool IsEnum(this OpenApiSchema schema)
{
if (schema is null) return false;
return schema.Enum.OfType<OpenApiString>().Any(static x => !string.IsNullOrEmpty(x.Value)) &&
(string.IsNullOrEmpty(schema.Type) || "string".Equals(schema.Type, StringComparison.OrdinalIgnoreCase)); // number and boolean enums are not supported
}
public static bool IsComposedEnum(this OpenApiSchema schema)
{
if (schema is null) return false;
return schema.AnyOf.Count(static x => !x.IsSemanticallyMeaningful(true)) == 1 && schema.AnyOf.Count(static x => x.IsEnum()) == 1 ||
schema.OneOf.Count(static x => !x.IsSemanticallyMeaningful(true)) == 1 && schema.OneOf.Count(static x => x.IsEnum()) == 1;
}
public static bool IsSemanticallyMeaningful(this OpenApiSchema schema, bool ignoreNullableObjects = false, bool ignoreEnums = false, bool ignoreArrays = false, bool ignoreType = false)
{
if (schema is null) return false;
return schema.HasAnyProperty() ||
(!ignoreEnums && schema.Enum is { Count: > 0 }) ||
(!ignoreArrays && schema.Items != null) ||
(!ignoreType && !string.IsNullOrEmpty(schema.Type) &&
((ignoreNullableObjects && !"object".Equals(schema.Type, StringComparison.OrdinalIgnoreCase)) ||
!ignoreNullableObjects)) ||
!string.IsNullOrEmpty(schema.Format) ||
!string.IsNullOrEmpty(schema.Reference?.Id);
}
public static IEnumerable<string> GetSchemaReferenceIds(this OpenApiSchema schema, HashSet<OpenApiSchema>? visitedSchemas = null)
{
visitedSchemas ??= new();
if (schema != null && !visitedSchemas.Contains(schema))
{
visitedSchemas.Add(schema);
var result = new List<string>();
if (!string.IsNullOrEmpty(schema.Reference?.Id))
result.Add(schema.Reference.Id);
if (schema.Items != null)
{
if (!string.IsNullOrEmpty(schema.Items.Reference?.Id))
result.Add(schema.Items.Reference.Id);
result.AddRange(schema.Items.GetSchemaReferenceIds(visitedSchemas));
}
var subSchemaReferences = (schema.Properties?.Values ?? Enumerable.Empty<OpenApiSchema>())
.Union(schema.AnyOf ?? Enumerable.Empty<OpenApiSchema>())
.Union(schema.AllOf ?? Enumerable.Empty<OpenApiSchema>())
.Union(schema.OneOf ?? Enumerable.Empty<OpenApiSchema>())
.SelectMany(x => x.GetSchemaReferenceIds(visitedSchemas))
.ToList();// this to list is important otherwise the any marks the schemas as visited and add range doesn't find anything
if (subSchemaReferences.Count != 0)
result.AddRange(subSchemaReferences);
return result.Distinct();
}
return [];
}
private static IEnumerable<OpenApiSchema> FlattenEmptyEntries(this IEnumerable<OpenApiSchema> schemas, Func<OpenApiSchema, IList<OpenApiSchema>> subsequentGetter, int? maxDepth = default)
{
if (schemas == null) return [];
ArgumentNullException.ThrowIfNull(subsequentGetter);
if ((maxDepth ?? 1) <= 0)
return schemas;
var result = schemas.ToList();
var permutations = new Dictionary<OpenApiSchema, IEnumerable<OpenApiSchema>>();
foreach (var item in result)
{
var subsequentItems = subsequentGetter(item);
if (subsequentItems.Any())
permutations.Add(item, subsequentItems.FlattenEmptyEntries(subsequentGetter, maxDepth.HasValue ? --maxDepth : default));
}
if (permutations.Count > 0)
{
foreach (var permutation in permutations)
{
var index = result.IndexOf(permutation.Key);
result.RemoveAt(index);
var offset = 0;
foreach (var insertee in permutation.Value)
{
result.Insert(index + offset, insertee);
offset++;
}
}
}
return result;
}
internal static string GetDiscriminatorPropertyName(this OpenApiSchema schema)
{
if (schema == null)
return string.Empty;
if (!string.IsNullOrEmpty(schema.Discriminator?.PropertyName))
return schema.Discriminator.PropertyName;
if (schema.OneOf.Select(GetDiscriminatorPropertyName).FirstOrDefault(static x => !string.IsNullOrEmpty(x)) is string oneOfDiscriminatorPropertyName)
return oneOfDiscriminatorPropertyName;
if (schema.AnyOf.Select(GetDiscriminatorPropertyName).FirstOrDefault(static x => !string.IsNullOrEmpty(x)) is string anyOfDiscriminatorPropertyName)
return anyOfDiscriminatorPropertyName;
if (schema.AllOf.Select(GetDiscriminatorPropertyName).FirstOrDefault(static x => !string.IsNullOrEmpty(x)) is string allOfDiscriminatorPropertyName)
return allOfDiscriminatorPropertyName;
return string.Empty;
}
internal static IEnumerable<KeyValuePair<string, string>> GetDiscriminatorMappings(this OpenApiSchema schema, ConcurrentDictionary<string, ConcurrentDictionary<string, bool>> inheritanceIndex)
{
if (schema == null)
return Enumerable.Empty<KeyValuePair<string, string>>();
if (!(schema.Discriminator?.Mapping?.Any() ?? false))
if (schema.OneOf.Any())
return schema.OneOf.SelectMany(x => GetDiscriminatorMappings(x, inheritanceIndex));
else if (schema.AnyOf.Any())
return schema.AnyOf.SelectMany(x => GetDiscriminatorMappings(x, inheritanceIndex));
else if (schema.AllOf.Any(allOfEvaluatorForMappings) && schema.AllOf[^1].Equals(schema.AllOf.Last(allOfEvaluatorForMappings)))
// ensure the matched AllOf entry is the last in the list
return GetDiscriminatorMappings(schema.AllOf.Last(allOfEvaluatorForMappings), inheritanceIndex);
else if (!string.IsNullOrEmpty(schema.Reference?.Id))
return GetAllInheritanceSchemaReferences(schema.Reference.Id, inheritanceIndex)
.Where(static x => !string.IsNullOrEmpty(x))
.Select(x => KeyValuePair.Create(x, x))
.Union(new[] { KeyValuePair.Create(schema.Reference.Id, schema.Reference.Id) });
else
return Enumerable.Empty<KeyValuePair<string, string>>();
return schema.Discriminator
.Mapping;
}
private static readonly Func<OpenApiSchema, bool> allOfEvaluatorForMappings = static x => x.Discriminator?.Mapping.Any() ?? false;
private static IEnumerable<string> GetAllInheritanceSchemaReferences(string currentReferenceId, ConcurrentDictionary<string, ConcurrentDictionary<string, bool>> inheritanceIndex)
{
ArgumentException.ThrowIfNullOrEmpty(currentReferenceId);
ArgumentNullException.ThrowIfNull(inheritanceIndex);
if (inheritanceIndex.TryGetValue(currentReferenceId, out var dependents))
return dependents.Keys.Union(dependents.Keys.SelectMany(x => GetAllInheritanceSchemaReferences(x, inheritanceIndex))).Distinct(StringComparer.OrdinalIgnoreCase);
return [];
}
}