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

[Feature] Generates navigation property paths defined in nested complex properties #447

Merged
merged 4 commits into from
Nov 13, 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
92 changes: 75 additions & 17 deletions src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,32 +315,90 @@ private void RetrieveComplexPropertyPaths(IEdmEntityType entityType, ODataPath c
currentPath.Push(new ODataComplexPropertySegment(sp));
AppendPath(currentPath.Clone());


if (sp.Type.IsCollection())
{
CreateTypeCastPaths(currentPath, convertSettings, sp.Type.Definition.AsElementType() as IEdmComplexType, sp, true);
var count = _model.GetRecord<CountRestrictionsType>(sp, CapabilitiesConstants.CountRestrictions);
if(count?.IsCountable ?? true)
if (count?.IsCountable ?? true)
CreateCountPath(currentPath, convertSettings);
}
else
{
var complexTypeReference = sp.Type.AsComplex();
var definition = complexTypeReference.ComplexDefinition();

CreateTypeCastPaths(currentPath, convertSettings, definition, sp, false);
foreach (IEdmNavigationProperty np in complexTypeReference
.DeclaredNavigationProperties()
.Union(definition
.FindAllBaseTypes()
.SelectMany(x => x.DeclaredNavigationProperties()))
.Distinct()
.Where(CanFilter))
{
var count = _model.GetRecord<CountRestrictionsType>(np, CapabilitiesConstants.CountRestrictions);
RetrieveNavigationPropertyPaths(np, count, currentPath, convertSettings);
}
var complexType = sp.Type.AsComplex().ComplexDefinition();

CreateTypeCastPaths(currentPath, convertSettings, complexType, sp, false);

// Append navigation property paths for this complex property
RetrieveComplexTypeNavigationPropertyPaths(complexType, currentPath, convertSettings);

// Traverse this complex property to rerieve nested navigation property paths
TraverseComplexProperty(sp, currentPath, convertSettings);
}

currentPath.Pop();
}
}

/// <summary>
/// Retrieves navigation property paths for complex types.
/// </summary>
/// <param name="complexType">The target complex type.</param>
/// <param name="currentPath">The current path.</param>
/// <param name="convertSettings">The convert settings.</param>
private bool RetrieveComplexTypeNavigationPropertyPaths(IEdmComplexType complexType, ODataPath currentPath, OpenApiConvertSettings convertSettings)
{
Utils.CheckArgumentNull(complexType, nameof(complexType));
Utils.CheckArgumentNull(currentPath, nameof(currentPath));
Utils.CheckArgumentNull(convertSettings, nameof(convertSettings));

var navigationProperties = complexType
.DeclaredNavigationProperties()
.Union(complexType
.FindAllBaseTypes()
.SelectMany(x => x.DeclaredNavigationProperties()))
.Distinct()
.Where(CanFilter);

if (!navigationProperties.Any()) return false;

foreach (var np in navigationProperties)
{
var count = _model.GetRecord<CountRestrictionsType>(np, CapabilitiesConstants.CountRestrictions);
RetrieveNavigationPropertyPaths(np, count, currentPath, convertSettings);
}

return true;
}


