Skip to content

Commit f458088

Browse files
Merge branch 'master' into ma/add-documentation-links-annotations
2 parents 26ec2f1 + 9865f1c commit f458088

15 files changed

+294
-158
lines changed

src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,17 @@ private void RetrieveNavigationPropertyPaths(
403403
visitedNavigationProperties.Push(navPropFullyQualifiedName);
404404

405405
// Check whether a collection-valued navigation property should be indexed by key value(s).
406-
bool indexableByKey = restriction?.IndexableByKey ?? true;
406+
bool indexableByKey = true;
407+
408+
if (restriction?.IndexableByKey != null)
409+
{
410+
indexableByKey = (bool)restriction.IndexableByKey;
411+
}
412+
else if (_model.FindVocabularyAnnotations<IEdmVocabularyAnnotation>(navigationProperty, CapabilitiesConstants.IndexableByKey).FirstOrDefault() is IEdmVocabularyAnnotation indexabilityAnnotation && indexabilityAnnotation.Value is IEdmBooleanValue booleanValue)
413+
{
414+
// Find indexability annotation annotated directly via NavigationPropertyRestriction
415+
indexableByKey = booleanValue.Value;
416+
}
407417

408418
if (indexableByKey)
409419
{

src/Microsoft.OpenApi.OData.Reader/Operation/NavigationPropertyDeleteOperationHandler.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
using System.Linq;
77
using Microsoft.OpenApi.Models;
88
using Microsoft.OpenApi.OData.Common;
9+
using Microsoft.OpenApi.OData.Edm;
910
using Microsoft.OpenApi.OData.Generator;
11+
using Microsoft.OpenApi.OData.Vocabulary.Capabilities;
1012

1113
namespace Microsoft.OpenApi.OData.Operation
1214
{
@@ -20,13 +22,22 @@ internal class NavigationPropertyDeleteOperationHandler : NavigationPropertyOper
2022
/// <inheritdoc/>
2123
public override OperationType OperationType => OperationType.Delete;
2224

25+
private DeleteRestrictionsType _deleteRestriction;
26+
27+
/// <inheritdoc/>
28+
protected override void Initialize(ODataContext context, ODataPath path)
29+
{
30+
base.Initialize(context, path);
31+
_deleteRestriction = GetRestrictionAnnotation(CapabilitiesConstants.DeleteRestrictions) as DeleteRestrictionsType;
32+
}
33+
2334
/// <inheritdoc/>
2435
protected override void SetBasicInfo(OpenApiOperation operation)
2536
{
2637
// Summary and Description
2738
string placeHolder = "Delete navigation property " + NavigationProperty.Name + " for " + NavigationSource.Name;
28-
operation.Summary = Restriction?.DeleteRestrictions?.Description ?? placeHolder;
29-
operation.Description = Restriction?.DeleteRestrictions?.LongDescription;
39+
operation.Summary = _deleteRestriction?.Description ?? placeHolder;
40+
operation.Description = _deleteRestriction?.LongDescription;
3041

3142
// OperationId
3243
if (Context.Settings.EnableOperationId)
@@ -58,12 +69,12 @@ protected override void SetParameters(OpenApiOperation operation)
5869
/// <inheritdoc/>
5970
protected override void SetSecurity(OpenApiOperation operation)
6071
{
61-
if (Restriction == null || Restriction.DeleteRestrictions == null)
72+
if (_deleteRestriction == null)
6273
{
6374
return;
6475
}
6576

66-
operation.Security = Context.CreateSecurityRequirements(Restriction.DeleteRestrictions.Permissions).ToList();
77+
operation.Security = Context.CreateSecurityRequirements(_deleteRestriction.Permissions).ToList();
6778
}
6879

6980
/// <inheritdoc/>
@@ -75,19 +86,19 @@ protected override void SetResponses(OpenApiOperation operation)
7586

7687
protected override void AppendCustomParameters(OpenApiOperation operation)
7788
{
78-
if (Restriction == null || Restriction.DeleteRestrictions == null)
89+
if (_deleteRestriction == null)
7990
{
8091
return;
8192
}
8293

83-
if (Restriction.DeleteRestrictions.CustomHeaders != null)
94+
if (_deleteRestriction.CustomHeaders != null)
8495
{
85-
AppendCustomParameters(operation, Restriction.DeleteRestrictions.CustomHeaders, ParameterLocation.Header);
96+
AppendCustomParameters(operation, _deleteRestriction.CustomHeaders, ParameterLocation.Header);
8697
}
8798

88-
if (Restriction.DeleteRestrictions.CustomQueryOptions != null)
99+
if (_deleteRestriction.CustomQueryOptions != null)
89100
{
90-
AppendCustomParameters(operation, Restriction.DeleteRestrictions.CustomQueryOptions, ParameterLocation.Query);
101+
AppendCustomParameters(operation, _deleteRestriction.CustomQueryOptions, ParameterLocation.Query);
91102
}
92103
}
93104
}

src/Microsoft.OpenApi.OData.Reader/Operation/NavigationPropertyGetOperationHandler.cs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Microsoft.OpenApi.Any;
1010
using Microsoft.OpenApi.Models;
1111
using Microsoft.OpenApi.OData.Common;
12+
using Microsoft.OpenApi.OData.Edm;
1213
using Microsoft.OpenApi.OData.Generator;
1314
using Microsoft.OpenApi.OData.Vocabulary.Capabilities;
1415

@@ -24,14 +25,22 @@ internal class NavigationPropertyGetOperationHandler : NavigationPropertyOperati
2425
/// <inheritdoc/>
2526
public override OperationType OperationType => OperationType.Get;
2627

28+
private ReadRestrictionsType _readRestriction;
29+
30+
/// <inheritdoc/>
31+
protected override void Initialize(ODataContext context, ODataPath path)
32+
{
33+
base.Initialize(context, path);
34+
_readRestriction = GetRestrictionAnnotation(CapabilitiesConstants.ReadRestrictions) as ReadRestrictionsType;
35+
}
36+
2737
/// <inheritdoc/>
2838
protected override void SetBasicInfo(OpenApiOperation operation)
2939
{
3040
// Summary and Description
31-
ReadRestrictionsType readRestriction = Restriction?.ReadRestrictions;
3241
string placeHolder = "Get " + NavigationProperty.Name + " from " + NavigationSource.Name;
33-
operation.Summary = (LastSegmentIsKeySegment ? readRestriction?.ReadByKeyRestrictions?.Description : readRestriction?.Description) ?? placeHolder;
34-
operation.Description = (LastSegmentIsKeySegment ? readRestriction?.ReadByKeyRestrictions?.LongDescription : readRestriction?.LongDescription)
42+
operation.Summary = (LastSegmentIsKeySegment ? _readRestriction?.ReadByKeyRestrictions?.Description : _readRestriction?.Description) ?? placeHolder;
43+
operation.Description = (LastSegmentIsKeySegment ? _readRestriction?.ReadByKeyRestrictions?.LongDescription : _readRestriction?.LongDescription)
3544
?? Context.Model.GetDescriptionAnnotation(NavigationProperty);
3645

3746
// OperationId
@@ -226,37 +235,31 @@ protected override void SetParameters(OpenApiOperation operation)
226235

227236
protected override void SetSecurity(OpenApiOperation operation)
228237
{
229-
if (Restriction == null || Restriction.ReadRestrictions == null)
238+
if (_readRestriction == null)
230239
{
231240
return;
232241
}
233242

234-
ReadRestrictionsBase readBase = Restriction.ReadRestrictions;
235-
if (LastSegmentIsKeySegment)
243+
ReadRestrictionsBase readBase = _readRestriction;
244+
if (LastSegmentIsKeySegment && _readRestriction.ReadByKeyRestrictions != null)
236245
{
237-
if (Restriction.ReadRestrictions.ReadByKeyRestrictions != null)
238-
{
239-
readBase = Restriction.ReadRestrictions.ReadByKeyRestrictions;
240-
}
246+
readBase = _readRestriction.ReadByKeyRestrictions;
241247
}
242248

243249
operation.Security = Context.CreateSecurityRequirements(readBase.Permissions).ToList();
244250
}
245251

246252
protected override void AppendCustomParameters(OpenApiOperation operation)
247253
{
248-
if (Restriction == null || Restriction.ReadRestrictions == null)
254+
if (_readRestriction == null)
249255
{
250256
return;
251257
}
252258

253-
ReadRestrictionsBase readBase = Restriction.ReadRestrictions;
254-
if (LastSegmentIsKeySegment)
259+
ReadRestrictionsBase readBase = _readRestriction;
260+
if (LastSegmentIsKeySegment && _readRestriction.ReadByKeyRestrictions != null)
255261
{
256-
if (Restriction.ReadRestrictions.ReadByKeyRestrictions != null)
257-
{
258-
readBase = Restriction.ReadRestrictions.ReadByKeyRestrictions;
259-
}
262+
readBase = _readRestriction.ReadByKeyRestrictions;
260263
}
261264

262265
if (readBase.CustomHeaders != null)

src/Microsoft.OpenApi.OData.Reader/Operation/NavigationPropertyOperationHandler.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// ------------------------------------------------------------
1+
// ------------------------------------------------------------
22
// Copyright (c) Microsoft Corporation. All rights reserved.
33
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
44
// ------------------------------------------------------------
@@ -10,6 +10,7 @@
1010
using Microsoft.OpenApi.Models;
1111
using Microsoft.OpenApi.OData.Common;
1212
using Microsoft.OpenApi.OData.Edm;
13+
using Microsoft.OpenApi.OData.Vocabulary;
1314
using Microsoft.OpenApi.OData.Vocabulary.Capabilities;
1415
using Microsoft.OpenApi.OData.Vocabulary.Core;
1516

@@ -172,5 +173,27 @@ protected override void SetExternalDocs(OpenApiOperation operation)
172173
}
173174
}
174175
}
176+
177+
/// <summary>
178+
/// Retrieves the CRUD restrictions annotations for the navigation property
179+
/// in context, given a capability annotation term.
180+
/// </summary>
181+
/// <param name="annotationTerm">The fully qualified restriction annotation term.</param>
182+
/// <returns>The restriction annotation, or null if not available.</returns>
183+
protected IRecord GetRestrictionAnnotation(string annotationTerm)
184+
{
185+
return annotationTerm switch
186+
{
187+
CapabilitiesConstants.ReadRestrictions => Restriction?.ReadRestrictions ??
188+
Context.Model.GetRecord<ReadRestrictionsType>(NavigationProperty, CapabilitiesConstants.ReadRestrictions),
189+
CapabilitiesConstants.UpdateRestrictions => Restriction?.UpdateRestrictions ??
190+
Context.Model.GetRecord<UpdateRestrictionsType>(NavigationProperty, CapabilitiesConstants.UpdateRestrictions),
191+
CapabilitiesConstants.InsertRestrictions => Restriction?.InsertRestrictions ??
192+
Context.Model.GetRecord<InsertRestrictionsType>(NavigationProperty, CapabilitiesConstants.InsertRestrictions),
193+
CapabilitiesConstants.DeleteRestrictions => Restriction?.DeleteRestrictions ??
194+
Context.Model.GetRecord<DeleteRestrictionsType>(NavigationProperty, CapabilitiesConstants.DeleteRestrictions),
195+
_ => null,
196+
};
197+
}
175198
}
176199
}

