Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for composable functions #431

Merged
merged 11 commits into from
Nov 28, 2023
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi.OData.Reader/Edm/ODataPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ private ODataPathKind CalcPathType()
{
return ODataPathKind.OperationImport;
}
else if (Segments.Any(c => c.Kind == ODataSegmentKind.Operation))
else if (Segments.Last().Kind == ODataSegmentKind.Operation)
{
return ODataPathKind.Operation;
}
Expand Down
57 changes: 56 additions & 1 deletion src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,10 @@ bool filter(IEdmStructuredType x) =>
/// </summary>
private void RetrieveBoundOperationPaths(OpenApiConvertSettings convertSettings)
{
foreach (var edmOperation in _model.GetAllElements().OfType<IEdmOperation>().Where(e => e.IsBound))
// process functions before actions
var edmFunctions = _model.GetAllElements().OfType<IEdmFunction>().Where(e => e.IsBound).ToList();
MartinM85 marked this conversation as resolved.
Show resolved Hide resolved
var edmActions = _model.GetAllElements().OfType<IEdmOperation>().Where(e => e.IsBound && e is not IEdmFunction).ToList();
MartinM85 marked this conversation as resolved.
Show resolved Hide resolved
foreach (var edmOperation in edmFunctions.Concat(edmActions))
{
if (!CanFilter(edmOperation))
{
Expand Down Expand Up @@ -755,6 +758,9 @@ bool filter(IEdmNavigationSource z) =>

// 4. Search for derived generated navigation property
AppendBoundOperationOnDerivedNavigationPropertyPath(edmOperation, isCollection, bindingEntityType, convertSettings);

// 5. Search for operation
AppendBoundOperationOnOperationPath(edmOperation, isCollection, bindingEntityType);
}
}
}
Expand Down Expand Up @@ -993,5 +999,54 @@ private void AppendBoundOperationOnDerivedNavigationPropertyPath(
}
}
}

private void AppendBoundOperationOnOperationPath(IEdmOperation edmOperation, bool isCollection, IEdmEntityType bindingEntityType)
{
bool isEscapedFunction = _model.IsUrlEscapeFunction(edmOperation);

// only composable functions
var paths = _allOperationPaths.Where(x => x.LastSegment is ODataOperationSegment operationSegment
&& operationSegment.Operation is IEdmFunction edmFunction
&& edmFunction.IsComposable).ToList();
baywet marked this conversation as resolved.
Show resolved Hide resolved

foreach (var path in paths)
{
if (path.LastSegment is ODataOperationSegment operationSegment)
MartinM85 marked this conversation as resolved.
Show resolved Hide resolved
{
if (path.Segments.Count > 1 && path.Segments[path.Segments.Count - 2] is ODataOperationSegment)
{
continue;
}

if (operationSegment.Operation is IEdmFunction edmFunction && edmFunction.IsComposable)
MartinM85 marked this conversation as resolved.
Show resolved Hide resolved
{
if (edmFunction.ReturnType != null && edmFunction.ReturnType.Definition.Equals(bindingEntityType))
MartinM85 marked this conversation as resolved.
Show resolved Hide resolved
{
if (!isCollection)
MartinM85 marked this conversation as resolved.
Show resolved Hide resolved
{
if (!EdmModelHelper.IsOperationAllowed(_model, edmOperation, operationSegment.Operation, true))
{
continue;
}

ODataPath newOperationPath = path.Clone();
newOperationPath.Push(new ODataOperationSegment(edmOperation, isEscapedFunction, _model));
AppendPath(newOperationPath);

if (edmOperation.ReturnType != null && edmOperation.ReturnType.Definition is IEdmEntityType returnBindingEntityType)
{
foreach (var navProperty in returnBindingEntityType.NavigationProperties())
{
ODataPath newNavigationPath = newOperationPath.Clone();
newNavigationPath.Push(new ODataNavigationPropertySegment(navProperty));
AppendPath(newNavigationPath);
}
}
}
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void GetPathsForGraphBetaModelReturnsAllPaths()

// Assert
Assert.NotNull(paths);
Assert.Equal(18264, paths.Count());
Assert.Equal(18832, paths.Count());
AssertGraphBetaModelPaths(paths);
}

Expand Down Expand Up @@ -103,7 +103,7 @@ public void GetPathsForGraphBetaModelWithDerivedTypesConstraintReturnsAllPaths()

// Assert
Assert.NotNull(paths);
Assert.Equal(18915, paths.Count());
Assert.Equal(19483, paths.Count());
}

[Theory]
Expand Down