Skip to content

Commit 6827d88

Browse files
committed
chore: another batch of NRT fixes
Signed-off-by: Vincent Biret <[email protected]>
1 parent a755dd8 commit 6827d88

File tree

8 files changed

+88
-81
lines changed

8 files changed

+88
-81
lines changed

src/Microsoft.OpenApi.OData.Reader/Common/EdmModelHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ internal static class EdmModelHelper
6060
/// <param name="restrictionProperty">The <see cref="NavigationPropertyRestriction"/>.</param>
6161
/// <returns>true, if navigability is allowed, otherwise false.</returns>
6262
internal static bool NavigationRestrictionsAllowsNavigability(
63-
NavigationRestrictionsType restrictionType,
64-
NavigationPropertyRestriction restrictionProperty)
63+
NavigationRestrictionsType? restrictionType,
64+
NavigationPropertyRestriction? restrictionProperty)
6565
{
6666
// Verify using individual navigation restriction first
6767
if (restrictionProperty?.Navigability != null && restrictionProperty.Navigability.Value == NavigationType.None)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,17 +287,17 @@ public override string ToString()
287287
return PathTemplate;
288288
}
289289

290-
return "/" + String.Join("/", Segments.Select(e => e.Kind));
290+
return "/" + string.Join("/", Segments.Select(e => e.Kind));
291291
}
292292

293293
/// <summary>
294294
/// Compare between two ODataPath using its path item name.
295295
/// </summary>
296296
/// <param name="other">The compare to ODataPath.</param>
297297
/// <returns>true/false</returns>
298-
public int CompareTo(ODataPath other)
298+
public int CompareTo(ODataPath? other)
299299
{
300-
return GetPathItemName().CompareTo(other.GetPathItemName());
300+
return GetPathItemName().CompareTo(other?.GetPathItemName());
301301
}
302302

303303
private ODataPathKind CalcPathType()

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

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ private void AppendPath(ODataPath path)
142142
case ODataPathKind.MediaEntity:
143143
if (path.FirstSegment is ODataNavigationSourceSegment navigationSourceSegment)
144144
{
145-
if(!_allNavigationSourcePaths.TryGetValue(navigationSourceSegment.EntityType, out IList<ODataPath> nsList))
145+
if(!_allNavigationSourcePaths.TryGetValue(navigationSourceSegment.EntityType, out var nsList))
146146
{
147-
nsList = new List<ODataPath>();
147+
nsList = [];
148148
_allNavigationSourcePaths[navigationSourceSegment.EntityType] = nsList;
149149
}
150150

@@ -157,9 +157,9 @@ private void AppendPath(ODataPath path)
157157
}
158158
else
159159
{
160-
if (!_dollarCountPaths.TryGetValue(navigationSourceSegment.EntityType, out IList<ODataPath> dollarPathList))
160+
if (!_dollarCountPaths.TryGetValue(navigationSourceSegment.EntityType, out var dollarPathList))
161161
{
162-
dollarPathList = new List<ODataPath>();
162+
dollarPathList = [];
163163
_dollarCountPaths[navigationSourceSegment.EntityType] = dollarPathList;
164164
}
165165
dollarPathList.Add(path);
@@ -174,9 +174,9 @@ private void AppendPath(ODataPath path)
174174
case ODataPathKind.Ref:
175175
ODataNavigationPropertySegment navigationPropertySegment = path.OfType<ODataNavigationPropertySegment>().Last();
176176

177-
if (!_allNavigationPropertyPaths.TryGetValue(navigationPropertySegment.EntityType, out IList<ODataPath> npList))
177+
if (!_allNavigationPropertyPaths.TryGetValue(navigationPropertySegment.EntityType, out var npList))
178178
{
179-
npList = new List<ODataPath>();
179+
npList = [];
180180
_allNavigationPropertyPaths[navigationPropertySegment.EntityType] = npList;
181181
}
182182

@@ -190,7 +190,7 @@ private void AppendPath(ODataPath path)
190190
foreach (var kvp in _dollarCountPaths)
191191
{
192192
if (kvp.Value.FirstOrDefault(p => DollarCountAndOperationPathsSimilar(p, path)) is ODataPath dollarCountPath &&
193-
_allNavigationSourcePaths.TryGetValue(kvp.Key, out IList<ODataPath> dollarPathList))
193+
_allNavigationSourcePaths.TryGetValue(kvp.Key, out var dollarPathList))
194194
{
195195
dollarPathList.Remove(dollarCountPath);
196196
break;
@@ -208,19 +208,19 @@ private void AppendPath(ODataPath path)
208208
bool DollarCountAndOperationPathsSimilar(ODataPath path1, ODataPath path2)
209209
{
210210
if ((path1.Kind == ODataPathKind.DollarCount &&
211-
path2.Kind == ODataPathKind.Operation && path2.LastSegment.Identifier.Equals(Constants.CountSegmentIdentifier, StringComparison.OrdinalIgnoreCase)) ||
211+
path2.Kind == ODataPathKind.Operation && Constants.CountSegmentIdentifier.Equals(path2.LastSegment?.Identifier, StringComparison.OrdinalIgnoreCase)) ||
212212
(path2.Kind == ODataPathKind.DollarCount &&
213-
path1.Kind == ODataPathKind.Operation && path1.LastSegment.Identifier.Equals(Constants.CountSegmentIdentifier, StringComparison.OrdinalIgnoreCase)))
213+
path1.Kind == ODataPathKind.Operation && Constants.CountSegmentIdentifier.Equals(path1.LastSegment?.Identifier, StringComparison.OrdinalIgnoreCase)))
214214
{
215215
return GetModifiedPathItemName(path1)?.Equals(GetModifiedPathItemName(path2), StringComparison.OrdinalIgnoreCase) ?? false;
216216
}
217217

