-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathEdmModelHelper.cs
More file actions
502 lines (439 loc) · 24.3 KB
/
EdmModelHelper.cs
File metadata and controls
502 lines (439 loc) · 24.3 KB
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.OData.Edm;
using Microsoft.OData.Edm.Csdl;
using Microsoft.OData.Edm.Vocabularies;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Models.Interfaces;
using Microsoft.OpenApi.Models.References;
using Microsoft.OpenApi.OData.Edm;
using Microsoft.OpenApi.OData.Vocabulary.Capabilities;
namespace Microsoft.OpenApi.OData.Common
{
internal static class EdmModelHelper
{
/// <summary>
/// Adds the derived types references together with their base type reference in the OneOf property of an OpenAPI schema.
/// </summary>
/// <returns>The OpenAPI schema with the list of derived types references and their base type references set in the OneOf property.</returns>
internal static OpenApiSchema? GetDerivedTypesReferenceSchema(IEdmStructuredType structuredType, IEdmModel edmModel, OpenApiDocument document)
{
Utils.CheckArgumentNull(structuredType, nameof(structuredType));
Utils.CheckArgumentNull(edmModel, nameof(edmModel));
if (structuredType is not IEdmSchemaElement schemaElement) throw new ArgumentException("The type is not a schema element.", nameof(structuredType));
IEnumerable<IEdmSchemaElement> derivedTypes = edmModel.FindAllDerivedTypes(structuredType).OfType<IEdmSchemaElement>();
if (!derivedTypes.Any())
{
return null;
}
OpenApiSchema schema = new()
{
OneOf = []
};
var baseTypeSchema = new OpenApiSchemaReference(schemaElement.FullName(), document);
schema.OneOf.Add(baseTypeSchema);
foreach (IEdmSchemaElement derivedType in derivedTypes)
{
var derivedTypeSchema = new OpenApiSchemaReference(derivedType.FullName(), document);
schema.OneOf.Add(derivedTypeSchema);
}
return schema;
}
/// <summary>
/// Verifies whether the provided navigation restrictions allow for navigability of a navigation property.
/// </summary>
/// <param name="restrictionType">The <see cref="NavigationRestrictionsType"/>.</param>
/// <param name="restrictionProperty">The <see cref="NavigationPropertyRestriction"/>.</param>
/// <returns>true, if navigability is allowed, otherwise false.</returns>
internal static bool NavigationRestrictionsAllowsNavigability(
NavigationRestrictionsType? restrictionType,
NavigationPropertyRestriction? restrictionProperty)
{
// Verify using individual navigation restriction first
if (restrictionProperty?.Navigability != null && restrictionProperty.Navigability.Value == NavigationType.None)
{
return false;
}
// if the individual has no navigability setting, use the global navigability setting
// Default navigability for all navigation properties of the annotation target.
// Individual navigation properties can override this value via `RestrictedProperties/Navigability`.
return restrictionProperty?.Navigability != null || restrictionType == null || restrictionType.IsNavigable;
}
/// <summary>
/// Generates the operation id from a navigation property path.
/// </summary>
/// <param name="path">The target <see cref="ODataPath"/>.</param>
/// <param name="context">The OData context.</param>
/// <param name="prefix">Optional: Identifier indicating whether it is a collection-valued non-indexed or single-valued navigation property.</param>
/// <returns>The operation id generated from the given navigation property path.</returns>
internal static string? GenerateNavigationPropertyPathOperationId(ODataPath path, ODataContext context, string? prefix = null)
{
IList<string> items = RetrieveNavigationPropertyPathsOperationIdSegments(path, context);
if (!items.Any())
return null;
int lastItemIndex = items[^1].StartsWith('-') ? items.Count - 2 : items.Count - 1;
if (!string.IsNullOrEmpty(prefix))
{
items[lastItemIndex] = prefix + Utils.UpperFirstChar(items[lastItemIndex]);
}
else if (Utils.UpperFirstChar(items[lastItemIndex]) is string lastIdentifier)
{
items[lastItemIndex] = lastIdentifier;
}
return GenerateNavigationPropertyPathOperationId(items);
}
/// <summary>
/// Generates the operation id from a complex property path.
/// </summary>
/// <param name="path">The target <see cref="ODataPath"/>.</param>
/// <param name="context">The OData context.</param>
/// <param name="prefix">Optional: Identifier indicating whether it is a collection-valued or single-valued complex property.</param>
/// <returns>The operation id generated from the given complex property path.</returns>
internal static string? GenerateComplexPropertyPathOperationId(ODataPath path, ODataContext context, string? prefix = null)
{
IList<string> items = RetrieveNavigationPropertyPathsOperationIdSegments(path, context);
if (!items.Any())
return null;
if (path.Segments.Skip(1).OfType<ODataComplexPropertySegment>()?.Last()?.Identifier is string lastSegmentIdentifier)
if (!string.IsNullOrEmpty(prefix))
{
items.Add(prefix + Utils.UpperFirstChar(lastSegmentIdentifier));
}
else if (Utils.UpperFirstChar(lastSegmentIdentifier) is string lastIdentifier)
{
items.Add(lastIdentifier);
}
return GenerateNavigationPropertyPathOperationId(items);
}
/// <summary>
/// Generates a navigation property operation id from a list of string values.
/// </summary>
/// <param name="items">The list of string values.</param>
/// <returns>The generated navigation property operation id.</returns>
private static string? GenerateNavigationPropertyPathOperationId(IList<string> items)
{
if (!items.Any())
return null;
return string.Join(".", items).Replace(".-", "-", StringComparison.OrdinalIgnoreCase); // Format any hashed value appropriately (this will be the last value)
}
/// <summary>
/// Retrieves the segments of an operation id generated from a navigation property path.
/// </summary>
/// <param name="path">The target <see cref="ODataPath"/>.</param>
/// <param name="context">The OData context.</param>
/// <returns>The segments of an operation id generated from the given navigation property path.</returns>
internal static IList<string> RetrieveNavigationPropertyPathsOperationIdSegments(ODataPath path, ODataContext context)
{
Utils.CheckArgumentNull(path, nameof(path));
if (path.FirstSegment is not ODataNavigationSourceSegment {NavigationSource: IEdmNavigationSource navigationSource})
throw new InvalidOperationException("The first segment of the path is not a navigation source segment.");
var items = new List<string>
{
navigationSource.Name
};
// For navigation property paths with odata type cast segments
// the OData type cast segments identifiers will be used in the operation id
// The same applies for navigation property paths with operation segments.
IEnumerable<ODataSegment> segments = path.Segments.Skip(1)
.Where(static s =>
s is ODataNavigationPropertySegment ||
s is ODataTypeCastSegment ||
s is ODataOperationSegment ||
s is ODataKeySegment);
Utils.CheckArgumentNull(segments, nameof(segments));
string? previousTypeCastSegmentId = null;
string pathHash = string.Empty;
foreach (var segment in segments)
{
if (segment is ODataNavigationPropertySegment navPropSegment)
{
items.Add(navPropSegment.NavigationProperty.Name);
}
else if (segment is ODataTypeCastSegment typeCastSegment
&& path.Kind != ODataPathKind.TypeCast // ex: ~/NavSource/NavProp/TypeCast
&& !(path.Kind == ODataPathKind.DollarCount && path.Segments[path.Segments.Count - 2]?.Kind == ODataSegmentKind.TypeCast)) // ex: ~/NavSource/NavProp/TypeCast/$count
{
// Only the last OData type cast segment identifier is added to the operation id
if (!string.IsNullOrEmpty(previousTypeCastSegmentId))
items.Remove(previousTypeCastSegmentId);
if (typeCastSegment.StructuredType is IEdmSchemaElement schemaElement)
{
previousTypeCastSegmentId = "As" + Utils.UpperFirstChar(schemaElement.Name);
items.Add(previousTypeCastSegmentId);
}
}
else if (segment is ODataOperationSegment operationSegment && !string.IsNullOrEmpty(operationSegment.Identifier))
{
// Navigation property generated via composable function
if (operationSegment.Operation is IEdmFunction function && context.Model.IsOperationOverload(function))
{
// Hash the segment to avoid duplicate operationIds
pathHash = string.IsNullOrEmpty(pathHash)
? operationSegment.GetPathHash(context.Settings)
: (pathHash + operationSegment.GetPathHash(context.Settings)).GetHashSHA256()[..4];
}
items.Add(operationSegment.Identifier);
}
else if (segment is ODataKeySegment keySegment && keySegment.IsAlternateKey)
{
// We'll consider alternate keys in the operation id to eliminate potential duplicates with operation id of primary path
if (segment == segments.Last())
{
items.Add("By" + string.Join("", keySegment.Identifier.Split(',').Select(static x => Utils.UpperFirstChar(x))));
}
else
{
items.Add(keySegment.Identifier);
}
}
}
if (!string.IsNullOrEmpty(pathHash))
{
items.Add("-" + pathHash);
}
return items;
}
/// <summary>
/// Generates the tag name from a navigation property path.
/// </summary>
/// <param name="path">The target <see cref="ODataPath"/>.</param>
/// <param name="context">The <see cref="ODataContext"/>.</param>
/// <returns>The tag name generated from the given navigation property path.</returns>
internal static string GenerateNavigationPropertyPathTagName(ODataPath path, ODataContext context)
{
Utils.CheckArgumentNull(path, nameof(path));
Utils.CheckArgumentNull(context, nameof(context));
if (path.FirstSegment is not ODataNavigationSourceSegment {NavigationSource: IEdmNavigationSource navigationSource })
throw new InvalidOperationException("The first segment of the path is not a navigation source segment.");
var items = new List<string>
{
navigationSource.Name
};
if (path.OfType<ODataNavigationPropertySegment>()?.Last()?.NavigationProperty is not IEdmNavigationProperty navigationProperty)
throw new InvalidOperationException("The last segment of the path is not a navigation property segment.");
foreach (var segment in path.Segments.Skip(1).OfType<ODataNavigationPropertySegment>().Select(static x => x.NavigationProperty))
{
if (segment == navigationProperty)
{
items.Add(navigationProperty.ToEntityType().Name);
break;
}
else
{
if (items.Count >= context.Settings.TagDepth - 1)
{
items.Add(segment.ToEntityType().Name);
break;
}
else
{
items.Add(segment.Name);
}
}
}
return string.Join(".", items);
}
/// <summary>
/// Generates the tag name from a complex property path.
/// </summary>
/// <param name="path">The target <see cref="ODataPath"/>.</param>
/// <param name="context">The <see cref="ODataContext"/>.</param>
/// <returns>The tag name generated from the given complex property path.</returns>
internal static string GenerateComplexPropertyPathTagName(ODataPath path, ODataContext context)
{
Utils.CheckArgumentNull(path, nameof(path));
Utils.CheckArgumentNull(context, nameof(context));
if (path.Segments.OfType<ODataComplexPropertySegment>()?.Last() is not ODataComplexPropertySegment complexSegment)
throw new InvalidOperationException("The last segment of the path is not a complex property segment.");
// Get the segment before the last complex type segment
int complexSegmentIndex = path.Segments.IndexOf(complexSegment);
ODataSegment preComplexSegment = path.Segments[complexSegmentIndex - 1];
int preComplexSegmentIndex = path.Segments.IndexOf(preComplexSegment);
while (preComplexSegment is ODataTypeCastSegment)
{
// Skip this segment,
// Tag names don't include OData type cast segment identifiers
preComplexSegmentIndex--;
preComplexSegment = path.Segments[preComplexSegmentIndex];
}
string? tagName = null;
if (preComplexSegment is ODataNavigationSourceSegment sourceSegment)
{
tagName = $"{sourceSegment.NavigationSource.Name}";
}
else if (preComplexSegment is ODataNavigationPropertySegment)
{
tagName = GenerateNavigationPropertyPathTagName(path, context);
}
else if (preComplexSegment is ODataKeySegment)
{
var prevKeySegment = path.Segments[preComplexSegmentIndex - 1];
if (prevKeySegment is ODataNavigationPropertySegment)
{
tagName = GenerateNavigationPropertyPathTagName(path, context);
}
else if (prevKeySegment is ODataNavigationSourceSegment sourceSegment1)
{
tagName = $"{sourceSegment1.NavigationSource.Name}";
}
}
List<string> tagNameItems = tagName?.Split('.').ToList() ?? [];
if (tagNameItems.Count < context.Settings.TagDepth && complexSegment.ComplexType is not null)
{
tagNameItems.Add(complexSegment.ComplexType.Name);
}
return string.Join(".", tagNameItems);
}
/// <summary>
/// Generates the operation id prefix from an OData type cast path.
/// </summary>
/// <param name="path">The target <see cref="ODataPath"/>.</param>
/// <param name="context">The OData context.</param>
/// <param name="includeListOrGetPrefix">Optional: Whether to include the List or Get prefix to the generated operation id.</param>
/// <returns>The operation id prefix generated from the OData type cast path.</returns>
internal static string? GenerateODataTypeCastPathOperationIdPrefix(ODataPath path, ODataContext context, bool includeListOrGetPrefix = true)
{
// Get the segment before the last OData type cast segment
if (path.Segments.OfType<ODataTypeCastSegment>()?.Last() is not ODataTypeCastSegment typeCastSegment)
throw new InvalidOperationException("The last segment of the path is not a type cast segment.");
int typeCastSegmentIndex = path.Segments.IndexOf(typeCastSegment);
// The segment 1 place before the last OData type cast segment
ODataSegment secondLastSegment = path.Segments[typeCastSegmentIndex - 1];
bool isIndexedCollValuedNavProp = false;
if (secondLastSegment is ODataKeySegment)
{
// The segment 2 places before the last OData type cast segment
ODataSegment thirdLastSegment = path.Segments[typeCastSegmentIndex - 2];
if (thirdLastSegment is ODataNavigationPropertySegment)
{
isIndexedCollValuedNavProp = true;
}
}
ODataNavigationSourceSegment? navigationSourceSegment = path.FirstSegment as ODataNavigationSourceSegment;
IEdmSingleton? singleton = navigationSourceSegment?.NavigationSource as IEdmSingleton;
IEdmEntitySet? entitySet = navigationSourceSegment?.NavigationSource as IEdmEntitySet;
string? operationId = null;
if (secondLastSegment is ODataComplexPropertySegment complexSegment)
{
string? listOrGet = includeListOrGetPrefix ? (complexSegment.Property.Type.IsCollection() ? "List" : "Get") : null;
operationId = GenerateComplexPropertyPathOperationId(path, context, listOrGet);
}
else if (secondLastSegment is ODataNavigationPropertySegment navPropSegment)
{
string? prefix = null;
if (includeListOrGetPrefix)
{
prefix = navPropSegment?.NavigationProperty.TargetMultiplicity() == EdmMultiplicity.Many ? "List" : "Get";
}
operationId = GenerateNavigationPropertyPathOperationId(path, context, prefix);
}
else if (secondLastSegment is ODataKeySegment keySegment)
{
if (isIndexedCollValuedNavProp)
{
operationId = GenerateNavigationPropertyPathOperationId(path, context, "Get");
}
else
{
string entityTypeName = keySegment.EntityType.Name;
string? getPrefix = includeListOrGetPrefix ? "Get" : null;
string operationName = $"{getPrefix}{Utils.UpperFirstChar(entityTypeName)}";
if (keySegment.IsAlternateKey)
{
string alternateKeyName = string.Join("", keySegment.Identifier.Split(',').Select(static x => Utils.UpperFirstChar(x)));
operationName = $"{operationName}By{alternateKeyName}";
}
if (entitySet != null)
{
operationId = entitySet.Name;
}
else if (singleton != null)
{
operationId = singleton.Name;
}
operationId += $".{entityTypeName}.{operationName}";
}
}
else if (secondLastSegment is ODataNavigationSourceSegment)
{
if (entitySet != null)
{
operationId = entitySet.Name + "." + entitySet.EntityType.Name + $".{(includeListOrGetPrefix ? "List" : null)}" + Utils.UpperFirstChar(entitySet.EntityType.Name);
}
else if (singleton != null)
{
operationId = singleton.Name + "." + singleton.EntityType.Name + $".{(includeListOrGetPrefix ? "Get" : null)}" + Utils.UpperFirstChar(singleton.EntityType.Name);
}
}
return operationId;
}
/// <summary>
/// Strips or aliases namespace prefixes from an element name.
/// </summary>
/// <param name="element">The target element.</param>
/// <param name="model">Optional: The Edm model. Used for searching for the namespace alias.</param>
/// <param name="settings">The OpenAPI convert settings.</param>
/// <returns>The element name, alias-prefixed or namespace-stripped if applicable.</returns>
internal static string StripOrAliasNamespacePrefix(IEdmSchemaElement element, OpenApiConvertSettings settings, IEdmModel? model = null)
{
Utils.CheckArgumentNull(element, nameof(element));
Utils.CheckArgumentNull(settings, nameof(settings));
string namespaceAlias = string.Empty;
string namespaceName = element.Namespace;
string segmentName = element.FullName();
if (!string.IsNullOrEmpty(namespaceName) && model != null)
{
namespaceAlias = model.GetNamespaceAlias(namespaceName);
}
if (element is IEdmStructuredType && settings.EnableAliasForTypeCastSegments && !string.IsNullOrEmpty(namespaceAlias))
{
// Alias type cast segment name
segmentName = namespaceAlias.TrimEnd('.') + "." + element.Name;
}
if (element is IEdmOperation)
{
if (settings.EnableAliasForOperationSegments && !string.IsNullOrEmpty(namespaceAlias))
{
// Alias operation segment name
segmentName = namespaceAlias.TrimEnd('.') + "." + element.Name;
}
if (!string.IsNullOrEmpty(settings.NamespacePrefixToStripForInMethodPaths) &&
element.Namespace.Equals(settings.NamespacePrefixToStripForInMethodPaths, StringComparison.OrdinalIgnoreCase))
{
// Strip specified namespace from operation segment name.
// If the namespace prefix to strip matches the namespace name,
// and the alias has been appended, the alias will be stripped.
segmentName = element.Name;
}
}
return segmentName;
}
/// <summary>
/// Checks whether an operation is allowed on a model element.
/// </summary>
/// <param name="model">The Edm model.</param>
/// <param name="edmOperation">The target operation.</param>
/// <param name="annotatable">The model element.</param>
/// <param name="operationAllowed">Optional: Default is true.
/// The operation will be allowed by default if the annotation Org.OData.Core.V1.RequiresExplicitBinding is undefined for the given operation. </param>
/// <returns>true if the operation is allowed, otherwise false.</returns>
internal static bool IsOperationAllowed(IEdmModel model, IEdmOperation edmOperation, IEdmVocabularyAnnotatable annotatable, bool operationAllowed = true)
{
Utils.CheckArgumentNull(model, nameof(model));
Utils.CheckArgumentNull(edmOperation, nameof(edmOperation));
Utils.CheckArgumentNull(annotatable, nameof(annotatable));
var requiresExplicitBinding = model.FindVocabularyAnnotations(edmOperation).FirstOrDefault(x => x.Term.Name == CapabilitiesConstants.RequiresExplicitBindingName);
if (requiresExplicitBinding == null)
{
return operationAllowed;
}
var boundOperations = model.GetCollection(annotatable, CapabilitiesConstants.ExplicitOperationBindings)?.ToList();
return boundOperations != null && boundOperations.Contains(edmOperation.FullName());
}
}
}