Skip to content

Commit

Permalink
Fixed: Sorting by ID from a lambda expression in a resource definitio…
Browse files Browse the repository at this point in the history
…n fails with error: Type 'JsonApiDotNetCore.Resources.Identifiable`1[System.Int64]' does not exist in the resource graph. (#1151)

The reason for failure is that in an expression like `book => book.Id`, the `Id` property is declared on `Identifiable<>`. The fix is to look at the type of the containing expression (which is `Book` in this case) when the `Id` property is used.
  • Loading branch information
bkoelman authored Apr 6, 2022
1 parent 0e42ebf commit 4dc8115
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ private static (Expression? innerExpression, bool isCount) TryReadCount(Expressi
{
if (expression is MemberExpression memberExpression)
{
ResourceType resourceType = _resourceGraph.GetResourceType(memberExpression.Member.DeclaringType!);
ResourceType resourceType = memberExpression.Member.Name == nameof(Identifiable<object>.Id) && memberExpression.Expression != null
? _resourceGraph.GetResourceType(memberExpression.Expression.Type)
: _resourceGraph.GetResourceType(memberExpression.Member.DeclaringType!);

AttrAttribute? attribute = resourceType.FindAttributeByPropertyName(memberExpression.Member.Name);

if (attribute != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ private SortExpression CreateSortFromExpressionSyntax()
{
AttrAttribute paintColorAttribute = ResourceGraph.GetResourceType<ChromeWheel>().GetAttributeByPropertyName(nameof(ChromeWheel.PaintColor));
AttrAttribute hasTubeAttribute = ResourceGraph.GetResourceType<CarbonWheel>().GetAttributeByPropertyName(nameof(CarbonWheel.HasTube));
AttrAttribute idAttribute = ResourceGraph.GetResourceType<Wheel>().GetAttributeByPropertyName(nameof(Wheel.Id));

var cylinderCountChain = new ResourceFieldChainExpression(ImmutableArray.Create<ResourceFieldAttribute>(
ResourceGraph.GetResourceType<Wheel>().GetRelationshipByPropertyName(nameof(Wheel.Vehicle)),
Expand All @@ -53,7 +54,8 @@ private SortExpression CreateSortFromExpressionSyntax()
{
new SortElementExpression(new ResourceFieldChainExpression(paintColorAttribute), true),
new SortElementExpression(new ResourceFieldChainExpression(hasTubeAttribute), false),
new SortElementExpression(new CountExpression(cylinderCountChain), true)
new SortElementExpression(new CountExpression(cylinderCountChain), true),
new SortElementExpression(new ResourceFieldChainExpression(idAttribute), true)
}.ToImmutableArray());
}

Expand All @@ -63,7 +65,8 @@ private SortExpression CreateSortFromLambdaSyntax()
{
(wheel => (wheel as ChromeWheel)!.PaintColor, ListSortDirection.Ascending),
(wheel => ((CarbonWheel)wheel).HasTube, ListSortDirection.Descending),
(wheel => ((GasolineEngine)((Car)wheel.Vehicle!).Engine).Cylinders.Count, ListSortDirection.Ascending)
(wheel => ((GasolineEngine)((Car)wheel.Vehicle!).Engine).Cylinders.Count, ListSortDirection.Ascending),
(wheel => wheel.Id, ListSortDirection.Ascending)
});
}
}

0 comments on commit 4dc8115

Please sign in to comment.