|
3 | 3 | // Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. |
4 | 4 | // ------------------------------------------------------------ |
5 | 5 |
|
| 6 | +using System; |
6 | 7 | using System.Collections.Generic; |
7 | 8 | using System.Linq; |
8 | 9 | using System.Net.Http; |
@@ -161,73 +162,66 @@ protected override void SetParameters(OpenApiOperation operation) |
161 | 162 | return; |
162 | 163 | } |
163 | 164 |
|
164 | | - var selectParameter = (string.IsNullOrEmpty(TargetPath) ? null : Context.CreateSelect(TargetPath, NavigationProperty.ToEntityType())) |
165 | | - ?? Context.CreateSelect(NavigationProperty); |
166 | | - |
167 | | - var expandParameter = (string.IsNullOrEmpty(TargetPath) ? null : Context.CreateExpand(TargetPath, NavigationProperty.ToEntityType())) |
168 | | - ?? Context.CreateExpand(NavigationProperty); |
169 | | - |
170 | | - if (!LastSegmentIsKeySegment && NavigationProperty.TargetMultiplicity() == EdmMultiplicity.Many) |
| 165 | + var (selectParameter, expandParameter) = (string.IsNullOrEmpty(TargetPath), NavigationProperty) switch |
| 166 | + { |
| 167 | + (false, not null) when NavigationProperty.ToEntityType() is {} entityType => |
| 168 | + (Context.CreateSelect(TargetPath!, entityType) ?? Context.CreateSelect(NavigationProperty), |
| 169 | + Context.CreateExpand(TargetPath!, entityType) ?? Context.CreateExpand(NavigationProperty)), |
| 170 | + (true, not null) => (Context.CreateSelect(NavigationProperty), Context.CreateExpand(NavigationProperty)), |
| 171 | + _ => (null, null), |
| 172 | + }; |
| 173 | + |
| 174 | + var parametersToAdd = new List<IOpenApiParameter>(); |
| 175 | + if (!LastSegmentIsKeySegment && NavigationProperty?.TargetMultiplicity() == EdmMultiplicity.Many) |
171 | 176 | { |
172 | 177 | // Need to verify that TopSupported or others should be applied to navigation source. |
173 | 178 | // So, how about for the navigation property. |
174 | | - var parameter = Context.CreateTop(TargetPath, _document) ?? Context.CreateTop(NavigationProperty, _document); |
175 | | - if (parameter != null) |
176 | | - { |
177 | | - operation.Parameters.Add(parameter); |
178 | | - } |
179 | | - |
180 | | - parameter = Context.CreateSkip(TargetPath, _document) ?? Context.CreateSkip(NavigationProperty, _document); |
181 | | - if (parameter != null) |
182 | | - { |
183 | | - operation.Parameters.Add(parameter); |
184 | | - } |
185 | | - |
186 | | - parameter = Context.CreateSearch(TargetPath, _document) ?? Context.CreateSearch(NavigationProperty, _document); |
187 | | - if (parameter != null) |
188 | | - { |
189 | | - operation.Parameters.Add(parameter); |
190 | | - } |
191 | | - |
192 | | - parameter = Context.CreateFilter(TargetPath, _document) ?? Context.CreateFilter(NavigationProperty, _document); |
193 | | - if (parameter != null) |
194 | | - { |
195 | | - operation.Parameters.Add(parameter); |
196 | | - } |
197 | | - |
198 | | - parameter = Context.CreateCount(TargetPath, _document) ?? Context.CreateCount(NavigationProperty, _document); |
199 | | - if (parameter != null) |
| 179 | + AddParameterIfExists(parametersToAdd, Context.CreateTop, Context.CreateTop); |
| 180 | + AddParameterIfExists(parametersToAdd, Context.CreateSkip, Context.CreateSkip); |
| 181 | + AddParameterIfExists(parametersToAdd, Context.CreateSearch, Context.CreateSearch); |
| 182 | + AddParameterIfExists(parametersToAdd, Context.CreateFilter, Context.CreateFilter); |
| 183 | + AddParameterIfExists(parametersToAdd, Context.CreateCount, Context.CreateCount); |
| 184 | + |
| 185 | + var orderByParameter = (string.IsNullOrEmpty(TargetPath), NavigationProperty) switch |
| 186 | + { |
| 187 | + (false, not null) when NavigationProperty.ToEntityType() is {} entityType => |
| 188 | + Context.CreateOrderBy(TargetPath!, entityType), |
| 189 | + (true, not null) => Context.CreateOrderBy(NavigationProperty), |
| 190 | + _ => null, |
| 191 | + }; |
| 192 | + if (orderByParameter != null) |
200 | 193 | { |
201 | | - operation.Parameters.Add(parameter); |
| 194 | + parametersToAdd.Add(orderByParameter); |
202 | 195 | } |
| 196 | + } |
203 | 197 |
|
204 | | - parameter = Context.CreateOrderBy(TargetPath, NavigationProperty.ToEntityType()) ?? Context.CreateOrderBy(NavigationProperty); |
205 | | - if (parameter != null) |
206 | | - { |
207 | | - operation.Parameters.Add(parameter); |
208 | | - } |
| 198 | + if (selectParameter != null) |
| 199 | + { |
| 200 | + parametersToAdd.Add(selectParameter); |
| 201 | + } |
209 | 202 |
|
210 | | - if (selectParameter != null) |
211 | | - { |
212 | | - operation.Parameters.Add(selectParameter); |
213 | | - } |
| 203 | + if (expandParameter != null) |
| 204 | + { |
| 205 | + parametersToAdd.Add(expandParameter); |
| 206 | + } |
214 | 207 |
|
215 | | - if (expandParameter != null) |
216 | | - { |
217 | | - operation.Parameters.Add(expandParameter); |
218 | | - } |
| 208 | + if (parametersToAdd.Count > 0) |
| 209 | + { |
| 210 | + if (operation.Parameters is null) operation.Parameters = parametersToAdd; |
| 211 | + else parametersToAdd.ForEach(p => operation.Parameters.Add(p)); |
219 | 212 | } |
220 | | - else |
| 213 | + } |
| 214 | + private void AddParameterIfExists(List<IOpenApiParameter> parameters, |
| 215 | + Func<string, OpenApiDocument, IOpenApiParameter?> createParameterFromPath, |
| 216 | + Func<IEdmNavigationProperty, OpenApiDocument, IOpenApiParameter?> createParameterFromProperty) |
| 217 | + { |
| 218 | + if (!string.IsNullOrEmpty(TargetPath) && createParameterFromPath(TargetPath, _document) is {} parameterFromPath) |
221 | 219 | { |
222 | | - if (selectParameter != null) |
223 | | - { |
224 | | - operation.Parameters.Add(selectParameter); |
225 | | - } |
226 | | - |
227 | | - if (expandParameter != null) |
228 | | - { |
229 | | - operation.Parameters.Add(expandParameter); |
230 | | - } |
| 220 | + parameters.Add(parameterFromPath); |
| 221 | + } |
| 222 | + else if (NavigationProperty is not null && createParameterFromProperty(NavigationProperty, _document) is {} parameterFromProperty) |
| 223 | + { |
| 224 | + parameters.Add(parameterFromProperty); |
231 | 225 | } |
232 | 226 | } |
233 | 227 |
|
|
0 commit comments