From 76ce3ddc90db312c16bd7f18c32c54ec7e14b8f1 Mon Sep 17 00:00:00 2001 From: Bart Koelman <10324372+bkoelman@users.noreply.github.com> Date: Wed, 6 Apr 2022 18:13:42 +0200 Subject: [PATCH] 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. 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. --- .../Resources/SortExpressionLambdaConverter.cs | 5 ++++- .../ResourceInheritance/WheelSortDefinition.cs | 7 +++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/JsonApiDotNetCore/Resources/SortExpressionLambdaConverter.cs b/src/JsonApiDotNetCore/Resources/SortExpressionLambdaConverter.cs index 3736fff6d9..a2371419b6 100644 --- a/src/JsonApiDotNetCore/Resources/SortExpressionLambdaConverter.cs +++ b/src/JsonApiDotNetCore/Resources/SortExpressionLambdaConverter.cs @@ -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.Id) && memberExpression.Expression != null + ? _resourceGraph.GetResourceType(memberExpression.Expression.Type) + : _resourceGraph.GetResourceType(memberExpression.Member.DeclaringType!); + AttrAttribute? attribute = resourceType.FindAttributeByPropertyName(memberExpression.Member.Name); if (attribute != null) diff --git a/test/JsonApiDotNetCoreTests/IntegrationTests/ResourceInheritance/WheelSortDefinition.cs b/test/JsonApiDotNetCoreTests/IntegrationTests/ResourceInheritance/WheelSortDefinition.cs index f727c011df..e5ae558489 100644 --- a/test/JsonApiDotNetCoreTests/IntegrationTests/ResourceInheritance/WheelSortDefinition.cs +++ b/test/JsonApiDotNetCoreTests/IntegrationTests/ResourceInheritance/WheelSortDefinition.cs @@ -43,6 +43,7 @@ private SortExpression CreateSortFromExpressionSyntax() { AttrAttribute paintColorAttribute = ResourceGraph.GetResourceType().GetAttributeByPropertyName(nameof(ChromeWheel.PaintColor)); AttrAttribute hasTubeAttribute = ResourceGraph.GetResourceType().GetAttributeByPropertyName(nameof(CarbonWheel.HasTube)); + AttrAttribute idAttribute = ResourceGraph.GetResourceType().GetAttributeByPropertyName(nameof(Wheel.Id)); var cylinderCountChain = new ResourceFieldChainExpression(ImmutableArray.Create( ResourceGraph.GetResourceType().GetRelationshipByPropertyName(nameof(Wheel.Vehicle)), @@ -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()); } @@ -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) }); } }