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 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
13 changes: 9 additions & 4 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 @@ -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 @@ -83,6 +83,7 @@ public static ODataModelBuilder Add_Address_ComplexType(this ODataModelBuilder b
address.Property(a => a.Street);
address.Property(a => a.City);
address.Property(a => a.State);
address.HasDynamicProperties(a => a.DynamicProperties);
return builder;
}

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,66 @@ public void ApplyTo_Returns_Correct_Queryable(string filter, List<Dictionary<str
}
}
}

[Fact]
public void SortOnNestedDynamicPropertyWorks()
{
// Arrange
var model = new ODataModelBuilder()
.Add_Customer_EntityType_With_Address()
.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
Loading