/// <summary>
/// Traverses a complex property to generate navigation property paths within nested complex properties.
/// </summary>
/// <param name="structuralProperty">The target complex property.</param>
/// <param name="currentPath">The current path.</param>
/// <param name="convertSettings">The convert settings.</param>
private void TraverseComplexProperty(IEdmStructuralProperty structuralProperty, ODataPath currentPath, OpenApiConvertSettings convertSettings)
{
Utils.CheckArgumentNull(structuralProperty, nameof(structuralProperty));
Utils.CheckArgumentNull(currentPath, nameof(currentPath));
Utils.CheckArgumentNull(convertSettings, nameof(convertSettings));

var complexType = structuralProperty.Type.AsComplex().ComplexDefinition();
Debug.Assert(complexType != null);

foreach (IEdmStructuralProperty sp in complexType.DeclaredStructuralProperties()
.Where(x => x.Type.IsComplex() ||
x.Type.IsCollection() && x.Type.Definition.AsElementType() is IEdmComplexType))
{
currentPath.Push(new ODataComplexPropertySegment(sp));

var spComplexType = sp.Type.AsComplex().ComplexDefinition();

if (!RetrieveComplexTypeNavigationPropertyPaths(spComplexType, currentPath, convertSettings))
{
TraverseComplexProperty(sp, currentPath, convertSettings);
}

currentPath.Pop();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<PackageId>Microsoft.OpenApi.OData</PackageId>
<SignAssembly>true</SignAssembly>
<Version>1.5.0-preview8</Version>
<Version>1.5.0-preview9</Version>
<Description>This package contains the codes you need to convert OData CSDL to Open API Document of Model.</Description>
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
<PackageTags>Microsoft OpenApi OData EDM</PackageTags>
Expand All @@ -29,6 +29,7 @@
- Adds schema to content types of stream properties that have a collection of acceptable media types #435
- Retrieves complex properties of derived types #437
- Updates operationIds of navigation property paths with OData type cast segments #442
- Generate navigation property paths defined in nested complex properties #446
</PackageReleaseNotes>
<AssemblyName>Microsoft.OpenApi.OData.Reader</AssemblyName>
<AssemblyOriginatorKeyFile>..\..\tool\Microsoft.OpenApi.OData.snk</AssemblyOriginatorKeyFile>
Expand Down
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(18280, paths.Count());
Assert.Equal(18288, paths.Count());
AssertGraphBetaModelPaths(paths);
}

Expand Down Expand Up @@ -84,11 +84,15 @@ private void AssertGraphBetaModelPaths(IEnumerable<ODataPath> paths)
Assert.NotNull(paths.FirstOrDefault(p => p.GetPathItemName().Equals("/directoryObjects({id})/microsoft.graph.getMemberObjects")));
Assert.Null(paths.FirstOrDefault(p => p.GetPathItemName().Equals("/directoryObjects({id})/microsoft.graph.restore")));

// Test that complex and navigation properties on derived types are created
// Test that complex and navigation properties within derived types are appended
Assert.NotNull(paths.FirstOrDefault(p => p.GetPathItemName().Equals(
"/identity/authenticationEventsFlows({id})/microsoft.graph.externalUsersSelfServiceSignUpEventsFlow/onAttributeCollection/microsoft.graph.onAttributeCollectionExternalUsersSelfServiceSignUp/attributes")));
Assert.NotNull(paths.FirstOrDefault(p => p.GetPathItemName().Equals(
"/identity/authenticationEventsFlows({id})/microsoft.graph.externalUsersSelfServiceSignUpEventsFlow/onAuthenticationMethodLoadStart/microsoft.graph.onAuthenticationMethodLoadStartExternalUsersSelfServiceSignUp/identityProviders")));

// Test that navigation properties within nested complex properties are appended
Assert.NotNull(paths.FirstOrDefault(p => p.GetPathItemName().Equals(
"/identity/authenticationEventsFlows({id})/conditions/applications/includeApplications")));
}

[Fact]
Expand All @@ -109,7 +113,7 @@ public void GetPathsForGraphBetaModelWithDerivedTypesConstraintReturnsAllPaths()

// Assert
Assert.NotNull(paths);
Assert.Equal(18931, paths.Count());
Assert.Equal(18939, paths.Count());
}

[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62022,7 +62022,12 @@
<EntityType Name="authenticationEventsFlow" BaseType="graph.entity" Abstract="true" OpenType="true">
<Property Name="conditions" Type="graph.authenticationConditions">
<Annotation Term="Org.OData.Core.V1.Description" String="The conditions representing the context of the authentication request that will be used to decide whether the events policy will be invoked." />
</Property>
<Annotation Term="Org.OData.Capabilities.V1.ReadRestrictions">
<Record>
<PropertyValue Property="Readable" Bool="true" />
</Record>
</Annotation>
</Property>
<Property Name="description" Type="Edm.String">
<Annotation Term="Org.OData.Core.V1.Description" String="The description of the events policy." />
</Property>
Expand Down