Skip to content

Commit 255a84b

Browse files
committed
chore: additional NRT fixes
Signed-off-by: Vincent Biret <[email protected]>
1 parent 93f46f0 commit 255a84b

File tree

2 files changed

+52
-59
lines changed

2 files changed

+52
-59
lines changed

src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiParameterGenerator.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -689,8 +689,7 @@ public static void AppendParameter(this IList<IOpenApiParameter> parameters, IOp
689689
Utils.CheckArgumentNull(context, nameof(context));
690690
Utils.CheckArgumentNull(targetPath, nameof(targetPath));
691691

692-
IEdmTargetPath target = context.Model.GetTargetPath(targetPath);
693-
if (target == null)
692+
if (context.Model.GetTargetPath(targetPath) is not {} target)
694693
return null;
695694

696695
return context.CreateSelect(target, entityType);

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

Lines changed: 51 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
44
// ------------------------------------------------------------
55

6+
using System;
67
using System.Collections.Generic;
78
using System.Linq;
89
using System.Net.Http;
@@ -161,73 +162,66 @@ protected override void SetParameters(OpenApiOperation operation)
161162
return;
162163
}
163164

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)
171176
{
172177
// Need to verify that TopSupported or others should be applied to navigation source.
173178
// 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)
200193
{
201-
operation.Parameters.Add(parameter);
194+
parametersToAdd.Add(orderByParameter);
202195
}
196+
}
203197

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+
}
209202

210-
if (selectParameter != null)
211-
{
212-
operation.Parameters.Add(selectParameter);
213-
}
203+
if (expandParameter != null)
204+
{
205+
parametersToAdd.Add(expandParameter);
206+
}
214207

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));
219212
}
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)
221219
{
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);
231225
}
232226
}
233227

0 commit comments

Comments
 (0)