Skip to content

Fixed: error setting default sort for non-string properties #754

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

Merged
merged 1 commit into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 13 additions & 6 deletions src/JsonApiDotNetCore/Internal/ResourceGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ private IEnumerable<IResourceField> Getter<T>(Expression<Func<T, dynamic>> selec

var targeted = new List<IResourceField>();

if (selector.Body is MemberExpression memberExpression)
var selectorBody = RemoveConvert(selector.Body);

if (selectorBody is MemberExpression memberExpression)
{ // model => model.Field1
try
{
Expand All @@ -97,8 +99,7 @@ private IEnumerable<IResourceField> Getter<T>(Expression<Func<T, dynamic>> selec
}
}


if (selector.Body is NewExpression newExpression)
if (selectorBody is NewExpression newExpression)
{ // model => new { model.Field1, model.Field2 }
string memberName = null;
try
Expand All @@ -119,11 +120,17 @@ private IEnumerable<IResourceField> Getter<T>(Expression<Func<T, dynamic>> selec
}
}

throw new ArgumentException($"The expression returned by '{selector}' for '{GetType()}' is of type {selector.Body.GetType()}"
+ " and cannot be used to select resource attributes. The type must be a NewExpression.Example: article => new { article.Author };");

throw new ArgumentException(
$"The expression '{selector}' should select a single property or select multiple properties into an anonymous type. " +
$"For example: 'article => article.Title' or 'article => new {{ article.Title, article.PageCount }}'.");
}

private static Expression RemoveConvert(Expression expression)
=> expression is UnaryExpression unaryExpression
&& unaryExpression.NodeType == ExpressionType.Convert
? RemoveConvert(unaryExpression.Operand)
: expression;

private void ThrowNotExposedError(string memberName, FieldFilterType type)
{
throw new ArgumentException($"{memberName} is not an json:api exposed {type:g}.");
Expand Down
17 changes: 10 additions & 7 deletions test/UnitTests/Models/ResourceDefinitionTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using JsonApiDotNetCore.Builders;
using JsonApiDotNetCore.Internal.Query;
Expand All @@ -23,7 +24,7 @@ public void Property_Sort_Order_Uses_NewExpression()
// Assert
Assert.Equal(2, sorts.Count);

Assert.Equal(nameof(Model.Prop), sorts[0].Attribute.PropertyInfo.Name);
Assert.Equal(nameof(Model.CreatedAt), sorts[0].Attribute.PropertyInfo.Name);
Assert.Equal(SortDirection.Ascending, sorts[0].SortDirection);

Assert.Equal(nameof(Model.Password), sorts[1].Attribute.PropertyInfo.Name);
Expand Down Expand Up @@ -62,7 +63,7 @@ public class Model : Identifiable
{
[Attr] public string AlwaysExcluded { get; set; }
[Attr] public string Password { get; set; }
[Attr] public string Prop { get; set; }
[Attr] public DateTime CreatedAt { get; set; }
}

public sealed class RequestFilteredResource : ResourceDefinition<Model>
Expand All @@ -72,19 +73,21 @@ public sealed class RequestFilteredResource : ResourceDefinition<Model>
public RequestFilteredResource(bool isAdmin) : base(new ResourceGraphBuilder(new JsonApiOptions(), NullLoggerFactory.Instance).AddResource<Model>().Build())
{
if (isAdmin)
HideFields(m => m.AlwaysExcluded);
HideFields(model => model.AlwaysExcluded);
else
HideFields(m => new { m.AlwaysExcluded, m.Password });
HideFields(model => new { model.AlwaysExcluded, model.Password });
}

public override QueryFilters GetQueryFilters()
=> new QueryFilters {
{ "is-active", (query, value) => query.Select(x => x) }
};

public override PropertySortOrder GetDefaultSortOrder()
=> new PropertySortOrder {
(t => t.Prop, SortDirection.Ascending),
(t => t.Password, SortDirection.Descending)
=> new PropertySortOrder
{
(model => model.CreatedAt, SortDirection.Ascending),
(model => model.Password, SortDirection.Descending)
};
}
}