@@ -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 }
0 commit comments