Skip to content

Commit 46722a9

Browse files
committed
chore: additional NRT fixes
Signed-off-by: Vincent Biret <[email protected]>
1 parent 5362d83 commit 46722a9

File tree

4 files changed

+73
-62
lines changed

4 files changed

+73
-62
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public RefPostOperationHandler(OpenApiDocument document) : base(document)
2929
}
3030
/// <inheritdoc/>
3131
public override HttpMethod OperationType => HttpMethod.Post;
32-
private InsertRestrictionsType _insertRestriction;
32+
private InsertRestrictionsType? _insertRestriction;
3333

3434
/// <inheritdoc/>
3535
protected override void Initialize(ODataContext context, ODataPath path)
@@ -42,12 +42,12 @@ protected override void Initialize(ODataContext context, ODataPath path)
4242
protected override void SetBasicInfo(OpenApiOperation operation)
4343
{
4444
// Summary and Description
45-
string placeHolder = "Create new navigation property ref to " + NavigationProperty.Name + " for " + NavigationSource.Name;
45+
string placeHolder = "Create new navigation property ref to " + NavigationProperty?.Name + " for " + NavigationSource?.Name;
4646
operation.Summary = _insertRestriction?.Description ?? placeHolder;
4747
operation.Description = _insertRestriction?.LongDescription;
4848

4949
// OperationId
50-
if (Context.Settings.EnableOperationId)
50+
if (Context is {Settings.EnableOperationId: true})
5151
{
5252
string prefix = "CreateRef";
5353
operation.OperationId = GetOperationId(prefix);
@@ -73,19 +73,20 @@ protected override void SetResponses(OpenApiOperation operation)
7373
}
7474
};
7575

76-
operation.AddErrorResponses(Context.Settings, _document, false);
76+
if (Context is not null)
77+
operation.AddErrorResponses(Context.Settings, _document, false);
7778

7879
base.SetResponses(operation);
7980
}
8081

8182
protected override void SetSecurity(OpenApiOperation operation)
8283
{
83-
if (_insertRestriction == null)
84+
if (_insertRestriction?.Permissions is null)
8485
{
8586
return;
8687
}
8788

88-
operation.Security = Context.CreateSecurityRequirements(_insertRestriction.Permissions, _document).ToList();
89+
operation.Security = Context?.CreateSecurityRequirements(_insertRestriction.Permissions, _document).ToList();
8990
}
9091

9192
protected override void AppendCustomParameters(OpenApiOperation operation)

src/Microsoft.OpenApi.OData.Reader/PathItem/OperationPathItemHandler.cs

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Text.Json.Nodes;
1010
using Microsoft.OData.Edm;
1111
using Microsoft.OpenApi.Any;
12+
using Microsoft.OpenApi.Interfaces;
1213
using Microsoft.OpenApi.Models;
1314
using Microsoft.OpenApi.OData.Common;
1415
using Microsoft.OpenApi.OData.Edm;
@@ -34,7 +35,7 @@ public OperationPathItemHandler(OpenApiDocument document) : base(document)
3435
/// <summary>
3536
/// Gets the Edm operation.
3637
/// </summary>
37-
public IEdmOperation EdmOperation { get; private set; }
38+
public IEdmOperation? EdmOperation { get; private set; }
3839

3940
/// <inheritdoc/>
4041
protected override void SetOperations(OpenApiPathItem item)
@@ -58,61 +59,65 @@ protected override void Initialize(ODataContext context, ODataPath path)
5859
{
5960
base.Initialize(context, path);
6061

61-
ODataOperationSegment operationSegment = path.LastSegment as ODataOperationSegment;
62-
EdmOperation = operationSegment.Operation;
62+
if (path.LastSegment is ODataOperationSegment {Operation: {} operation})
63+
EdmOperation = operation;
6364
}
6465

