Skip to content

Commit 3cce515

Browse files
authored
Skips generation of action/function paths for non-contained nav. props. (#115)
* Do not create actions/functions for non-containment nav. props * Add test for Function Operations
1 parent 611067f commit 3cce515

File tree

2 files changed

+88
-3
lines changed

2 files changed

+88
-3
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,11 @@ private bool AppendBoundOperationOnNavigationPropertyPath(IEdmOperation edmOpera
478478

479479
ODataNavigationPropertySegment npSegment = path.Segments.Last(s => s is ODataNavigationPropertySegment) as ODataNavigationPropertySegment;
480480

481+
if (!npSegment.NavigationProperty.ContainsTarget)
482+
{
483+
continue;
484+
}
485+
481486
bool isLastKeySegment = path.LastSegment is ODataKeySegment;
482487

483488
if (isCollection)
@@ -605,6 +610,11 @@ private bool AppendBoundOperationOnDerivedNavigationPropertyPath(
605610
continue;
606611
}
607612

613+
if (!npSegment.NavigationProperty.ContainsTarget)
614+
{
615+
continue;
616+
}
617+
608618
bool isLastKeySegment = path.LastSegment is ODataKeySegment;
609619

610620
if (isCollection)

test/Microsoft.OpenAPI.OData.Reader.Tests/Edm/ODataPathProviderTests.cs

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void GetPathsForGraphBetaModelReturnsAllPaths()
4848

4949
// Assert
5050
Assert.NotNull(paths);
51-
Assert.Equal(17401, paths.Count());
51+
Assert.Equal(13727, paths.Count());
5252
}
5353

5454
[Fact]
@@ -67,7 +67,7 @@ public void GetPathsForGraphBetaModelWithDerivedTypesConstraintReturnsAllPaths()
6767

6868
// Assert
6969
Assert.NotNull(paths);
70-
Assert.Equal(13730, paths.Count());
70+
Assert.Equal(13712, paths.Count());
7171
}
7272

7373
[Fact]
@@ -276,6 +276,80 @@ public void GetPathsWithBoundActionOperationWorks()
276276
Assert.Contains("/Customers({ID})/NS.renew", paths.Select(p => p.GetPathItemName()));
277277
}
278278

279+
[Theory]
280+
[InlineData(true)]
281+
[InlineData(false)]
282+
public void GetPathsWithBoundActionOperationForContainmentNavigationPropertyPathsWorks(bool containsTarget)
283+
{
284+
// Arrange
285+
string navProp = $@"<NavigationProperty Name=""Referral"" Type=""NS.NiceCustomer"" ContainsTarget=""{containsTarget}""/>";
286+
string boundAction =
287+
@"<Action Name=""Ack"" IsBound=""true"">
288+
<Parameter Name=""bindingParameter"" Type=""NS.NiceCustomer"" />
289+
<ReturnType Type=""Edm.Boolean"" />
290+
</Action>
291+
<EntityType Name=""NiceCustomer"">
292+
<Property Name=""Other"" Type=""Edm.Int32"" Nullable=""true"" />
293+
</EntityType>";
294+
295+
IEdmModel model = GetEdmModel(boundAction, "", navProp);
296+
ODataPathProvider provider = new ODataPathProvider();
297+
var settings = new OpenApiConvertSettings();
298+
299+
// Act
300+
var paths = provider.GetPaths(model, settings);
301+
302+
// Assert
303+
Assert.NotNull(paths);
304+
Assert.Equal(4, paths.Count());
305+
306+
if (containsTarget)
307+
{
308+
Assert.Contains("/Customers({ID})/Referral/NS.Ack", paths.Select(p => p.GetPathItemName()));
309+
}
310+
else
311+
{
312+
Assert.DoesNotContain("/Customers({ID})/Referral/NS.Ack", paths.Select(p => p.GetPathItemName()));
313+
}
314+
}
315+
316+
[Theory]
317+
[InlineData(true)]
318+
[InlineData(false)]
319+
public void GetPathsWithBoundFunctionOperationForContainmentNavigationPropertyPathsWorks(bool containsTarget)
320+
{
321+
// Arrange
322+
string navProp = $@"<NavigationProperty Name=""Referral"" Type=""NS.NiceCustomer"" ContainsTarget=""{containsTarget}""/>";
323+
string boundAction =
324+
@"<Function Name=""Search"" IsBound=""true"">
325+
<Parameter Name=""bindingParameter"" Type=""NS.NiceCustomer"" />
326+
<ReturnType Type=""Collection(NS.Customer)"" />
327+
</Function>
328+
<EntityType Name=""NiceCustomer"">
329+
<Property Name=""Other"" Type=""Edm.Int32"" Nullable=""true"" />
330+
</EntityType>";
331+
332+
IEdmModel model = GetEdmModel(boundAction, "", navProp);
333+
ODataPathProvider provider = new ODataPathProvider();
334+
var settings = new OpenApiConvertSettings();
335+
336+
// Act
337+
var paths = provider.GetPaths(model, settings);
338+
339+
// Assert
340+
Assert.NotNull(paths);
341+
Assert.Equal(4, paths.Count());
342+
343+
if (containsTarget)
344+
{
345+
Assert.Contains("/Customers({ID})/Referral/NS.Search()", paths.Select(p => p.GetPathItemName()));
346+
}
347+
else
348+
{
349+
Assert.DoesNotContain("/Customers({ID})/Referral/NS.Search()", paths.Select(p => p.GetPathItemName()));
350+
}
351+
}
352+
279353
[Fact]
280354
public void GetPathsWithUnboundOperationImportWorks()
281355
{
@@ -500,7 +574,7 @@ public void GetPathsWithStreamPropertyAndWithEntityHasStreamWorks(bool hasStream
500574
}
501575
}
502576

503-
private static IEdmModel GetEdmModel(string schemaElement, string containerElement)
577+
private static IEdmModel GetEdmModel(string schemaElement, string containerElement, string propertySchema = null)
504578
{
505579
string template = $@"<?xml version=""1.0"" encoding=""utf-16""?>
506580
<Schema Namespace=""NS"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
@@ -509,6 +583,7 @@ private static IEdmModel GetEdmModel(string schemaElement, string containerEleme
509583
<PropertyRef Name=""ID"" />
510584
</Key>
511585
<Property Name=""ID"" Type=""Edm.Int32"" Nullable=""false"" />
586+
{propertySchema}
512587
</EntityType>
513588
{schemaElement}
514589
<EntityContainer Name =""Default"">

0 commit comments

Comments
 (0)