-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathNavigationPropertyOperationHandler.cs
More file actions
240 lines (205 loc) · 11.1 KB
/
NavigationPropertyOperationHandler.cs
File metadata and controls
240 lines (205 loc) · 11.1 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
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------
using Microsoft.OData.Edm;
using Microsoft.OpenApi.OData.Common;
using Microsoft.OpenApi.OData.Edm;
using Microsoft.OpenApi.OData.Vocabulary;
using Microsoft.OpenApi.OData.Vocabulary.Capabilities;
using Microsoft.OpenApi.OData.Vocabulary.Core;
using System.Collections.Generic;
using System.Linq;
namespace Microsoft.OpenApi.OData.Operation
{
/// <summary>
/// Base class for operation of <see cref="IEdmNavigationProperty"/>.
/// </summary>
internal abstract class NavigationPropertyOperationHandler : OperationHandler
{
/// <summary>
/// Initializes a new instance of <see cref="NavigationPropertyOperationHandler"/> class.
/// </summary>
/// <param name="document">The document to use to lookup references.</param>
protected NavigationPropertyOperationHandler(OpenApiDocument document):base(document)
{
}
/// <summary>
/// Gets the navigation property.
/// </summary>
protected IEdmNavigationProperty? NavigationProperty { get; private set; }
/// <summary>
/// Gets the navigation source.
/// </summary>
protected IEdmNavigationSource? NavigationSource { get; private set; }
/// <summary>
/// Gets the navigation restriction.
/// </summary>
protected NavigationPropertyRestriction? Restriction { get; private set; }
/// <summary>
/// Gets a bool value indicating whether the last segment is a key segment.
/// </summary>
protected bool LastSegmentIsKeySegment { get; private set; }
/// <summary>
/// Gets a bool value indicating whether the second last segment in a $ref path is a key segment
/// </summary>
protected bool SecondLastSegmentIsKeySegment { get; private set; }
/// <summary>
/// Gets a bool value indicating whether the last segment is a $ref segment.
/// </summary>
protected bool LastSegmentIsRefSegment { get; private set; }
/// <inheritdoc/>
protected override void Initialize(ODataContext context, ODataPath path)
{
base.Initialize(context, path);
ODataNavigationSourceSegment? navigationSourceSegment = path.FirstSegment as ODataNavigationSourceSegment;
NavigationSource = navigationSourceSegment?.NavigationSource;
LastSegmentIsKeySegment = path.LastSegment is ODataKeySegment;
LastSegmentIsRefSegment = path.LastSegment is ODataRefSegment;
SecondLastSegmentIsKeySegment = Path?.Segments.Reverse().Skip(1).Take(1).Single().Kind == ODataSegmentKind.Key;
NavigationProperty = path.OfType<ODataNavigationPropertySegment>().Last().NavigationProperty;
var navigation = NavigationSource switch {
IEdmEntitySet entitySet => Context?.Model.GetRecord<NavigationRestrictionsType>(entitySet, CapabilitiesConstants.NavigationRestrictions),
IEdmSingleton singleton => Context?.Model.GetRecord<NavigationRestrictionsType>(singleton, CapabilitiesConstants.NavigationRestrictions),
_ => null
};
var navPropertyPath = Path?.NavigationPropertyPath();
Restriction = navigation?.RestrictedProperties?.FirstOrDefault(r => r.NavigationProperty != null && r.NavigationProperty == navPropertyPath)
?? Context?.Model.GetRecord<NavigationRestrictionsType>(NavigationProperty, CapabilitiesConstants.NavigationRestrictions)?.RestrictedProperties?.FirstOrDefault();
}
/// <inheritdoc/>
protected override void SetTags(OpenApiOperation operation)
{
if (Context is not null && Path is not null)
{
string name = EdmModelHelper.GenerateNavigationPropertyPathTagName(Path, Context);
Context.AddExtensionToTag(name, Constants.xMsTocType, new JsonNodeExtension("page"), () => new OpenApiTag()
{
Name = name
});
operation.Tags ??= new HashSet<OpenApiTagReference>();
operation.Tags.Add(new OpenApiTagReference(name, _document));
}
base.SetTags(operation);
}
/// <inheritdoc/>
protected override void SetExtensions(OpenApiOperation operation)
{
operation.Extensions ??= new Dictionary<string, IOpenApiExtension>();
operation.Extensions.Add(Constants.xMsDosOperationType, new JsonNodeExtension("operation"));
base.SetExtensions(operation);
}
internal string? GetOperationId(string? prefix = null)
{
if (Context is null || Path is null)
{
return null;
}
return EdmModelHelper.GenerateNavigationPropertyPathOperationId(Path, Context, prefix);
}
/// <inheritdoc/>
protected override void SetExternalDocs(OpenApiOperation operation)
{
if (Context is {Settings.ShowExternalDocs: true} && !string.IsNullOrEmpty(CustomLinkRel))
{
var externalDocs = (string.IsNullOrEmpty(TargetPath) ? null : Context.Model.GetLinkRecord(TargetPath, CustomLinkRel)) ??
(NavigationProperty is null ? null : Context.Model.GetLinkRecord(NavigationProperty, CustomLinkRel));
if (externalDocs != null)
{
operation.ExternalDocs = new OpenApiExternalDocs()
{
Description = CoreConstants.ExternalDocsDescription,
Url = externalDocs.Href
};
}
}
}
/// <summary>
/// Retrieves the CRUD restrictions annotations for the navigation property
/// in context, given a capability annotation term.
/// </summary>
/// <param name="annotationTerm">The fully qualified restriction annotation term.</param>
/// <returns>The restriction annotation, or null if not available.</returns>
protected IRecord? GetRestrictionAnnotation(string annotationTerm)
{
if (Context is null) return null;
switch (annotationTerm)
{
case CapabilitiesConstants.ReadRestrictions:
var readRestrictions = string.IsNullOrEmpty(TargetPath) ? null : Context.Model.GetRecord<ReadRestrictionsType>(TargetPath, CapabilitiesConstants.ReadRestrictions);
readRestrictions?.MergePropertiesIfNull(Restriction?.ReadRestrictions);
readRestrictions ??= Restriction?.ReadRestrictions;
if (NavigationProperty is not null)
{
var navPropReadRestrictions = Context.Model.GetRecord<ReadRestrictionsType>(NavigationProperty, CapabilitiesConstants.ReadRestrictions);
readRestrictions?.MergePropertiesIfNull(navPropReadRestrictions);
readRestrictions ??= navPropReadRestrictions;
}
return readRestrictions;
case CapabilitiesConstants.UpdateRestrictions:
var updateRestrictions = string.IsNullOrEmpty(TargetPath) ? null : Context.Model.GetRecord<UpdateRestrictionsType>(TargetPath, CapabilitiesConstants.UpdateRestrictions);
updateRestrictions?.MergePropertiesIfNull(Restriction?.UpdateRestrictions);
updateRestrictions ??= Restriction?.UpdateRestrictions;
if (NavigationProperty is not null)
{
var navPropUpdateRestrictions = Context.Model.GetRecord<UpdateRestrictionsType>(NavigationProperty, CapabilitiesConstants.UpdateRestrictions);
updateRestrictions?.MergePropertiesIfNull(navPropUpdateRestrictions);
updateRestrictions ??= navPropUpdateRestrictions;
}
return updateRestrictions;
case CapabilitiesConstants.InsertRestrictions:
var insertRestrictions = string.IsNullOrEmpty(TargetPath) ? null : Context.Model.GetRecord<InsertRestrictionsType>(TargetPath, CapabilitiesConstants.InsertRestrictions);
insertRestrictions?.MergePropertiesIfNull(Restriction?.InsertRestrictions);
insertRestrictions ??= Restriction?.InsertRestrictions;
if (NavigationProperty is not null)
{
var navPropInsertRestrictions = Context.Model.GetRecord<InsertRestrictionsType>(NavigationProperty, CapabilitiesConstants.InsertRestrictions);
insertRestrictions?.MergePropertiesIfNull(navPropInsertRestrictions);
insertRestrictions ??= navPropInsertRestrictions;
}
return insertRestrictions;
case CapabilitiesConstants.DeleteRestrictions:
var deleteRestrictions = string.IsNullOrEmpty(TargetPath) ? null : Context.Model.GetRecord<DeleteRestrictionsType>(TargetPath, CapabilitiesConstants.DeleteRestrictions);
deleteRestrictions?.MergePropertiesIfNull(Restriction?.DeleteRestrictions);
deleteRestrictions ??= Restriction?.DeleteRestrictions;
if (NavigationProperty is not null)
{
var navPropDeleteRestrictions = Context.Model.GetRecord<DeleteRestrictionsType>(NavigationProperty, CapabilitiesConstants.DeleteRestrictions);
deleteRestrictions?.MergePropertiesIfNull(navPropDeleteRestrictions);
deleteRestrictions ??= navPropDeleteRestrictions;
}
return deleteRestrictions;
default:
return null;
}
}
protected Dictionary<string, IOpenApiMediaType> GetContent(IOpenApiSchema? schema = null, IEnumerable<string>? mediaTypes = null)
{
schema ??= GetOpenApiSchema();
var content = new Dictionary<string, IOpenApiMediaType>();
if (mediaTypes != null)
{
foreach (string mediaType in mediaTypes)
{
content.Add(mediaType, new OpenApiMediaType
{
Schema = schema
});
}
}
else
{
// Default content type
content.Add(Constants.ApplicationJsonMediaType, new OpenApiMediaType
{
Schema = schema
});
}
return content;
}
protected IOpenApiSchema GetOpenApiSchema()
{
return new OpenApiSchemaReference(NavigationProperty.ToEntityType().FullName(), _document);
}
}
}