33// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
44// ------------------------------------------------------------
55
6+ using System . Collections . Generic ;
67using System . Linq ;
78using System . Net . Http ;
89using System . Text . Json . Nodes ;
910using Microsoft . OData . Edm ;
1011using Microsoft . OpenApi . Any ;
12+ using Microsoft . OpenApi . Interfaces ;
1113using Microsoft . OpenApi . Models ;
1214using Microsoft . OpenApi . Models . Interfaces ;
1315using Microsoft . OpenApi . Models . References ;
@@ -31,14 +33,14 @@ public ComplexPropertyGetOperationHandler(OpenApiDocument document):base(documen
3133 /// <inheritdoc />
3234 public override HttpMethod OperationType => HttpMethod . Get ;
3335
34- private ReadRestrictionsType _readRestrictions ;
36+ private ReadRestrictionsType ? _readRestrictions ;
3537
3638 protected override void Initialize ( ODataContext context , ODataPath path )
3739 {
3840 base . Initialize ( context , path ) ;
3941
40- _readRestrictions = Context . Model . GetRecord < ReadRestrictionsType > ( TargetPath , CapabilitiesConstants . ReadRestrictions ) ;
41- var complexPropertyReadRestrictions = Context . Model . GetRecord < ReadRestrictionsType > ( ComplexPropertySegment . Property , CapabilitiesConstants . ReadRestrictions ) ;
42+ _readRestrictions = string . IsNullOrEmpty ( TargetPath ) ? null : Context ? . Model . GetRecord < ReadRestrictionsType > ( TargetPath , CapabilitiesConstants . ReadRestrictions ) ;
43+ var complexPropertyReadRestrictions = Context ? . Model . GetRecord < ReadRestrictionsType > ( ComplexPropertySegment . Property , CapabilitiesConstants . ReadRestrictions ) ;
4244 _readRestrictions ? . MergePropertiesIfNull ( complexPropertyReadRestrictions ) ;
4345 _readRestrictions ??= complexPropertyReadRestrictions ;
4446 }
@@ -47,7 +49,7 @@ protected override void Initialize(ODataContext context, ODataPath path)
4749 protected override void SetBasicInfo ( OpenApiOperation operation )
4850 {
4951 // OperationId
50- if ( Context . Settings . EnableOperationId )
52+ if ( Context is { Settings . EnableOperationId : true } && Path is not null )
5153 {
5254 string prefix = ComplexPropertySegment . Property . Type . IsCollection ( ) ? "List" : "Get" ;
5355 operation . OperationId = EdmModelHelper . GenerateComplexPropertyPathOperationId ( Path , Context , prefix ) ;
@@ -56,7 +58,7 @@ protected override void SetBasicInfo(OpenApiOperation operation)
5658 // Summary and Description
5759 string placeHolder = $ "Get { ComplexPropertySegment . Property . Name } property value";
5860 operation . Summary = _readRestrictions ? . Description ?? placeHolder ;
59- operation . Description = _readRestrictions ? . LongDescription ?? Context . Model . GetDescriptionAnnotation ( ComplexPropertySegment . Property ) ;
61+ operation . Description = _readRestrictions ? . LongDescription ?? Context ? . Model . GetDescriptionAnnotation ( ComplexPropertySegment . Property ) ;
6062
6163 base . SetBasicInfo ( operation ) ;
6264 }
@@ -65,43 +67,46 @@ protected override void SetParameters(OpenApiOperation operation)
6567 {
6668 base . SetParameters ( operation ) ;
6769
68- IOpenApiParameter parameter ;
70+ if ( Context is null ) return ;
71+
72+ IOpenApiParameter ? parameter ;
73+ operation . Parameters ??= [ ] ;
6974 if ( ComplexPropertySegment . Property . Type . IsCollection ( ) )
7075 {
7176 // The parameters array contains Parameter Objects for all system query options allowed for this collection,
7277 // and it does not list system query options not allowed for this collection, see terms
7378 // Capabilities.TopSupported, Capabilities.SkipSupported, Capabilities.SearchRestrictions,
7479 // Capabilities.FilterRestrictions, and Capabilities.CountRestrictions
7580 // $top
76- parameter = Context . CreateTop ( TargetPath , _document ) ?? Context . CreateTop ( ComplexPropertySegment . Property , _document ) ;
81+ parameter = ( string . IsNullOrEmpty ( TargetPath ) ? null : Context . CreateTop ( TargetPath , _document ) ) ?? Context . CreateTop ( ComplexPropertySegment . Property , _document ) ;
7782 if ( parameter != null )
7883 {
7984 operation . Parameters . Add ( parameter ) ;
8085 }
8186
8287 // $skip
83- parameter = Context . CreateSkip ( TargetPath , _document ) ?? Context . CreateSkip ( ComplexPropertySegment . Property , _document ) ;
88+ parameter = ( string . IsNullOrEmpty ( TargetPath ) ? null : Context . CreateSkip ( TargetPath , _document ) ) ?? Context . CreateSkip ( ComplexPropertySegment . Property , _document ) ;
8489 if ( parameter != null )
8590 {
8691 operation . Parameters . Add ( parameter ) ;
8792 }
8893
8994 // $search
90- parameter = Context . CreateSearch ( TargetPath , _document ) ?? Context . CreateSearch ( ComplexPropertySegment . Property , _document ) ;
95+ parameter = ( string . IsNullOrEmpty ( TargetPath ) ? null : Context . CreateSearch ( TargetPath , _document ) ) ?? Context . CreateSearch ( ComplexPropertySegment . Property , _document ) ;
9196 if ( parameter != null )
9297 {
9398 operation . Parameters . Add ( parameter ) ;
9499 }
95100
96101 // $filter
97- parameter = Context . CreateFilter ( TargetPath , _document ) ?? Context . CreateFilter ( ComplexPropertySegment . Property , _document ) ;
102+ parameter = ( string . IsNullOrEmpty ( TargetPath ) ? null : Context . CreateFilter ( TargetPath , _document ) ) ?? Context . CreateFilter ( ComplexPropertySegment . Property , _document ) ;
98103 if ( parameter != null )
99104 {
100105 operation . Parameters . Add ( parameter ) ;
101106 }
102107
103108 // $count
104- parameter = Context . CreateCount ( TargetPath , _document ) ?? Context . CreateCount ( ComplexPropertySegment . Property , _document ) ;
109+ parameter = ( string . IsNullOrEmpty ( TargetPath ) ? null : Context . CreateCount ( TargetPath , _document ) ) ?? Context . CreateCount ( ComplexPropertySegment . Property , _document ) ;
105110 if ( parameter != null )
106111 {
107112 operation . Parameters . Add ( parameter ) ;
@@ -112,7 +117,7 @@ protected override void SetParameters(OpenApiOperation operation)
112117 // of just providing a comma-separated list of properties can be expressed via an array-valued
113118 // parameter with an enum constraint
114119 // $order
115- parameter = Context . CreateOrderBy ( TargetPath , ComplexPropertySegment . ComplexType )
120+ parameter = ( string . IsNullOrEmpty ( TargetPath ) ? null : Context . CreateOrderBy ( TargetPath , ComplexPropertySegment . ComplexType ) )
116121 ?? Context . CreateOrderBy ( ComplexPropertySegment . Property , ComplexPropertySegment . ComplexType ) ;
117122 if ( parameter != null )
118123 {
@@ -121,15 +126,15 @@ protected override void SetParameters(OpenApiOperation operation)
121126 }
122127
123128 // $select
124- parameter = Context . CreateSelect ( TargetPath , ComplexPropertySegment . ComplexType )
129+ parameter = ( string . IsNullOrEmpty ( TargetPath ) ? null : Context . CreateSelect ( TargetPath , ComplexPropertySegment . ComplexType ) )
125130 ?? Context . CreateSelect ( ComplexPropertySegment . Property , ComplexPropertySegment . ComplexType ) ;
126131 if ( parameter != null )
127132 {
128133 operation . Parameters . Add ( parameter ) ;
129134 }
130135
131136 // $expand
132- parameter = Context . CreateExpand ( TargetPath , ComplexPropertySegment . ComplexType )
137+ parameter = ( string . IsNullOrEmpty ( TargetPath ) ? null : Context . CreateExpand ( TargetPath , ComplexPropertySegment . ComplexType ) )
133138 ?? Context . CreateExpand ( ComplexPropertySegment . Property , ComplexPropertySegment . ComplexType ) ;
134139 if ( parameter != null )
135140 {
@@ -139,13 +144,14 @@ protected override void SetParameters(OpenApiOperation operation)
139144
140145 protected override void SetExtensions ( OpenApiOperation operation )
141146 {
142- if ( Context . Settings . EnablePagination && ComplexPropertySegment . Property . Type . IsCollection ( ) )
147+ if ( Context is { Settings . EnablePagination : true } && ComplexPropertySegment . Property . Type . IsCollection ( ) )
143148 {
144149 JsonObject extension = new ( )
145150 {
146151 { "nextLinkName" , "@odata.nextLink" } ,
147152 { "operationName" , Context . Settings . PageableOperationName }
148153 } ;
154+ operation . Extensions ??= new Dictionary < string , IOpenApiExtension > ( ) ;
149155 operation . Extensions . Add ( Constants . xMsPageable , new OpenApiAny ( extension ) ) ;
150156
151157 base . SetExtensions ( operation ) ;
@@ -165,7 +171,8 @@ protected override void SetResponses(OpenApiOperation operation)
165171 SetSingleResponse ( operation , schema ) ;
166172 }
167173
168- operation . AddErrorResponses ( Context . Settings , _document , false ) ;
174+ if ( Context is not null )
175+ operation . AddErrorResponses ( Context . Settings , _document , false ) ;
169176 base . SetResponses ( operation ) ;
170177 }
171178
@@ -176,7 +183,7 @@ protected override void SetSecurity(OpenApiOperation operation)
176183 return ;
177184 }
178185
179- operation . Security = Context . CreateSecurityRequirements ( _readRestrictions . Permissions , _document ) . ToList ( ) ;
186+ operation . Security = Context ? . CreateSecurityRequirements ( _readRestrictions . Permissions , _document ) . ToList ( ) ;
180187 }
181188
182189 protected override void AppendCustomParameters ( OpenApiOperation operation )
0 commit comments