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

method to get all navigation property bindings #2157

Merged
merged 7 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/Microsoft.OData.Edm/ExtensionMethods/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1646,6 +1646,40 @@ public static IEnumerable<IEdmOperationImport> OperationImports(this IEdmEntityC
return container.AllElements().OfType<IEdmOperationImport>();
}

/// <summary>
/// Returns navigation property bindings with their corresponding container elements (entity set or singleton)
/// </summary>
/// <param name="container">Reference to the calling object.</param>
/// <returns>Collection of pairs of container element and navigation property bindings.</returns>
public static IEnumerable<Tuple<IEdmEntityContainerElement, IEdmNavigationPropertyBinding>> GetNavigationPropertyBindings(this IEdmEntityContainer container)
{
EdmUtil.CheckArgumentNull(container, "container");

IEnumerable<IEdmEntityContainerElement> elements = container.AllElements();

foreach(IEdmEntityContainerElement element in elements)
{
switch (element)
{
case IEdmEntitySet entitySet:
foreach(IEdmNavigationPropertyBinding binding in entitySet.NavigationPropertyBindings)
{
yield return Tuple.Create<IEdmEntityContainerElement, IEdmNavigationPropertyBinding>(entitySet, binding);
}
break;
case IEdmSingleton singleton:
foreach (IEdmNavigationPropertyBinding binding in singleton.NavigationPropertyBindings)
{
yield return Tuple.Create< IEdmEntityContainerElement, IEdmNavigationPropertyBinding>(singleton, binding);
}
break;
default:
break;
}
}
}


#endregion

#region IEdmTypeReference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,45 @@ public void EnsureActionImportActionPropertyIsUnresolvedAction()
Assert.Equal(EdmContainerElementKind.ActionImport, imports[0].ContainerElementKind);
Assert.Null(imports[0].EntitySet);
}

[Fact]
public void NavigationPropertyBindingsReturned()
{
// arrange
var entitySet1 = new CsdlEntitySet("EntitySet1", "unknown", new[] {
new CsdlNavigationPropertyBinding("foo", "bar", testLocation)
}, testLocation);
var singleton1 = new CsdlSingleton("Singleton", "unknown", new[] {
new CsdlNavigationPropertyBinding("foo", "bar", testLocation)
}, testLocation);
var csdlEntityContainer = CsdlBuilder.EntityContainer("Container", entitySets: new [] { entitySet1 }, singletons: new[] { singleton1 } );
var schema = CsdlBuilder.Schema("FQ.NS", csdlEntityContainers: new CsdlEntityContainer[] { csdlEntityContainer });
var csdlModel = new CsdlModel();
csdlModel.AddSchema(schema);

// act
var container = new CsdlSemanticsEntityContainer(
new CsdlSemanticsSchema(
new CsdlSemanticsModel(
csdlModel,
new EdmDirectValueAnnotationsManager(),
Enumerable.Empty<IEdmModel>()),
schema),
csdlEntityContainer);
var bindings = container.GetNavigationPropertyBindings().ToList();

// assert
Assert.Equal(2, bindings.Count);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should also assert the actual bindings are what's expected?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@habbes Added requested asserts

var entitySet = Assert.IsType<CsdlSemanticsEntitySet>(bindings[0].Item1);
var singleton = Assert.IsType<CsdlSemanticsSingleton>(bindings[1].Item1);
Assert.Equal("EntitySet1", entitySet.Name);
Assert.Equal("Singleton", singleton.Name);
var navigationPropertyBinding1 = Assert.IsType<EdmNavigationPropertyBinding>(bindings[0].Item2);
var navigationPropertyBinding2 = Assert.IsType<EdmNavigationPropertyBinding>(bindings[1].Item2);
Assert.Equal("foo", navigationPropertyBinding1.Path.Path);
Assert.Equal("bar", navigationPropertyBinding1.Target.Name);
Assert.Equal("foo", navigationPropertyBinding2.Path.Path);
Assert.Equal("bar", navigationPropertyBinding2.Target.Name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2025,6 +2025,11 @@ public sealed class Microsoft.OData.Edm.ExtensionMethods {
]
public static string GetLongDescriptionAnnotation (Microsoft.OData.Edm.IEdmModel model, Microsoft.OData.Edm.Vocabularies.IEdmVocabularyAnnotatable target)

[
ExtensionAttribute(),
]
public static System.Collections.Generic.IEnumerable`1[[System.Tuple`2[[Microsoft.OData.Edm.IEdmEntityContainerElement],[Microsoft.OData.Edm.IEdmNavigationPropertyBinding]]]] GetNavigationPropertyBindings (Microsoft.OData.Edm.IEdmEntityContainer container)

[
ExtensionAttribute(),
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2025,6 +2025,11 @@ public sealed class Microsoft.OData.Edm.ExtensionMethods {
]
public static string GetLongDescriptionAnnotation (Microsoft.OData.Edm.IEdmModel model, Microsoft.OData.Edm.Vocabularies.IEdmVocabularyAnnotatable target)

[
ExtensionAttribute(),
]
public static System.Collections.Generic.IEnumerable`1[[System.Tuple`2[[Microsoft.OData.Edm.IEdmEntityContainerElement],[Microsoft.OData.Edm.IEdmNavigationPropertyBinding]]]] GetNavigationPropertyBindings (Microsoft.OData.Edm.IEdmEntityContainer container)

[
ExtensionAttribute(),
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2025,6 +2025,11 @@ public sealed class Microsoft.OData.Edm.ExtensionMethods {
]
public static string GetLongDescriptionAnnotation (Microsoft.OData.Edm.IEdmModel model, Microsoft.OData.Edm.Vocabularies.IEdmVocabularyAnnotatable target)

[
ExtensionAttribute(),
]
public static System.Collections.Generic.IEnumerable`1[[System.Tuple`2[[Microsoft.OData.Edm.IEdmEntityContainerElement],[Microsoft.OData.Edm.IEdmNavigationPropertyBinding]]]] GetNavigationPropertyBindings (Microsoft.OData.Edm.IEdmEntityContainer container)

[
ExtensionAttribute(),
]
Expand Down