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

Fix sorting on a grouped nested dynamic property #1244

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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 @@ -29,13 +29,13 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.OData.ModelBuilder" Version="1.0.9" />
<PackageReference Include="Microsoft.OData.Core" Version="7.20.0" />
<PackageReference Include="Microsoft.OData.Edm" Version="7.20.0" />
<PackageReference Include="Microsoft.OData.Core" Version="7.21.2" />
clemvnt marked this conversation as resolved.
Show resolved Hide resolved
<PackageReference Include="Microsoft.OData.Edm" Version="7.21.2" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Spatial" Version="7.20.0" />
<PackageReference Include="Microsoft.Spatial" Version="7.21.2" />
clemvnt marked this conversation as resolved.
Show resolved Hide resolved
</ItemGroup>

<ItemGroup>
Expand Down
15 changes: 10 additions & 5 deletions src/Microsoft.AspNetCore.OData/Query/Expressions/QueryBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public virtual Expression BindDynamicPropertyAccessQueryNode(SingleValueOpenProp

if (context.ElementClrType.IsDynamicTypeWrapper())
{
return GetFlattenedPropertyExpression(openNode.Name, context) ?? Expression.Property(Bind(openNode.Source, context), openNode.Name);
return GetFlattenedPropertyExpression(GetFullPropertyPath(openNode), context) ?? Expression.Property(Bind(openNode.Source, context), openNode.Name);
}

if (context.ComputedProperties.TryGetValue(openNode.Name, out var computedProperty))
Expand Down Expand Up @@ -713,7 +713,7 @@ public virtual Expression BindAllNode(AllNode allNode, QueryBinderContext contex
{
CheckArgumentNull(allNode, context);

// context.EnterLambdaScope();
// context.EnterLambdaScope();

(string name, ParameterExpression allIt) = context.HandleLambdaParameters(allNode.RangeVariables);

Expand Down Expand Up @@ -1034,7 +1034,7 @@ protected Expression GetFlattenedPropertyExpression(string propertyPath, QueryBi
}
#endregion

internal string GetFullPropertyPath(SingleValueNode node)
internal static string GetFullPropertyPath(SingleValueNode node)
{
string path = null;
SingleValueNode parent = null;
Expand All @@ -1046,15 +1046,20 @@ internal string GetFullPropertyPath(SingleValueNode node)
parent = complexNode.Source;
break;
case QueryNodeKind.SingleValuePropertyAccess:
var propertyNode = ((SingleValuePropertyAccessNode)node);
var propertyNode = (SingleValuePropertyAccessNode)node;
path = propertyNode.Property.Name;
parent = propertyNode.Source;
break;
case QueryNodeKind.SingleNavigationNode:
var navNode = ((SingleNavigationNode)node);
var navNode = (SingleNavigationNode)node;
path = navNode.NavigationProperty.Name;
parent = navNode.Source;
break;
case QueryNodeKind.SingleValueOpenPropertyAccess:
clemvnt marked this conversation as resolved.
Show resolved Hide resolved
var openPropertyNode = (SingleValueOpenPropertyAccessNode)node;
path = openPropertyNode.Name;
parent = openPropertyNode.Source;
break;
}

if (parent != null)
Expand Down
4 changes: 4 additions & 0 deletions test/Microsoft.AspNetCore.OData.Tests/Models/Address.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// </copyright>
//------------------------------------------------------------------------------

using System.Collections.Generic;

namespace Microsoft.AspNetCore.OData.Tests.Models
{
public class Address
Expand All @@ -22,5 +24,7 @@ public class Address
public ZipCode ZipCode { get; set; }

public string IgnoreThis { get; set; }

public Dictionary<string, object> DynamicProperties { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,161 @@ public void GetPropertyExpression_Works_ForAggregateOrNonAggregate(bool isAggreg
Assert.NotNull(expression);
Assert.Equal(expected, expression.ToString());
}

[Fact]
public void GetFullPropertyPath_WithSingleComplexNode()
{
// Arrange
Mock<IEdmStructuredTypeReference> structuredType = new Mock<IEdmStructuredTypeReference>();
Mock<IEdmNavigationSource> navigationSource = new Mock<IEdmNavigationSource>();
ResourceRangeVariable rangeVariable = new ResourceRangeVariable("$it", structuredType.Object, navigationSource.Object);
ResourceRangeVariableReferenceNode source = new ResourceRangeVariableReferenceNode("$it", rangeVariable);

Mock<IEdmComplexTypeReference> type = new Mock<IEdmComplexTypeReference>();

Mock<IEdmProperty> property = new Mock<IEdmProperty>();
property.Setup(p => p.Name).Returns("Address");
property.Setup(p => p.PropertyKind).Returns(EdmPropertyKind.Structural);
property.Setup(p => p.Type).Returns(type.Object);

SingleComplexNode node = new SingleComplexNode(source, property.Object);

// Act
string fullPropertyPath = QueryBinder.GetFullPropertyPath(node);

// Assert
Assert.Equal("Address", fullPropertyPath);
}

[Fact]
public void GetFullPropertyPath_WithSingleValuePropertyAccessNode()
{
// Arrange
Mock<IEdmStructuredTypeReference> structuredType = new Mock<IEdmStructuredTypeReference>();
Mock<IEdmNavigationSource> navigationSource = new Mock<IEdmNavigationSource>();
ResourceRangeVariable rangeVariable = new ResourceRangeVariable("$it", structuredType.Object, navigationSource.Object);
ResourceRangeVariableReferenceNode source = new ResourceRangeVariableReferenceNode("$it", rangeVariable);

Mock<IEdmTypeReference> type = new Mock<IEdmTypeReference>();

Mock<IEdmProperty> property = new Mock<IEdmProperty>();
property.Setup(p => p.Name).Returns("ZipCode");
property.Setup(p => p.PropertyKind).Returns(EdmPropertyKind.Structural);
property.Setup(p => p.Type).Returns(type.Object);

SingleValuePropertyAccessNode node = new SingleValuePropertyAccessNode(source, property.Object);

// Act
string fullPropertyPath = QueryBinder.GetFullPropertyPath(node);

// Assert
Assert.Equal("ZipCode", fullPropertyPath);
}

[Fact]
public void GetFullPropertyPath_WithSingleNavigationNode()
{
// Arrange
Mock<IEdmStructuredTypeReference> structuredType = new Mock<IEdmStructuredTypeReference>();
Mock<IEdmNavigationSource> navigationSource = new Mock<IEdmNavigationSource>();
ResourceRangeVariable rangeVariable = new ResourceRangeVariable("$it", structuredType.Object, navigationSource.Object);
ResourceRangeVariableReferenceNode source = new ResourceRangeVariableReferenceNode("$it", rangeVariable);

Mock<IEdmEntityTypeReference> type = new Mock<IEdmEntityTypeReference>();

Mock<IEdmNavigationProperty> property = new Mock<IEdmNavigationProperty>();
property.Setup(p => p.Name).Returns("Address");
property.Setup(p => p.Type).Returns(type.Object);

Mock<IEdmPathExpression> bindingPath = new Mock<IEdmPathExpression>();

SingleNavigationNode node = new SingleNavigationNode(source, property.Object, bindingPath.Object);

// Act
string fullPropertyPath = QueryBinder.GetFullPropertyPath(node);

// Assert
Assert.Equal("Address", fullPropertyPath);
}

[Fact]
public void GetFullPropertyPath_WithSingleValueOpenPropertyAccess()
{
// Arrange
Mock<IEdmStructuredTypeReference> structuredType = new Mock<IEdmStructuredTypeReference>();
Mock<IEdmNavigationSource> navigationSource = new Mock<IEdmNavigationSource>();
ResourceRangeVariable rangeVariable = new ResourceRangeVariable("$it", structuredType.Object, navigationSource.Object);
ResourceRangeVariableReferenceNode source = new ResourceRangeVariableReferenceNode("$it", rangeVariable);

SingleValueOpenPropertyAccessNode node = new SingleValueOpenPropertyAccessNode(source, "ZipCode");

// Act
string fullPropertyPath = QueryBinder.GetFullPropertyPath(node);

// Assert
Assert.Equal("ZipCode", fullPropertyPath);
}

[Fact]
public void GetFullPropertyPath_WithSingleValuePropertyAccessNodeInSingleComplexNode()
{
// Arrange
Mock<IEdmStructuredTypeReference> structuredType = new Mock<IEdmStructuredTypeReference>();
Mock<IEdmNavigationSource> navigationSource = new Mock<IEdmNavigationSource>();
ResourceRangeVariable rangeVariable = new ResourceRangeVariable("$it", structuredType.Object, navigationSource.Object);
ResourceRangeVariableReferenceNode source = new ResourceRangeVariableReferenceNode("$it", rangeVariable);

Mock<IEdmComplexTypeReference> complexType = new Mock<IEdmComplexTypeReference>();

Mock<IEdmProperty> complexProperty = new Mock<IEdmProperty>();
complexProperty.Setup(p => p.Name).Returns("Address");
complexProperty.Setup(p => p.PropertyKind).Returns(EdmPropertyKind.Structural);
complexProperty.Setup(p => p.Type).Returns(complexType.Object);

SingleComplexNode complexNode = new SingleComplexNode(source, complexProperty.Object);

Mock<IEdmTypeReference> type = new Mock<IEdmTypeReference>();

Mock<IEdmProperty> property = new Mock<IEdmProperty>();
property.Setup(p => p.Name).Returns("ZipCode");
property.Setup(p => p.PropertyKind).Returns(EdmPropertyKind.Structural);
property.Setup(p => p.Type).Returns(type.Object);

SingleValuePropertyAccessNode node = new SingleValuePropertyAccessNode(complexNode, property.Object);

// Act
string fullPropertyPath = QueryBinder.GetFullPropertyPath(node);

// Assert
Assert.Equal("Address\\ZipCode", fullPropertyPath);
}

[Fact]
public void GetFullPropertyPath_WithSingleValueOpenPropertyAccessNodeInSingleComplexNode()
{
// Arrange
Mock<IEdmStructuredTypeReference> structuredType = new Mock<IEdmStructuredTypeReference>();
Mock<IEdmNavigationSource> navigationSource = new Mock<IEdmNavigationSource>();
ResourceRangeVariable rangeVariable = new ResourceRangeVariable("$it", structuredType.Object, navigationSource.Object);
ResourceRangeVariableReferenceNode source = new ResourceRangeVariableReferenceNode("$it", rangeVariable);

Mock<IEdmComplexTypeReference> complexType = new Mock<IEdmComplexTypeReference>();

Mock<IEdmProperty> complexProperty = new Mock<IEdmProperty>();
complexProperty.Setup(p => p.Name).Returns("Address");
complexProperty.Setup(p => p.PropertyKind).Returns(EdmPropertyKind.Structural);
complexProperty.Setup(p => p.Type).Returns(complexType.Object);

SingleComplexNode complexNode = new SingleComplexNode(source, complexProperty.Object);

SingleValueOpenPropertyAccessNode node = new SingleValueOpenPropertyAccessNode(complexNode, "ZipCode");

// Act
string fullPropertyPath = QueryBinder.GetFullPropertyPath(node);

// Assert
Assert.Equal("Address\\ZipCode", fullPropertyPath);
}
}

public class MyQueryBinder : QueryBinder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.AspNetCore.OData.Routing.Controllers;
using Microsoft.AspNetCore.OData.TestCommon;
using Microsoft.AspNetCore.OData.Tests.Commons;
using Microsoft.AspNetCore.OData.Tests.Extensions;
using Microsoft.AspNetCore.OData.Tests.Models;
using Microsoft.OData.Edm;
using Microsoft.OData.ModelBuilder;
Expand Down Expand Up @@ -1297,6 +1298,72 @@ public void ApplyTo_Returns_Correct_Queryable(string filter, List<Dictionary<str
}
}
}

[Fact]
public void SortOnNestedDynamicPropertyWorks()
{
// Arrange
var modelBuilder = new ODataModelBuilder();

var addressType = modelBuilder.ComplexType<Address>();
addressType.HasDynamicProperties(a => a.DynamicProperties);

var customerType = modelBuilder.EntityType<Customer>();
customerType.ComplexProperty(c => c.Address);

var model = modelBuilder.GetEdmModel();

var context = new ODataQueryContext(model, typeof(Customer));

var request = RequestFactory.Create("Get", "http://localhost/?$apply=groupby((Address/DynamicCity))&$orderby=Address/DynamicCity");

var options = new ODataQueryOptions(context, request);

Customer[] customers =
{
new Customer
{
Address = new Address { DynamicProperties = new Dictionary<string, object> { { "DynamicCity", "City 2" } } },
},
new Customer
{
Address = new Address { DynamicProperties = new Dictionary<string, object> { { "DynamicCity", "City 1" } } },
},
new Customer
{
Address = new Address { DynamicProperties = new Dictionary<string, object> { { "DynamicCity", "City 2" } } },
},
new Customer
{
Address = new Address { DynamicProperties = new Dictionary<string, object> { { "DynamicCity", "City 1" } } },
}
};

// Act
IQueryable queryable = options.ApplyTo(customers.AsQueryable());

// Assert
Dictionary<string, object>[] expectedGroups =
{
new Dictionary<string, object> { { "Address/DynamicCity", "City 1" } },
new Dictionary<string, object> { { "Address/DynamicCity", "City 2" } }
};
var actualGroups = Assert.IsAssignableFrom<IEnumerable<DynamicTypeWrapper>>(queryable).ToList();
Assert.Equal(expectedGroups.Length, actualGroups.Count);

var aggEnum = actualGroups.GetEnumerator();
foreach (var expected in expectedGroups)
{
aggEnum.MoveNext();
var agg = aggEnum.Current;
foreach (var key in expected.Keys)
{
object value = GetValue(agg, key);
Assert.Equal(expected[key], value);
}
}
}

/*
[Theory]
[MemberData(nameof(CustomerTestAppliesMixedWithOthers))]
Expand Down
2 changes: 1 addition & 1 deletion tool/builder.versions.settings.targets
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<!-- For NuGet Package Dependencies -->
<PropertyGroup>
<ODataLibPackageDependency>[7.20.0, 8.0.0)</ODataLibPackageDependency>
<ODataLibPackageDependency>[7.21.2, 8.0.0)</ODataLibPackageDependency>
<ODataModelBuilderPackageDependency>[1.0.9, 2.0.0)</ODataModelBuilderPackageDependency>
</PropertyGroup>

Expand Down