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) }); } }