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

Fixes #2737: Support getting navigation source for complex types #2744

Merged
merged 3 commits into from
Feb 4, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ internal static ODataDeserializerContext GenerateNestedReadContext(ODataNestedRe
segmentType = readContext.Model.FindType(propertyTypeName);
}

// could it be a problem later that the navigationSource is null?
DynamicPathSegment pathSegment = new DynamicPathSegment(
nestedResourceInfo.Name,
segmentType,
Expand All @@ -79,12 +78,12 @@ internal static ODataDeserializerContext GenerateNestedReadContext(ODataNestedRe
{
if (edmProperty.PropertyKind == EdmPropertyKind.Navigation)
{
Contract.Assert(readContext.Path.NavigationSource != null, "Navigation property segment with null navigationSource");
IEdmNavigationProperty navigationProperty = edmProperty as IEdmNavigationProperty;
IEdmNavigationSource parentNavigationSource = readContext.Path.NavigationSource;
IEdmNavigationSource navigationSource = parentNavigationSource.FindNavigationTarget(navigationProperty);
IEdmPathExpression bindingPath = GetBindingPath(readContext.Path, navigationProperty);
IEdmNavigationSource navigationSource = parentNavigationSource?.FindNavigationTarget(navigationProperty, bindingPath);

if (navigationProperty.ContainsTarget)
if (navigationProperty.ContainsTarget || navigationSource == null || navigationSource is IEdmUnknownEntitySet)
{
path = AppendToPath(path, new NavigationPropertySegment(navigationProperty, navigationSource), navigationProperty.DeclaringType, parentNavigationSource);
}
Expand All @@ -110,6 +109,44 @@ internal static ODataDeserializerContext GenerateNestedReadContext(ODataNestedRe
return BuildNestedContextFromCurrentContext(readContext, path);
}

// Determines the binding path for an OData Path to a given navigationProperty
private static IEdmPathExpression GetBindingPath(Routing.ODataPath path, IEdmNavigationProperty navigationProperty)
Copy link
Member

Choose a reason for hiding this comment

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

looks like it could be extension method for ODataPath and move it to ODataPathExtensions?

{
Contract.Assert(navigationProperty != null, "Called GetBindingPath with a null navigation property");
if (path == null)
{
return null;
}

// Binding Path is made up of complex types, containment navigation properties, and type segments
List<string> segments = new List<string>();
foreach (ODataPathSegment segment in path.Segments)
{
if (segment is NavigationPropertySegment navSegment)
{
segments.Add(navSegment.NavigationProperty.Name);
}
else if (segment is PropertySegment propertySegment)
{
segments.Add(propertySegment.Property.Name);
}
else if (segment is TypeSegment typeSegment)
{
segments.Add(typeSegment.Identifier);
}
}

if(navigationProperty.DeclaringType != path.EdmType as IEdmStructuredType)
{
// Add a type cast segment
segments.Add(navigationProperty.DeclaringType.FullTypeName());
}

segments.Add(navigationProperty.Name);

return new EdmPathExpression(String.Join("/", segments));
}

/// <summary>
/// It builds a nested deserializer context from the current deserializer context
/// </summary>
Expand Down Expand Up @@ -154,11 +191,13 @@ internal static Routing.ODataPath AppendToPath(Routing.ODataPath path, ODataPath
}

List<ODataPathSegment> segments = new List<ODataPathSegment>(path.Segments);
IEdmType pathType = path.EdmType;

// Append type cast segment if required
if (declaringType != null && path.EdmType != declaringType)
if (declaringType != null && pathType != null && pathType != declaringType
&& declaringType.IsOrInheritsFrom(pathType.AsElementType()))
{
segments.Add(new TypeSegment(declaringType, navigationSource));
segments.Add(new TypeSegment(declaringType, pathType, navigationSource));
}

segments.Add(segment);
Expand Down
Loading