src/Microsoft.OpenApi.OData.Reader/Operation/NavigationPropertyPostOperationHandler.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
using Microsoft.OData.Edm;
99
using Microsoft.OpenApi.Models;
1010
using Microsoft.OpenApi.OData.Common;
11+
using Microsoft.OpenApi.OData.Edm;
1112
using Microsoft.OpenApi.OData.Generator;
13+
using Microsoft.OpenApi.OData.Vocabulary.Capabilities;
1214

1315
namespace Microsoft.OpenApi.OData.Operation
1416
{
@@ -22,13 +24,22 @@ internal class NavigationPropertyPostOperationHandler : NavigationPropertyOperat
2224
/// <inheritdoc/>
2325
public override OperationType OperationType => OperationType.Post;
2426

27+
private InsertRestrictionsType _insertRestriction;
28+
29+
/// <inheritdoc/>
30+
protected override void Initialize(ODataContext context, ODataPath path)
31+
{
32+
base.Initialize(context, path);
33+
_insertRestriction = GetRestrictionAnnotation(CapabilitiesConstants.InsertRestrictions) as InsertRestrictionsType;
34+
}
35+
2536
/// <inheritdoc/>
2637
protected override void SetBasicInfo(OpenApiOperation operation)
2738
{
2839
// Summary and Description
2940
string placeHolder = "Create new navigation property to " + NavigationProperty.Name + " for " + NavigationSource.Name;
30-
operation.Summary = Restriction?.InsertRestrictions?.Description ?? placeHolder;
31-
operation.Description = Restriction?.InsertRestrictions?.LongDescription;
41+
operation.Summary = _insertRestriction?.Description ?? placeHolder;
42+
operation.Description = _insertRestriction?.LongDescription;
3243

3344
// OperationId
3445
if (Context.Settings.EnableOperationId)
@@ -131,29 +142,29 @@ protected override void SetResponses(OpenApiOperation operation)
131142

132143
protected override void SetSecurity(OpenApiOperation operation)
133144
{
134-
if (Restriction == null || Restriction.InsertRestrictions == null)
145+
if (_insertRestriction == null)
135146
{
136147
return;
137148
}
138149

139-
operation.Security = Context.CreateSecurityRequirements(Restriction.InsertRestrictions.Permissions).ToList();
150+
operation.Security = Context.CreateSecurityRequirements(_insertRestriction.Permissions).ToList();
140151
}
141152

142153
protected override void AppendCustomParameters(OpenApiOperation operation)
143154
{
144-
if (Restriction == null || Restriction.InsertRestrictions == null)
155+
if (_insertRestriction == null)
145156
{
146157
return;
147158
}
148159

149-
if (Restriction.InsertRestrictions.CustomHeaders != null)
160+
if (_insertRestriction.CustomHeaders != null)
150161
{
151-
AppendCustomParameters(operation, Restriction.InsertRestrictions.CustomHeaders, ParameterLocation.Header);
162+
AppendCustomParameters(operation, _insertRestriction.CustomHeaders, ParameterLocation.Header);
152163
}
153164

154-
if (Restriction.InsertRestrictions.CustomQueryOptions != null)
165+
if (_insertRestriction.CustomQueryOptions != null)
155166
{
156-
AppendCustomParameters(operation, Restriction.InsertRestrictions.CustomQueryOptions, ParameterLocation.Query);
167+
AppendCustomParameters(operation, _insertRestriction.CustomQueryOptions, ParameterLocation.Query);
157168
}
158169
}
159170
}

src/Microsoft.OpenApi.OData.Reader/Operation/NavigationPropertyUpdateOperationHandler.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
using Microsoft.OData.Edm;
99
using Microsoft.OpenApi.Models;
1010
using Microsoft.OpenApi.OData.Common;
11+
using Microsoft.OpenApi.OData.Edm;
1112
using Microsoft.OpenApi.OData.Generator;
13+
using Microsoft.OpenApi.OData.Vocabulary.Capabilities;
1214

1315
namespace Microsoft.OpenApi.OData.Operation
1416
{
@@ -19,13 +21,22 @@ namespace Microsoft.OpenApi.OData.Operation
1921
/// </summary>
2022
internal abstract class NavigationPropertyUpdateOperationHandler : NavigationPropertyOperationHandler
2123
{
24+
private UpdateRestrictionsType _updateRestriction;
25+
26+
/// <inheritdoc/>
27+
protected override void Initialize(ODataContext context, ODataPath path)
28+
{
29+
base.Initialize(context, path);
30+
_updateRestriction = GetRestrictionAnnotation(CapabilitiesConstants.UpdateRestrictions) as UpdateRestrictionsType;
31+
}
32+
2233
/// <inheritdoc/>
2334
protected override void SetBasicInfo(OpenApiOperation operation)
2435
{
2536
// Summary and Description
2637
string placeHolder = "Update the navigation property " + NavigationProperty.Name + " in " + NavigationSource.Name;
27-
operation.Summary = Restriction?.UpdateRestrictions?.Description ?? placeHolder;
28-
operation.Description = Restriction?.UpdateRestrictions?.LongDescription;
38+
operation.Summary = _updateRestriction?.Description ?? placeHolder;
39+
operation.Description = _updateRestriction?.LongDescription;
2940

3041
// OperationId
3142
if (Context.Settings.EnableOperationId)
@@ -67,29 +78,29 @@ protected override void SetResponses(OpenApiOperation operation)
6778

6879
protected override void SetSecurity(OpenApiOperation operation)
6980
{
70-
if (Restriction == null || Restriction.UpdateRestrictions == null)
81+
if (_updateRestriction == null)
7182
{
7283
return;
7384
}
7485

75-
operation.Security = Context.CreateSecurityRequirements(Restriction.UpdateRestrictions.Permissions).ToList();
86+
operation.Security = Context.CreateSecurityRequirements(_updateRestriction.Permissions).ToList();
7687
}
7788

7889
protected override void AppendCustomParameters(OpenApiOperation operation)
7990
{
80-
if (Restriction == null || Restriction.UpdateRestrictions == null)
91+
if (_updateRestriction == null)
8192
{
8293
return;
8394
}
8495

85-
if (Restriction.UpdateRestrictions.CustomHeaders != null)
96+
if (_updateRestriction.CustomHeaders != null)
8697
{
87-
AppendCustomParameters(operation, Restriction.UpdateRestrictions.CustomHeaders, ParameterLocation.Header);
98+
AppendCustomParameters(operation, _updateRestriction.CustomHeaders, ParameterLocation.Header);
8899
}
89100

90-
if (Restriction.UpdateRestrictions.CustomQueryOptions != null)
101+
if (_updateRestriction.CustomQueryOptions != null)
91102
{
92-
AppendCustomParameters(operation, Restriction.UpdateRestrictions.CustomQueryOptions, ParameterLocation.Query);
103+
AppendCustomParameters(operation, _updateRestriction.CustomQueryOptions, ParameterLocation.Query);
93104
}
94105
}
95106

0 commit comments

Comments
 (0)