6566
/// <inheritdoc/>
6667
protected override void SetExtensions(OpenApiPathItem item)
6768
{
68-
if (!Context.Settings.ShowMsDosGroupPath)
69+
if (Context is null || !Context.Settings.ShowMsDosGroupPath)
6970
{
7071
return;
7172
}
7273

73-
ODataNavigationSourceSegment navigationSourceSegment = Path.FirstSegment as ODataNavigationSourceSegment;
74-
IEdmNavigationSource currentNavSource = navigationSourceSegment.NavigationSource;
75-
76-
IList<ODataPath> samePaths = new List<ODataPath>();
77-
foreach (var path in Context.AllPaths.Where(p => p.Kind == ODataPathKind.Operation && p != Path))
74+
if (Path?.FirstSegment is ODataNavigationSourceSegment {NavigationSource: IEdmNavigationSource currentNavSource})
7875
{
79-
navigationSourceSegment = path.FirstSegment as ODataNavigationSourceSegment;
80-
if (currentNavSource != navigationSourceSegment.NavigationSource)
81-
{
82-
continue;
83-
}
8476

85-
ODataOperationSegment operationSegment = path.LastSegment as ODataOperationSegment;
86-
if (EdmOperation.FullName() != operationSegment.Operation.FullName())
77+
var samePaths = new List<ODataPath>();
78+
foreach (var path in Context.AllPaths.Where(p => p.Kind == ODataPathKind.Operation && p != Path))
8779
{
88-
continue;
89-
}
80+
if (path.FirstSegment is ODataNavigationSourceSegment navigationSourceSegment && currentNavSource != navigationSourceSegment.NavigationSource)
81+
{
82+
continue;
83+
}
9084

91-
samePaths.Add(path);
92-
}
85+
if (path.LastSegment is ODataOperationSegment operationSegment && EdmOperation.FullName() != operationSegment.Operation.FullName())
86+
{
87+
continue;
88+
}
9389

94-
if (samePaths.Any())
95-
{
96-
JsonArray array = new JsonArray();
97-
OpenApiConvertSettings settings = Context.Settings.Clone();
98-
settings.EnableKeyAsSegment = Context.KeyAsSegment;
99-
foreach (var p in samePaths)
100-
{
101-
array.Add(p.GetPathItemName(settings));
90+
samePaths.Add(path);
10291
}
10392

104-
item.Extensions.Add(Constants.xMsDosGroupPath, new OpenApiAny(array));
93+
if (samePaths.Any())
94+
{
95+
JsonArray array = new JsonArray();
96+
OpenApiConvertSettings settings = Context.Settings.Clone();
97+
settings.EnableKeyAsSegment = Context.KeyAsSegment;
98+
foreach (var p in samePaths)
99+
{
100+
array.Add(p.GetPathItemName(settings));
101+
}
102+
103+
item.Extensions ??= new Dictionary<string, IOpenApiExtension>();
104+
item.Extensions.Add(Constants.xMsDosGroupPath, new OpenApiAny(array));
105+
}
105106
}
106107

107108
base.SetExtensions(item);
108-
item.Extensions.AddCustomAttributesToExtensions(Context, EdmOperation);
109+
if (EdmOperation is not null)
110+
{
111+
item.Extensions ??= new Dictionary<string, IOpenApiExtension>();
112+
item.Extensions.AddCustomAttributesToExtensions(Context, EdmOperation);
113+
}
109114
}
110115

111116
/// <inheritdoc/>
112117
protected override void SetBasicInfo(OpenApiPathItem pathItem)
113118
{
114119
base.SetBasicInfo(pathItem);
115-
pathItem.Description = $"Provides operations to call the {EdmOperation.Name} method.";
120+
pathItem.Description = $"Provides operations to call the {EdmOperation?.Name} method.";
116121
}
117122
}
118123
}

src/Microsoft.OpenApi.OData.Reader/PathItem/RefPathItemHandler.cs

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,28 +34,32 @@ public RefPathItemHandler(OpenApiDocument document) : base(document)
3434
/// <summary>
3535
/// Gets the navigation property.
3636
/// </summary>
37-
public IEdmNavigationProperty NavigationProperty { get; private set; }
37+
public IEdmNavigationProperty? NavigationProperty { get; private set; }
3838

3939
/// <summary>
4040
/// Gets the navigation source.
4141
/// </summary>
42-
public IEdmNavigationSource NavigationSource { get; private set; }
42+
public IEdmNavigationSource? NavigationSource { get; private set; }
4343

