Skip to content

Commit dd1ec47

Browse files
irvinesundaybaywet
andauthored
Appends OData query parameters to functions (#267)
* Update test and integration files * Add system query option parameters to functions * Simplify retrieval of IndexableByKey annotation * Remove unnecessary usings * Update src/Microsoft.OpenApi.OData.Reader/Operation/EdmOperationOperationHandler.cs Co-authored-by: Vincent Biret <[email protected]> * Rename variables; get $select and $orderby parameters * Update integration files Co-authored-by: Vincent Biret <[email protected]>
1 parent 7439ab5 commit dd1ec47

File tree

7 files changed

+1575
-9
lines changed

7 files changed

+1575
-9
lines changed

src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,16 +403,17 @@ private void RetrieveNavigationPropertyPaths(
403403
visitedNavigationProperties.Push(navPropFullyQualifiedName);
404404

405405
// Check whether a collection-valued navigation property should be indexed by key value(s).
406+
// Find indexability annotation annotated directly via NavigationPropertyRestriction.
407+
bool? annotatedIndexability = _model.GetBoolean(navigationProperty, CapabilitiesConstants.IndexableByKey);
406408
bool indexableByKey = true;
407409

408410
if (restriction?.IndexableByKey != null)
409411
{
410412
indexableByKey = (bool)restriction.IndexableByKey;
411413
}
412-
else if (_model.FindVocabularyAnnotations<IEdmVocabularyAnnotation>(navigationProperty, CapabilitiesConstants.IndexableByKey).FirstOrDefault() is IEdmVocabularyAnnotation indexabilityAnnotation && indexabilityAnnotation.Value is IEdmBooleanValue booleanValue)
414+
else if (annotatedIndexability != null)
413415
{
414-
// Find indexability annotation annotated directly via NavigationPropertyRestriction
415-
indexableByKey = booleanValue.Value;
416+
indexableByKey = annotatedIndexability.Value;
416417
}
417418

418419
if (indexableByKey)

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

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
// ------------------------------------------------------------
1+
// ------------------------------------------------------------
22
// Copyright (c) Microsoft Corporation. All rights reserved.
33
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
44
// ------------------------------------------------------------
55

66
using System.Collections.Generic;
77
using System.Linq;
88
using Microsoft.OData.Edm;
9+
using Microsoft.OData.Edm.Vocabularies;
910
using Microsoft.OpenApi.Any;
1011
using Microsoft.OpenApi.Models;
1112
using Microsoft.OpenApi.OData.Common;
@@ -158,6 +159,8 @@ protected override void SetParameters(OpenApiOperation operation)
158159
}
159160
}
160161
}
162+
163+
AppendSystemQueryOptions(function, operation);
161164
}
162165
}
163166

@@ -200,6 +203,57 @@ protected override void AppendCustomParameters(OpenApiOperation operation)
200203
}
201204
}
202205

206+
private void AppendSystemQueryOptions(IEdmFunction function, OpenApiOperation operation)
207+
{
208+
if (function.ReturnType.IsCollection())
209+
{
210+
// $top
211+
if (Context.CreateTop(function) is OpenApiParameter topParameter)
212+
{
213+
operation.Parameters.AppendParameter(topParameter);
214+
}
215+
216+
// $skip
217+
if (Context.CreateSkip(function) is OpenApiParameter skipParameter)
218+
{
219+
operation.Parameters.AppendParameter(skipParameter);
220+
}
221+
222+
// $search
223+
if (Context.CreateSearch(function) is OpenApiParameter searchParameter)
224+
{
225+
operation.Parameters.AppendParameter(searchParameter);
226+
}
227+
228+
// $filter
229+
if (Context.CreateFilter(function) is OpenApiParameter filterParameter)
230+
{
231+
operation.Parameters.AppendParameter(filterParameter);
232+
}
233+
234+
// $count
235+
if (Context.CreateCount(function) is OpenApiParameter countParameter)
236+
{
237+
operation.Parameters.AppendParameter(countParameter);
238+
}
239+
240+
if (function.ReturnType?.Definition?.AsElementType() is IEdmEntityType entityType)
241+
{
242+
// $select
243+
if (Context.CreateSelect(function, entityType) is OpenApiParameter selectParameter)
244+
{
245+
operation.Parameters.AppendParameter(selectParameter);
246+
}
247+
248+
// $orderby
249+
if (Context.CreateOrderBy(function, entityType) is OpenApiParameter orderbyParameter)
250+
{
251+
operation.Parameters.AppendParameter(orderbyParameter);
252+
}
253+
}
254+
}
255+
}
256+
203257
/// <inheritdoc/>
204258
protected override void SetCustomLinkRelType()
205259
{
@@ -211,9 +265,8 @@ protected override void SetCustomLinkRelType()
211265
}
212266
}
213267

214-
215-
/// <inheritdoc/>
216-
protected override void SetExternalDocs(OpenApiOperation operation)
268+
/// <inheritdoc/>
269+
protected override void SetExternalDocs(OpenApiOperation operation)
217270
{
218271
if (Context.Settings.ShowExternalDocs && Context.Model.GetLinkRecord(EdmOperation, CustomLinkRel) is Link externalDocs)
219272
{

test/Microsoft.OpenAPI.OData.Reader.Tests/Operation/EdmFunctionOperationHandlerTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ public void CreateOperationForEdmFunctionReturnsCorrectOperationHierarchicalClas
139139
Assert.Equal($"{entitySetName}.Functions", tag.Name);
140140

141141
Assert.NotNull(operation.Parameters);
142-
Assert.Equal(1, operation.Parameters.Count);
143-
Assert.Equal(new string[] { "id" }, operation.Parameters.Select(p => p.Name));
142+
Assert.Equal(6, operation.Parameters.Count); // id, top, skip, count, search, filter
143+
Assert.Contains("id", operation.Parameters.Select(x => x.Name).FirstOrDefault());
144144

145145
Assert.Null(operation.RequestBody);
146146

0 commit comments

Comments
 (0)