-
Notifications
You must be signed in to change notification settings - Fork 357
Expand fully qualified type #2160
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
Changes from all commits
a8d8195
a7c9914
2c152a1
c044025
6ebd2ce
c5c7afe
79194ad
eec3212
7b4a2e4
fbfed14
5bc1655
b5165f9
d1cd491
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -369,23 +369,46 @@ private SelectItem GenerateExpandItem(ExpandTermToken tokenIn) | |
| currentNavProp = ParseComplexTypesBeforeNavigation(currentComplexProp, ref firstNonTypeToken, pathSoFar); | ||
| } | ||
|
|
||
| bool isRef = false; | ||
| bool isCount = false; | ||
| bool hasDerivedTypeSegment = false; | ||
| IEdmType derivedType = null; | ||
|
|
||
| // Handle $expand=Customer/Fully.Qualified.Namespace.VipCustomer | ||
| // The deriveTypeToken is Fully.Qualified.Namespace.VipCustomer | ||
| if (firstNonTypeToken.NextToken != null && firstNonTypeToken.NextToken.IsNamespaceOrContainerQualified()) | ||
| { | ||
| hasDerivedTypeSegment = true; | ||
| derivedType = UriEdmHelpers.FindTypeFromModel(this.Model, firstNonTypeToken.NextToken.Identifier, this.configuration.Resolver); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do you need any special handling for FindTypeFromModel not finding the type? In that case, I believe derivedType would return null, and it looks like CheckRelatedTo may throw a null ref exception as IsRelatedTo will return false, but the exception that we try to generate on line 57 of EdmUriHelpers will try to call FullTypeName() on the null value. Minimally, we should fix EdmUriHelpers.CheckRelatedTo to handle the null childType and add tests for adding a cast segment with a type that is not found.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When Regarding |
||
|
|
||
| if (derivedType == null) | ||
| { | ||
| // Exception example: The type Fully.Qualified.Namespace.UndefinedType is not defined in the model. | ||
| throw new ODataException(ODataErrorStrings.ExpandItemBinder_CannotFindType(firstNonTypeToken.NextToken.Identifier)); | ||
| } | ||
|
|
||
| // In this example: $expand=Customer/Fully.Qualified.Namespace.VipCustomer | ||
| // We validate that the derived type Fully.Qualified.Namespace.VipCustomer is related to Navigation property Customer. | ||
| UriEdmHelpers.CheckRelatedTo(currentNavProp.ToEntityType(), derivedType); | ||
gathogojr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // ensure that we're always dealing with proper V4 syntax | ||
| if (firstNonTypeToken.NextToken != null && firstNonTypeToken.NextToken.NextToken != null) | ||
| if (firstNonTypeToken?.NextToken?.NextToken != null && !hasDerivedTypeSegment) | ||
| { | ||
| throw new ODataException(ODataErrorStrings.ExpandItemBinder_TraversingMultipleNavPropsInTheSamePath); | ||
| } | ||
|
|
||
| bool isRef = false; | ||
| bool isCount = false; | ||
|
|
||
| if (firstNonTypeToken.NextToken != null) | ||
| if ((firstNonTypeToken.NextToken != null && !hasDerivedTypeSegment) || | ||
| (firstNonTypeToken?.NextToken?.NextToken != null && hasDerivedTypeSegment)) | ||
| { | ||
| PathSegmentToken nextToken = hasDerivedTypeSegment ? firstNonTypeToken.NextToken.NextToken : firstNonTypeToken.NextToken; | ||
|
|
||
| // lastly... make sure that, since we're on a NavProp, that the next token isn't null. | ||
| if (firstNonTypeToken.NextToken.Identifier == UriQueryConstants.RefSegment) | ||
| if (nextToken.Identifier == UriQueryConstants.RefSegment) | ||
| { | ||
| isRef = true; | ||
| } | ||
| else if (firstNonTypeToken.NextToken.Identifier == UriQueryConstants.CountSegment) | ||
| else if (nextToken.Identifier == UriQueryConstants.CountSegment) | ||
| { | ||
| isCount = true; | ||
| } | ||
|
|
@@ -398,7 +421,6 @@ private SelectItem GenerateExpandItem(ExpandTermToken tokenIn) | |
| // Add the segments in select and expand to parsed segments | ||
| List<ODataPathSegment> parsedPath = new List<ODataPathSegment>(this.parsedSegments); | ||
| parsedPath.AddRange(pathSoFar); | ||
|
|
||
| IEdmNavigationSource targetNavigationSource = null; | ||
| if (this.NavigationSource != null) | ||
| { | ||
|
|
@@ -409,6 +431,14 @@ private SelectItem GenerateExpandItem(ExpandTermToken tokenIn) | |
| NavigationPropertySegment navSegment = new NavigationPropertySegment(currentNavProp, targetNavigationSource); | ||
| pathSoFar.Add(navSegment); | ||
| parsedPath.Add(navSegment); // Add the navigation property segment to parsed segments for future usage. | ||
|
|
||
| if (hasDerivedTypeSegment) | ||
| { | ||
| TypeSegment derivedTypeSegment = new TypeSegment(derivedType, targetNavigationSource); | ||
| pathSoFar.Add(derivedTypeSegment); | ||
| parsedPath.Add(derivedTypeSegment); | ||
| } | ||
|
|
||
| ODataExpandPath pathToNavProp = new ODataExpandPath(pathSoFar); | ||
|
|
||
| // $apply | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1667,6 +1667,157 @@ public void ExpandWithNavigationPropCountWithSearchOptionWorks() | |
| Assert.NotNull(expandedCountSelectItem.SearchOption); | ||
| } | ||
|
|
||
| // $expand=navProp/fully.qualified.type/$ref | ||
| [Theory] | ||
| [InlineData("MyPeople/Fully.Qualified.Namespace.Employee/$ref")] | ||
| [InlineData("MyPeople/MainAlias.Employee/$ref")] | ||
| public void ExpandWithNavigationPropRefWithFullyQualifiedTypeWorks(string query) | ||
| { | ||
| // Arrange | ||
| var odataQueryOptionParser = new ODataQueryOptionParser(HardCodedTestModel.TestModel, | ||
| HardCodedTestModel.GetDogType(), HardCodedTestModel.GetDogsSet(), | ||
| new Dictionary<string, string>() | ||
| { | ||
| {"$expand", query} | ||
| }); | ||
|
|
||
| // Act | ||
| var selectExpandClause = odataQueryOptionParser.ParseSelectAndExpand(); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(selectExpandClause); | ||
| ExpandedReferenceSelectItem expandedRefSelectItem = Assert.IsType<ExpandedReferenceSelectItem>(Assert.Single(selectExpandClause.SelectedItems)); | ||
| Assert.Same(HardCodedTestModel.GetPeopleSet(), expandedRefSelectItem.NavigationSource); | ||
| Assert.Equal(2, expandedRefSelectItem.PathToNavigationProperty.Count); | ||
|
|
||
| NavigationPropertySegment navPropSegment = Assert.IsType<NavigationPropertySegment>(expandedRefSelectItem.PathToNavigationProperty.Segments.First()); | ||
| TypeSegment typeSegment = Assert.IsType<TypeSegment>(expandedRefSelectItem.PathToNavigationProperty.Segments.Last()); | ||
| Assert.Equal("MyPeople", navPropSegment.Identifier); | ||
| Assert.Equal("Collection(Fully.Qualified.Namespace.Person)", navPropSegment.EdmType.FullTypeName()); | ||
| Assert.Equal("Fully.Qualified.Namespace.Employee", typeSegment.EdmType.FullTypeName()); | ||
| } | ||
|
|
||
| // $expand=navProp/fully.qualified.type/$count | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should also have tests for /$expand=navProp/fully.qualified.type/$ref #Closed
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add tests for fully.qualified.type being an alias? #Closed
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mikepizzo I am not sure what you mean. Kindly clarify.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From the spec, parameter aliases are only used for
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry; i meant using a (namespace alias)[http://docs.oasis-open.org/odata/odata-csdl-xml/v4.01/odata-csdl-xml-v4.01.html#sec_Alias] to qualify the type name, rather than the full namespace name. For example: ...$expand=navProp/alias.type
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mikepizzo I have added the tests
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a (negative) test case for fully.qualified.undefinedType and verify meaningful exception (i.e., saying that the type fully.qualified.undefinedType was not found). #Closed
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| [Theory] | ||
| [InlineData("MyPeople/Fully.Qualified.Namespace.Employee/$count")] | ||
| [InlineData("MyPeople/MainAlias.Employee/$count")] // With schema alias | ||
| public void ExpandWithNavigationPropCountWithFullyQualifiedTypeWorks(string query) | ||
| { | ||
| // Arrange | ||
| var odataQueryOptionParser = new ODataQueryOptionParser(HardCodedTestModel.TestModel, | ||
| HardCodedTestModel.GetDogType(), HardCodedTestModel.GetDogsSet(), | ||
| new Dictionary<string, string>() | ||
| { | ||
| {"$expand", query} | ||
| }); | ||
|
|
||
| // Act | ||
| var selectExpandClause = odataQueryOptionParser.ParseSelectAndExpand(); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(selectExpandClause); | ||
| ExpandedCountSelectItem expandedCountSelectItem = Assert.IsType<ExpandedCountSelectItem>(Assert.Single(selectExpandClause.SelectedItems)); | ||
| Assert.Same(HardCodedTestModel.GetPeopleSet(), expandedCountSelectItem.NavigationSource); | ||
| Assert.Null(expandedCountSelectItem.FilterOption); | ||
| Assert.Null(expandedCountSelectItem.SearchOption); | ||
| Assert.Equal(2, expandedCountSelectItem.PathToNavigationProperty.Count); | ||
|
|
||
| NavigationPropertySegment navPropSegment = Assert.IsType<NavigationPropertySegment>(expandedCountSelectItem.PathToNavigationProperty.Segments.First()); | ||
| TypeSegment typeSegment = Assert.IsType<TypeSegment>(expandedCountSelectItem.PathToNavigationProperty.Segments.Last()); | ||
| Assert.Equal("MyPeople", navPropSegment.Identifier); | ||
| Assert.Equal("Collection(Fully.Qualified.Namespace.Person)", navPropSegment.EdmType.FullTypeName()); | ||
| Assert.Equal("Fully.Qualified.Namespace.Employee", typeSegment.EdmType.FullTypeName()); | ||
| } | ||
|
|
||
| // $expand=navProp/fully.qualified.type/$count($filter=prop) | ||
| [Theory] | ||
| [InlineData("MyPeople/Fully.Qualified.Namespace.Employee/$count($filter=ID eq 1)")] | ||
| [InlineData("MyPeople/MainAlias.Employee/$count($filter=ID eq 1)")] // With schema alias | ||
| public void ExpandWithNavigationPropCountWithFilterAndFullyQualifiedTypeWorks(string query) | ||
| { | ||
| // Arrange | ||
| var odataQueryOptionParser = new ODataQueryOptionParser(HardCodedTestModel.TestModel, | ||
| HardCodedTestModel.GetDogType(), HardCodedTestModel.GetDogsSet(), | ||
| new Dictionary<string, string>() | ||
| { | ||
| {"$expand", query} | ||
| }); | ||
|
|
||
| // Act | ||
| var selectExpandClause = odataQueryOptionParser.ParseSelectAndExpand(); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(selectExpandClause); | ||
| ExpandedCountSelectItem expandedCountSelectItem = Assert.IsType<ExpandedCountSelectItem>(Assert.Single(selectExpandClause.SelectedItems)); | ||
| Assert.Same(HardCodedTestModel.GetPeopleSet(), expandedCountSelectItem.NavigationSource); | ||
| Assert.NotNull(expandedCountSelectItem.FilterOption); | ||
| Assert.Null(expandedCountSelectItem.SearchOption); | ||
| Assert.Equal(2, expandedCountSelectItem.PathToNavigationProperty.Count); | ||
|
|
||
| NavigationPropertySegment navPropSegment = Assert.IsType<NavigationPropertySegment>(expandedCountSelectItem.PathToNavigationProperty.Segments.First()); | ||
| TypeSegment typeSegment = Assert.IsType<TypeSegment>(expandedCountSelectItem.PathToNavigationProperty.Segments.Last()); | ||
| Assert.Equal("MyPeople", navPropSegment.Identifier); | ||
| Assert.Equal("Collection(Fully.Qualified.Namespace.Person)", navPropSegment.EdmType.FullTypeName()); | ||
| Assert.Equal("Fully.Qualified.Namespace.Employee", typeSegment.EdmType.FullTypeName()); | ||
| } | ||
|
|
||
| // $expand=navProp/fully.qualified.type/$count($search=prop) | ||
| [Theory] | ||
| [InlineData("MyPeople/Fully.Qualified.Namespace.Employee/$count($search=blue)")] | ||
| [InlineData("MyPeople/MainAlias.Employee/$count($search=blue)")] // With schema alias | ||
| public void ExpandWithNavigationPropCountWithSearchAndFullyQualifiedTypeWorks(string query) | ||
| { | ||
| // Arrange | ||
| var odataQueryOptionParser = new ODataQueryOptionParser(HardCodedTestModel.TestModel, | ||
| HardCodedTestModel.GetDogType(), HardCodedTestModel.GetDogsSet(), | ||
| new Dictionary<string, string>() | ||
| { | ||
| {"$expand", query} | ||
| }); | ||
|
|
||
| // Act | ||
| var selectExpandClause = odataQueryOptionParser.ParseSelectAndExpand(); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(selectExpandClause); | ||
| ExpandedCountSelectItem expandedCountSelectItem = Assert.IsType<ExpandedCountSelectItem>(Assert.Single(selectExpandClause.SelectedItems)); | ||
| Assert.Same(HardCodedTestModel.GetPeopleSet(), expandedCountSelectItem.NavigationSource); | ||
| Assert.Null(expandedCountSelectItem.FilterOption); | ||
| Assert.NotNull(expandedCountSelectItem.SearchOption); | ||
| Assert.Equal(2, expandedCountSelectItem.PathToNavigationProperty.Count); | ||
|
|
||
| NavigationPropertySegment navPropSegment = Assert.IsType<NavigationPropertySegment>(expandedCountSelectItem.PathToNavigationProperty.Segments.First()); | ||
| TypeSegment typeSegment = Assert.IsType<TypeSegment>(expandedCountSelectItem.PathToNavigationProperty.Segments.Last()); | ||
| Assert.Equal("MyPeople", navPropSegment.Identifier); | ||
| Assert.Equal("Collection(Fully.Qualified.Namespace.Person)", navPropSegment.EdmType.FullTypeName()); | ||
| Assert.Equal("Fully.Qualified.Namespace.Employee", typeSegment.EdmType.FullTypeName()); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("MyPeople/Fully.Qualified.Namespace.UndefinedType")] | ||
| [InlineData("MyPeople/Fully.Qualified.Namespace.UndefinedType/$ref")] | ||
| [InlineData("MyPeople/Fully.Qualified.Namespace.UndefinedType/$count")] | ||
| [InlineData("MyPeople/Fully.Qualified.Namespace.UndefinedType/$count($search=blue)")] | ||
| [InlineData("MyPeople/Fully.Qualified.Namespace.UndefinedType/$count($filter=ID eq 1)")] | ||
| public void ExpandWithNavigationPropWithUndefinedTypeThrows(string query) | ||
| { | ||
| // Arrange | ||
| var odataQueryOptionParser = new ODataQueryOptionParser(HardCodedTestModel.TestModel, | ||
| HardCodedTestModel.GetDogType(), HardCodedTestModel.GetDogsSet(), | ||
| new Dictionary<string, string>() | ||
| { | ||
| {"$expand", query} | ||
| }); | ||
|
|
||
| // Act | ||
| Action action = () => odataQueryOptionParser.ParseSelectAndExpand(); | ||
|
|
||
| // Assert | ||
|
|
||
| // Exception: The type Fully.Qualified.Namespace.UndefinedType is not defined in the model. | ||
| action.Throws<ODataException>(ODataErrorStrings.ExpandItemBinder_CannotFindType("Fully.Qualified.Namespace.UndefinedType")); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SelectWithNestedSelectWorks() | ||
| { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.