4444
/// <inheritdoc/>
4545
protected override void SetOperations(OpenApiPathItem item)
4646
{
47-
IEdmEntitySet entitySet = NavigationSource as IEdmEntitySet;
48-
IEdmVocabularyAnnotatable target = entitySet;
49-
target ??= NavigationSource as IEdmSingleton;
47+
if (NavigationSource is not IEdmVocabularyAnnotatable target)
48+
throw new InvalidOperationException($"The navigation source {NavigationSource?.Name} is not a vocabulary annotatable.");
5049

51-
string navigationPropertyPath = String.Join("/",
52-
Path.Segments.Where(s => !(s is ODataKeySegment || s is ODataNavigationSourceSegment)).Select(e => e.Identifier));
50+
string navigationPropertyPath = string.Join("/",
51+
Path?.Segments.Where(s => !(s is ODataKeySegment || s is ODataNavigationSourceSegment)).Select(e => e.Identifier) ?? []);
5352

54-
NavigationRestrictionsType navigationRestrictions = Context.Model.GetRecord<NavigationRestrictionsType>(TargetPath, CapabilitiesConstants.NavigationRestrictions);
55-
NavigationRestrictionsType sourceNavigationRestrictions = Context.Model.GetRecord<NavigationRestrictionsType>(target, CapabilitiesConstants.NavigationRestrictions);
56-
navigationRestrictions?.MergePropertiesIfNull(sourceNavigationRestrictions);
57-
navigationRestrictions ??= sourceNavigationRestrictions;
58-
NavigationPropertyRestriction restriction = navigationRestrictions?.RestrictedProperties?.FirstOrDefault(r => r.NavigationProperty == navigationPropertyPath);
53+
var navigationRestrictions = string.IsNullOrEmpty(TargetPath) ? null : Context?.Model.GetRecord<NavigationRestrictionsType>(TargetPath, CapabilitiesConstants.NavigationRestrictions);
54+
55+
if (Context is not null && target is not null)
56+
{
57+
var sourceNavigationRestrictions = Context.Model.GetRecord<NavigationRestrictionsType>(target, CapabilitiesConstants.NavigationRestrictions);
58+
navigationRestrictions?.MergePropertiesIfNull(sourceNavigationRestrictions);
59+
navigationRestrictions ??= sourceNavigationRestrictions;
60+
}
61+
62+
var restriction = navigationRestrictions?.RestrictedProperties?.FirstOrDefault(r => r.NavigationProperty == navigationPropertyPath);
5963

6064
// verify using individual first
6165
if (restriction?.Navigability != null && restriction.Navigability.Value == NavigationType.None)
@@ -77,8 +81,7 @@ protected override void SetOperations(OpenApiPathItem item)
7781
// Create the ref
7882
if (NavigationProperty.TargetMultiplicity() == EdmMultiplicity.Many)
7983
{
80-
ODataSegment penultimateSegment = Path.Segments.Reverse().Skip(1).First();
81-
if (penultimateSegment is ODataKeySegment)
84+
if (Path?.Segments.Reverse().Skip(1).First() is ODataKeySegment)
8285
{
8386
// Collection-valued indexed: DELETE ~/entityset/{key}/collection-valued-Nav/{key}/$ref
8487
AddDeleteOperation(item, restriction);
@@ -99,9 +102,9 @@ protected override void SetOperations(OpenApiPathItem item)
99102
}
100103
}
101104

102-
private void AddDeleteOperation(OpenApiPathItem item, NavigationPropertyRestriction restriction)
105+
private void AddDeleteOperation(OpenApiPathItem item, NavigationPropertyRestriction? restriction)
103106
{
104-
DeleteRestrictionsType deleteRestrictions = Context.Model.GetRecord<DeleteRestrictionsType>(TargetPath, CapabilitiesConstants.DeleteRestrictions);
107+
var deleteRestrictions = string.IsNullOrEmpty(TargetPath) ? null : Context?.Model.GetRecord<DeleteRestrictionsType>(TargetPath, CapabilitiesConstants.DeleteRestrictions);
105108
deleteRestrictions?.MergePropertiesIfNull(restriction?.DeleteRestrictions);
106109
deleteRestrictions ??= restriction?.DeleteRestrictions;
107110
if (deleteRestrictions?.IsDeletable ?? true)
@@ -110,9 +113,9 @@ private void AddDeleteOperation(OpenApiPathItem item, NavigationPropertyRestrict
110113
}
111114
}
112115

113-
private void AddReadOperation(OpenApiPathItem item, NavigationPropertyRestriction restriction)
116+
private void AddReadOperation(OpenApiPathItem item, NavigationPropertyRestriction? restriction)
114117
{
115-
ReadRestrictionsType readRestrictions = Context.Model.GetRecord<ReadRestrictionsType>(TargetPath, CapabilitiesConstants.ReadRestrictions);
118+
var readRestrictions = string.IsNullOrEmpty(TargetPath) ? null : Context?.Model.GetRecord<ReadRestrictionsType>(TargetPath, CapabilitiesConstants.ReadRestrictions);
116119
readRestrictions?.MergePropertiesIfNull(restriction?.ReadRestrictions);
117120
readRestrictions ??= restriction?.ReadRestrictions;
118121
if (readRestrictions?.IsReadable ?? true)
@@ -121,9 +124,9 @@ private void AddReadOperation(OpenApiPathItem item, NavigationPropertyRestrictio
121124
}
122125
}
123126

124-
private void AddInsertOperation(OpenApiPathItem item, NavigationPropertyRestriction restriction)
127+
private void AddInsertOperation(OpenApiPathItem item, NavigationPropertyRestriction? restriction)
125128
{
126-
InsertRestrictionsType insertRestrictions = Context.Model.GetRecord<InsertRestrictionsType>(TargetPath, CapabilitiesConstants.InsertRestrictions);
129+
var insertRestrictions = string.IsNullOrEmpty(TargetPath) ? null : Context?.Model.GetRecord<InsertRestrictionsType>(TargetPath, CapabilitiesConstants.InsertRestrictions);
127130
insertRestrictions?.MergePropertiesIfNull(restriction?.InsertRestrictions);
128131
insertRestrictions ??= restriction?.InsertRestrictions;
129132
if (insertRestrictions?.IsInsertable ?? true)
@@ -132,9 +135,9 @@ private void AddInsertOperation(OpenApiPathItem item, NavigationPropertyRestrict
132135
}
133136
}
134137

135-
private void AddUpdateOperation(OpenApiPathItem item, NavigationPropertyRestriction restriction)
138+
private void AddUpdateOperation(OpenApiPathItem item, NavigationPropertyRestriction? restriction)
136139
{
137-
UpdateRestrictionsType updateRestrictions = Context.Model.GetRecord<UpdateRestrictionsType>(TargetPath, CapabilitiesConstants.UpdateRestrictions);
140+
var updateRestrictions = string.IsNullOrEmpty(TargetPath) ? null : Context?.Model.GetRecord<UpdateRestrictionsType>(TargetPath, CapabilitiesConstants.UpdateRestrictions);
138141
updateRestrictions?.MergePropertiesIfNull(restriction?.UpdateRestrictions);
139142
updateRestrictions ??= restriction?.UpdateRestrictions;
140143
if (updateRestrictions?.IsUpdatable ?? true)
@@ -148,8 +151,10 @@ protected override void Initialize(ODataContext context, ODataPath path)
148151
{
149152
base.Initialize(context, path);
150153

151-
ODataNavigationSourceSegment navigationSourceSegment = path.FirstSegment as ODataNavigationSourceSegment;
152-
NavigationSource = navigationSourceSegment.NavigationSource;
154+
if (path.FirstSegment is ODataNavigationSourceSegment {NavigationSource: {} source})
155+
{
156+
NavigationSource = source;
157+
}
153158

154159
NavigationProperty = path.OfType<ODataNavigationPropertySegment>().Last().NavigationProperty;
155160
}

src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/NavigationRestrictionsType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ public void Initialize(IEdmRecordExpression record)
244244
/// Merges properties of the specified <see cref="NavigationRestrictionsType"/> object into this instance if they are null.
245245
/// </summary>
246246
/// <param name="source">The <see cref="NavigationRestrictionsType"/> object containing properties to merge.</param>
247-
public void MergePropertiesIfNull(NavigationRestrictionsType source)
247+
public void MergePropertiesIfNull(NavigationRestrictionsType? source)
248248
{
249249
if (source == null)
250250
return;

0 commit comments

Comments
 (0)