218218
return false;
219219
}
220220

221-
string GetModifiedPathItemName(ODataPath path)
221+
string? GetModifiedPathItemName(ODataPath path)
222222
{
223-
if (!path.Any()) return null;
223+
if (path.Count == 0) return null;
224224

225225
IEnumerable<ODataSegment> modifiedSegments = path.Take(path.Count - 1);
226226
ODataPath modifiedPath = new(modifiedSegments);
@@ -242,14 +242,15 @@ private void RetrieveNavigationSourcePaths(IEdmNavigationSource navigationSource
242242
ODataPath path = new(new ODataNavigationSourceSegment(navigationSource));
243243
AppendPath(path.Clone());
244244

245-
IEdmEntitySet entitySet = navigationSource as IEdmEntitySet;
246245
IEdmEntityType entityType = navigationSource.EntityType;
247-
CountRestrictionsType count = null;
246+
CountRestrictionsType? count = null;
248247
bool? indexableByKey = false;
248+
var entitySetIsNull = true;
249249

250250
// for entity set, create a path with key and a $count path
251-
if (entitySet != null)
251+
if (navigationSource is IEdmEntitySet entitySet && _model is not null)
252252
{
253+
entitySetIsNull = false;
253254
string targetPath = path.GetTargetPath(_model);
254255
count = _model.GetRecord<CountRestrictionsType>(targetPath, CapabilitiesConstants.CountRestrictions)
255256
?? _model.GetRecord<CountRestrictionsType>(entitySet, CapabilitiesConstants.CountRestrictions);
@@ -291,13 +292,13 @@ private void RetrieveNavigationSourcePaths(IEdmNavigationSource navigationSource
291292
}
292293
}
293294

294-
if (entitySet != null && (indexableByKey ?? true))
295+
if (!entitySetIsNull && (indexableByKey ?? true))
295296
{
296297
path.Pop(); // end of entity
297298
}
298299

299300
path.Pop(); // end of navigation source.
300-
Debug.Assert(path.Any() == false);
301+
Debug.Assert(path.Count == 0);
301302
}
302303

303304
/// <summary>
@@ -312,7 +313,7 @@ private void RetrieveComplexPropertyPaths(IEdmEntityType entityType, ODataPath c
312313
Utils.CheckArgumentNull(currentPath, nameof(currentPath));
313314
Utils.CheckArgumentNull(convertSettings, nameof(convertSettings));
314315

315-
if (!convertSettings.EnableNavigationPropertyPath) return;
316+
if (!convertSettings.EnableNavigationPropertyPath || _model is null) return;
316317

317318
foreach (IEdmStructuralProperty sp in entityType.StructuralProperties()
318319
.Where(x => x.Type.IsComplex() ||
@@ -327,9 +328,9 @@ private void RetrieveComplexPropertyPaths(IEdmEntityType entityType, ODataPath c
327328
}
328329
AppendPath(currentPath.Clone());
329330

330-
if (sp.Type.IsCollection())
331+
if (sp.Type.IsCollection() && sp.Type.Definition.AsElementType() is IEdmComplexType elemType)
331332
{
332-
CreateTypeCastPaths(currentPath, convertSettings, sp.Type.Definition.AsElementType() as IEdmComplexType, sp, true);
333+
CreateTypeCastPaths(currentPath, convertSettings, elemType, sp, true);
333334
var isCountable = _model.GetRecord<CountRestrictionsType>(targetPath, CapabilitiesConstants.CountRestrictions)?.IsCountable
334335
?? _model.GetRecord<CountRestrictionsType>(sp, CapabilitiesConstants.CountRestrictions)?.IsCountable
335336
?? true;
@@ -373,7 +374,7 @@ private bool RetrieveComplexTypeNavigationPropertyPaths(IEdmComplexType complexT
373374
.Distinct()
374375
.Where(CanFilter);
375376

376-
if (!navigationProperties.Any()) return false;
377+
if (!navigationProperties.Any() || _model is null) return false;
377378

378379
foreach (var np in navigationProperties)
379380
{
@@ -434,14 +435,14 @@ private bool ShouldCreateComplexPropertyPaths(IEdmStructuralProperty complexProp
434435
if (!convertSettings.RequireRestrictionAnnotationsToGenerateComplexPropertyPaths)
435436
return true;
436437

437-
bool isReadable = _model.GetRecord<ReadRestrictionsType>(targetPath, CapabilitiesConstants.ReadRestrictions)?.Readable
438-
?? _model.GetRecord<ReadRestrictionsType>(complexProperty, CapabilitiesConstants.ReadRestrictions)?.Readable
438+
bool isReadable = _model?.GetRecord<ReadRestrictionsType>(targetPath, CapabilitiesConstants.ReadRestrictions)?.Readable
439+
?? _model?.GetRecord<ReadRestrictionsType>(complexProperty, CapabilitiesConstants.ReadRestrictions)?.Readable
439440
?? false;
440-
bool isUpdatable = _model.GetRecord<UpdateRestrictionsType>(targetPath, CapabilitiesConstants.UpdateRestrictions)?.Updatable
441-
??_model.GetRecord<UpdateRestrictionsType>(complexProperty, CapabilitiesConstants.UpdateRestrictions)?.Updatable
441+
bool isUpdatable = _model?.GetRecord<UpdateRestrictionsType>(targetPath, CapabilitiesConstants.UpdateRestrictions)?.Updatable
442+
??_model?.GetRecord<UpdateRestrictionsType>(complexProperty, CapabilitiesConstants.UpdateRestrictions)?.Updatable
442443
?? false;
443-
bool isInsertable = _model.GetRecord<InsertRestrictionsType>(targetPath, CapabilitiesConstants.InsertRestrictions)?.Insertable
444-
?? _model.GetRecord<InsertRestrictionsType>(complexProperty, CapabilitiesConstants.InsertRestrictions)?.Insertable
444+
bool isInsertable = _model?.GetRecord<InsertRestrictionsType>(targetPath, CapabilitiesConstants.InsertRestrictions)?.Insertable
445+
?? _model?.GetRecord<InsertRestrictionsType>(complexProperty, CapabilitiesConstants.InsertRestrictions)?.Insertable
445446
?? false;
446447

447448
return isReadable || isUpdatable || isInsertable;
@@ -494,10 +495,10 @@ private void RetrieveMediaEntityStreamPaths(IEdmEntityType entityType, ODataPath
494495
/// <param name="visitedNavigationProperties">A stack that holds the visited navigation properties in the <paramref name="currentPath"/>.</param>
495496
private void RetrieveNavigationPropertyPaths(
496497
IEdmNavigationProperty navigationProperty,
497-
CountRestrictionsType count,
498+
CountRestrictionsType? count,
498499
ODataPath currentPath,
499500
OpenApiConvertSettings convertSettings,
500-
Stack<string> visitedNavigationProperties = null)
501+
Stack<string>? visitedNavigationProperties = null)
501502
{
502503
Utils.CheckArgumentNull(navigationProperty, nameof(navigationProperty));
503504
Utils.CheckArgumentNull(currentPath, nameof(currentPath));
@@ -519,17 +520,17 @@ private void RetrieveNavigationPropertyPaths(
519520

520521
// Get the annotatable navigation source for this navigation property.
521522
IEdmVocabularyAnnotatable annotatableNavigationSource = (currentPath.FirstSegment as ODataNavigationSourceSegment)?.NavigationSource as IEdmVocabularyAnnotatable;
522-
NavigationRestrictionsType navSourceRestrictionType = null;
523-
NavigationRestrictionsType navPropRestrictionType = null;
523+
NavigationRestrictionsType? navSourceRestrictionType = null;
524+
NavigationRestrictionsType? navPropRestrictionType = null;
524525

525526
// Get the NavigationRestrictions referenced by this navigation property: Can be defined in the navigation source or in-lined in the navigation property.
526-
if (annotatableNavigationSource != null)
527+
if (annotatableNavigationSource != null && _model is not null)
527528
{
528529
navSourceRestrictionType = _model.GetRecord<NavigationRestrictionsType>(annotatableNavigationSource, CapabilitiesConstants.NavigationRestrictions);
529530
navPropRestrictionType = _model.GetRecord<NavigationRestrictionsType>(navigationProperty, CapabilitiesConstants.NavigationRestrictions);
530531
}
531532

532-
NavigationPropertyRestriction restriction = navSourceRestrictionType?.RestrictedProperties?
533+
NavigationPropertyRestriction? restriction = navSourceRestrictionType?.RestrictedProperties?
533534
.FirstOrDefault(r => r.NavigationProperty == currentPath.NavigationPropertyPath(navigationProperty.Name))
534535
?? navPropRestrictionType?.RestrictedProperties?.FirstOrDefault();
535536

@@ -1162,10 +1163,13 @@ private void AppendBoundOperationOnDerivedNavigationPropertyPath(
11621163
continue;
11631164
}
11641165

1165-
ODataPath newPath = path.Clone();
1166-
newPath.Push(new ODataTypeCastSegment(bindingEntityType, _model));
1167-
newPath.Push(new ODataOperationSegment(edmOperation, isEscapedFunction, _model));
1168-
AppendPath(newPath);
1166+
if (_model is not null)
1167+
{
1168+
ODataPath newPath = path.Clone();
1169+
newPath.Push(new ODataTypeCastSegment(bindingEntityType, _model));
1170+
newPath.Push(new ODataOperationSegment(edmOperation, isEscapedFunction, _model));
1171+
AppendPath(newPath);
1172+
}
11691173
}
11701174
}
11711175
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ protected override void SetBasicInfo(OpenApiOperation operation)
4141
{
4242
base.SetBasicInfo(operation);
4343

44-
ReadRestrictionsType readRestrictions = Context.Model.GetRecord<ReadRestrictionsType>(TargetPath, CapabilitiesConstants.ReadRestrictions);
45-
ReadRestrictionsType operationReadRestrictions = Context.Model.GetRecord<ReadRestrictionsType>(EdmOperation, CapabilitiesConstants.ReadRestrictions);
44+
var readRestrictions = string.IsNullOrEmpty(TargetPath) ? null : Context?.Model.GetRecord<ReadRestrictionsType>(TargetPath, CapabilitiesConstants.ReadRestrictions);
45+
var operationReadRestrictions = Context?.Model.GetRecord<ReadRestrictionsType>(EdmOperation, CapabilitiesConstants.ReadRestrictions);
4646
readRestrictions?.MergePropertiesIfNull(operationReadRestrictions);
4747
readRestrictions ??= operationReadRestrictions;
4848

0 commit comments

Comments
 (0)