Skip to content

Commit 4dc8115

Browse files
authored
Fixed: Sorting by ID from a lambda expression in a resource definition 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.
1 parent 0e42ebf commit 4dc8115

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/JsonApiDotNetCore/Resources/SortExpressionLambdaConverter.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@ private static (Expression? innerExpression, bool isCount) TryReadCount(Expressi
116116
{
117117
if (expression is MemberExpression memberExpression)
118118
{
119-
ResourceType resourceType = _resourceGraph.GetResourceType(memberExpression.Member.DeclaringType!);
119+
ResourceType resourceType = memberExpression.Member.Name == nameof(Identifiable<object>.Id) && memberExpression.Expression != null
120+
? _resourceGraph.GetResourceType(memberExpression.Expression.Type)
121+
: _resourceGraph.GetResourceType(memberExpression.Member.DeclaringType!);
122+
120123
AttrAttribute? attribute = resourceType.FindAttributeByPropertyName(memberExpression.Member.Name);
121124

122125
if (attribute != null)

test/JsonApiDotNetCoreTests/IntegrationTests/ResourceInheritance/WheelSortDefinition.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ private SortExpression CreateSortFromExpressionSyntax()
4343
{
4444
AttrAttribute paintColorAttribute = ResourceGraph.GetResourceType<ChromeWheel>().GetAttributeByPropertyName(nameof(ChromeWheel.PaintColor));
4545
AttrAttribute hasTubeAttribute = ResourceGraph.GetResourceType<CarbonWheel>().GetAttributeByPropertyName(nameof(CarbonWheel.HasTube));
46+
AttrAttribute idAttribute = ResourceGraph.GetResourceType<Wheel>().GetAttributeByPropertyName(nameof(Wheel.Id));
4647

4748
var cylinderCountChain = new ResourceFieldChainExpression(ImmutableArray.Create<ResourceFieldAttribute>(
4849
ResourceGraph.GetResourceType<Wheel>().GetRelationshipByPropertyName(nameof(Wheel.Vehicle)),
@@ -53,7 +54,8 @@ private SortExpression CreateSortFromExpressionSyntax()
5354
{
5455
new SortElementExpression(new ResourceFieldChainExpression(paintColorAttribute), true),
5556
new SortElementExpression(new ResourceFieldChainExpression(hasTubeAttribute), false),
56-
new SortElementExpression(new CountExpression(cylinderCountChain), true)
57+
new SortElementExpression(new CountExpression(cylinderCountChain), true),
58+
new SortElementExpression(new ResourceFieldChainExpression(idAttribute), true)
5759
}.ToImmutableArray());
5860
}
5961

@@ -63,7 +65,8 @@ private SortExpression CreateSortFromLambdaSyntax()
6365
{
6466
(wheel => (wheel as ChromeWheel)!.PaintColor, ListSortDirection.Ascending),
6567
(wheel => ((CarbonWheel)wheel).HasTube, ListSortDirection.Descending),
66-
(wheel => ((GasolineEngine)((Car)wheel.Vehicle!).Engine).Cylinders.Count, ListSortDirection.Ascending)
68+
(wheel => ((GasolineEngine)((Car)wheel.Vehicle!).Engine).Cylinders.Count, ListSortDirection.Ascending),
69+
(wheel => wheel.Id, ListSortDirection.Ascending)
6770
});
6871
}
6972
}

0 commit comments

Comments
 (0)