33// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
44// ------------------------------------------------------------
55
6+ using System ;
7+ using System . Collections . Generic ;
68using System . Linq ;
79using System . Net . Http ;
810using System . Text . Json . Nodes ;
911using Microsoft . OData . Edm ;
1012using Microsoft . OpenApi . Any ;
13+ using Microsoft . OpenApi . Interfaces ;
1114using Microsoft . OpenApi . Models ;
1215using Microsoft . OpenApi . Models . References ;
1316using Microsoft . OpenApi . OData . Common ;
@@ -36,16 +39,20 @@ public EntitySetGetOperationHandler(OpenApiDocument document) : base(document)
3639 /// <inheritdoc/>
3740 public override HttpMethod OperationType => HttpMethod . Get ;
3841
39- private ReadRestrictionsType _readRestrictions ;
42+ private ReadRestrictionsType ? _readRestrictions ;
4043
4144 protected override void Initialize ( ODataContext context , ODataPath path )
4245 {
4346 base . Initialize ( context , path ) ;
4447
45- _readRestrictions = Context . Model . GetRecord < ReadRestrictionsType > ( TargetPath , CapabilitiesConstants . ReadRestrictions ) ;
46- var entityReadRestrictions = Context . Model . GetRecord < ReadRestrictionsType > ( EntitySet , CapabilitiesConstants . ReadRestrictions ) ;
47- _readRestrictions ? . MergePropertiesIfNull ( entityReadRestrictions ) ;
48- _readRestrictions ??= entityReadRestrictions ;
48+ if ( ! string . IsNullOrEmpty ( TargetPath ) )
49+ _readRestrictions = Context ? . Model . GetRecord < ReadRestrictionsType > ( TargetPath , CapabilitiesConstants . ReadRestrictions ) ;
50+ if ( Context is not null )
51+ {
52+ var entityReadRestrictions = Context . Model . GetRecord < ReadRestrictionsType > ( EntitySet , CapabilitiesConstants . ReadRestrictions ) ;
53+ _readRestrictions ? . MergePropertiesIfNull ( entityReadRestrictions ) ;
54+ _readRestrictions ??= entityReadRestrictions ;
55+ }
4956 }
5057
5158 /// <inheritdoc/>
@@ -54,10 +61,10 @@ protected override void SetBasicInfo(OpenApiOperation operation)
5461 // Summary and Descriptions
5562 string placeHolder = "Get entities from " + EntitySet . Name ;
5663 operation . Summary = _readRestrictions ? . Description ?? placeHolder ;
57- operation . Description = _readRestrictions ? . LongDescription ?? Context . Model . GetDescriptionAnnotation ( EntitySet ) ;
64+ operation . Description = _readRestrictions ? . LongDescription ?? Context ? . Model . GetDescriptionAnnotation ( EntitySet ) ;
5865
5966 // OperationId
60- if ( Context . Settings . EnableOperationId )
67+ if ( Context is { Settings . EnableOperationId : true } )
6168 {
6269 string typeName = EntitySet . EntityType . Name ;
6370 operation . OperationId = EntitySet . Name + "." + typeName + ".List" + Utils . UpperFirstChar ( typeName ) ;
@@ -66,13 +73,14 @@ protected override void SetBasicInfo(OpenApiOperation operation)
6673
6774 protected override void SetExtensions ( OpenApiOperation operation )
6875 {
69- if ( Context . Settings . EnablePagination )
76+ if ( Context is { Settings . EnablePagination : true } )
7077 {
71- JsonObject extension = new JsonObject
78+ var extension = new JsonObject
7279 {
7380 { "nextLinkName" , "@odata.nextLink" } ,
7481 { "operationName" , Context . Settings . PageableOperationName }
7582 } ;
83+ operation . Extensions ??= new Dictionary < string , IOpenApiExtension > ( ) ;
7684 operation . Extensions . Add ( Constants . xMsPageable , new OpenApiAny ( extension ) ) ;
7785
7886 base . SetExtensions ( operation ) ;
@@ -83,41 +91,43 @@ protected override void SetExtensions(OpenApiOperation operation)
8391 protected override void SetParameters ( OpenApiOperation operation )
8492 {
8593 base . SetParameters ( operation ) ;
94+ if ( Context is null ) return ;
8695
8796 // The parameters array contains Parameter Objects for all system query options allowed for this collection,
8897 // and it does not list system query options not allowed for this collection, see terms
8998 // Capabilities.TopSupported, Capabilities.SkipSupported, Capabilities.SearchRestrictions,
9099 // Capabilities.FilterRestrictions, and Capabilities.CountRestrictions
91100 // $top
92- var parameter = Context . CreateTop ( TargetPath , _document ) ?? Context . CreateTop ( EntitySet , _document ) ;
101+ operation . Parameters ??= [ ] ;
102+ var parameter = ( TargetPath is null ? null : Context . CreateTop ( TargetPath , _document ) ) ?? Context . CreateTop ( EntitySet , _document ) ;
93103 if ( parameter != null )
94104 {
95105 operation . Parameters . Add ( parameter ) ;
96106 }
97107
98108 // $skip
99- parameter = Context . CreateSkip ( TargetPath , _document ) ?? Context . CreateSkip ( EntitySet , _document ) ;
109+ parameter = ( TargetPath is null ? null : Context . CreateSkip ( TargetPath , _document ) ) ?? Context . CreateSkip ( EntitySet , _document ) ;
100110 if ( parameter != null )
101111 {
102112 operation . Parameters . Add ( parameter ) ;
103113 }
104114
105115 // $search
106- parameter = Context . CreateSearch ( TargetPath , _document ) ?? Context . CreateSearch ( EntitySet , _document ) ;
116+ parameter = ( TargetPath is null ? null : Context . CreateSearch ( TargetPath , _document ) ) ?? Context . CreateSearch ( EntitySet , _document ) ;
107117 if ( parameter != null )
108118 {
109119 operation . Parameters . Add ( parameter ) ;
110120 }
111121
112122 // $filter
113- parameter = Context . CreateFilter ( TargetPath , _document ) ?? Context . CreateFilter ( EntitySet , _document ) ;
123+ parameter = ( TargetPath is null ? null : Context . CreateFilter ( TargetPath , _document ) ) ?? Context . CreateFilter ( EntitySet , _document ) ;
114124 if ( parameter != null )
115125 {
116126 operation . Parameters . Add ( parameter ) ;
117127 }
118128
119129 // $count
120- parameter = Context . CreateCount ( TargetPath , _document ) ?? Context . CreateCount ( EntitySet , _document ) ;
130+ parameter = ( TargetPath is null ? null : Context . CreateCount ( TargetPath , _document ) ) ?? Context . CreateCount ( EntitySet , _document ) ;
121131 if ( parameter != null )
122132 {
123133 operation . Parameters . Add ( parameter ) ;
@@ -128,21 +138,21 @@ protected override void SetParameters(OpenApiOperation operation)
128138 // of just providing a comma-separated list of properties can be expressed via an array-valued
129139 // parameter with an enum constraint
130140 // $order
131- parameter = Context . CreateOrderBy ( TargetPath , EntitySet . EntityType ) ?? Context . CreateOrderBy ( EntitySet ) ;
141+ parameter = ( TargetPath is null ? null : Context . CreateOrderBy ( TargetPath , EntitySet . EntityType ) ) ?? Context . CreateOrderBy ( EntitySet ) ;
132142 if ( parameter != null )
133143 {
134144 operation . Parameters . Add ( parameter ) ;
135145 }
136146
137147 // $select
138- parameter = Context . CreateSelect ( TargetPath , EntitySet . EntityType ) ?? Context . CreateSelect ( EntitySet ) ;
148+ parameter = ( TargetPath is null ? null : Context . CreateSelect ( TargetPath , EntitySet . EntityType ) ) ?? Context . CreateSelect ( EntitySet ) ;
139149 if ( parameter != null )
140150 {
141151 operation . Parameters . Add ( parameter ) ;
142152 }
143153
144154 // $expand
145- parameter = Context . CreateExpand ( TargetPath , EntitySet . EntityType ) ?? Context . CreateExpand ( EntitySet ) ;
155+ parameter = ( TargetPath is null ? null : Context . CreateExpand ( TargetPath , EntitySet . EntityType ) ) ?? Context . CreateExpand ( EntitySet ) ;
146156 if ( parameter != null )
147157 {
148158 operation . Parameters . Add ( parameter ) ;
@@ -155,12 +165,13 @@ protected override void SetResponses(OpenApiOperation operation)
155165 operation . Responses = new OpenApiResponses
156166 {
157167 {
158- Context . Settings . UseSuccessStatusCodeRange ? Constants . StatusCodeClass2XX : Constants . StatusCode200 ,
168+ Context ? . Settings . UseSuccessStatusCodeRange ?? false ? Constants . StatusCodeClass2XX : Constants . StatusCode200 ,
159169 new OpenApiResponseReference ( $ "{ EntitySet . EntityType . FullName ( ) } { Constants . CollectionSchemaSuffix } ", _document )
160170 }
161171 } ;
162172
163- operation . AddErrorResponses ( Context . Settings , _document , false ) ;
173+ if ( Context is not null )
174+ operation . AddErrorResponses ( Context . Settings , _document , false ) ;
164175
165176 base . SetResponses ( operation ) ;
166177 }
@@ -172,7 +183,7 @@ protected override void SetSecurity(OpenApiOperation operation)
172183 return ;
173184 }
174185
175- operation . Security = Context . CreateSecurityRequirements ( _readRestrictions . Permissions , _document ) . ToList ( ) ;
186+ operation . Security = Context ? . CreateSecurityRequirements ( _readRestrictions . Permissions , _document ) . ToList ( ) ;
176187 }
177188
178189 protected override void AppendCustomParameters ( OpenApiOperation operation )
0 commit comments