Skip to content

Commit

Permalink
Fixed: Nullable properties will no longer lead to errors in the sorti…
Browse files Browse the repository at this point in the history
…ng middleware (#1419)
  • Loading branch information
PascalSenn authored and michaelstaib committed Jan 27, 2020
1 parent bbab143 commit 012c471
Show file tree
Hide file tree
Showing 17 changed files with 91 additions and 2 deletions.
67 changes: 67 additions & 0 deletions src/Core/Types.Sorting.Tests/QueryableSortVisitorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,72 @@ public void Sort_NoSortSpecified_ShouldReturnUnalteredSource()
);
}

[Fact]
public void Sort_Nullable_ShouldSortNullableProperlyAsc()
{
// arrange
var value = new ObjectValueNode(
new ObjectFieldNode("nullableInt",
new EnumValueNode(SortOperationKind.Asc)
)
);

FooSortType sortType = CreateType(new FooSortType());

IQueryable<Foo> a = new[]
{
new Foo {Bar = "b"},
new Foo {Bar = "c", NullableInt = 2},
new Foo {Bar = "a", NullableInt = 1}
}.AsQueryable();

// act
var filter = new QueryableSortVisitor(
sortType, typeof(Foo));
value.Accept(filter);
IQueryable<Foo> aFiltered = filter.Sort(a);

// assert
Assert.Collection(aFiltered,
foo => Assert.Equal("b", foo.Bar),
foo => Assert.Equal("a", foo.Bar),
foo => Assert.Equal("c", foo.Bar)
);
}

[Fact]
public void Sort_Nullable_ShouldSortNullableProperlyDesc()
{
// arrange
var value = new ObjectValueNode(
new ObjectFieldNode("nullableInt",
new EnumValueNode(SortOperationKind.Desc)
)
);

FooSortType sortType = CreateType(new FooSortType());

IQueryable<Foo> a = new[]
{
new Foo {Bar = "b"},
new Foo {Bar = "c", NullableInt = 2},
new Foo {Bar = "a", NullableInt = 1}
}.AsQueryable();

// act
var filter = new QueryableSortVisitor(
sortType, typeof(Foo));
value.Accept(filter);
IQueryable<Foo> aFiltered = filter.Sort(a);

// assert
Assert.Collection(aFiltered,
foo => Assert.Equal("c", foo.Bar),
foo => Assert.Equal("a", foo.Bar),
foo => Assert.Equal("b", foo.Bar)
);
}

public class FooSortType
: SortInputType<Foo>
{
Expand All @@ -160,6 +226,7 @@ protected override void Configure(

public class Foo
{
public int? NullableInt { get; set; }
public string Bar { get; set; }
public string Baz { get; set; }
}
Expand Down
1 change: 1 addition & 0 deletions src/Core/Types.Sorting.Tests/SortInputTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ private class FooDirective { }

private class Foo
{
public bool? NullableBoolean { get; set; }
public string Bar { get; set; }
public string Baz { get; set; }
}
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Types.Sorting.Tests/Types.Sorting.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="Current">
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="Current">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<ChilliCurrentDirectory>$(MSBuildThisFileDirectory.TrimEnd('\').TrimEnd('/'))</ChilliCurrentDirectory>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Query {
input FooSort {
bar: SortOperationKind
baz: SortOperationKind
nullableBoolean: SortOperationKind
}

enum SortOperationKind {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Query {
input FooSort {
bar: SortOperationKind
baz: SortOperationKind
nullableBoolean: SortOperationKind
}

enum SortOperationKind {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type Query {

input FooSort {
bar: SortOperationKind
nullableBoolean: SortOperationKind
}

enum SortOperationKind {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type Query {

input FooSort {
bar: SortOperationKind
nullableBoolean: SortOperationKind
quux: SortOperationKind
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Query {
input FooSort {
bar: SortOperationKind
baz: SortOperationKind
nullableBoolean: SortOperationKind
}

enum SortOperationKind {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Query {
input FooSort @foo {
bar: SortOperationKind
baz: SortOperationKind
nullableBoolean: SortOperationKind
}

enum SortOperationKind {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Query {
input FooSort @foo {
bar: SortOperationKind
baz: SortOperationKind
nullableBoolean: SortOperationKind
}

enum SortOperationKind {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Query {
input FooSort @foo {
bar: SortOperationKind
baz: SortOperationKind
nullableBoolean: SortOperationKind
}

enum SortOperationKind {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Query {
input FooSort @foo {
bar: SortOperationKind
baz: SortOperationKind
nullableBoolean: SortOperationKind
}

enum SortOperationKind {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Query {
input FooSort @foo {
bar: SortOperationKind
baz: SortOperationKind
nullableBoolean: SortOperationKind
}

enum SortOperationKind {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Query {
input Test {
bar: SortOperationKind
baz: SortOperationKind
nullableBoolean: SortOperationKind
}

enum SortOperationKind {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Query {
input StringFoo {
bar: SortOperationKind
baz: SortOperationKind
nullableBoolean: SortOperationKind
}

enum SortOperationKind {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Query {
input StringFoo {
bar: SortOperationKind
baz: SortOperationKind
nullableBoolean: SortOperationKind
}

enum SortOperationKind {
Expand Down
10 changes: 9 additions & 1 deletion src/Core/Types.Sorting/SortInputTypeDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,15 @@ private bool TryCreateImplicitSorting(
PropertyInfo property,
out SortFieldDefinition definition)
{
if (typeof(IComparable).IsAssignableFrom(property.PropertyType))
Type type = property.PropertyType;

if (type.IsGenericType
&& System.Nullable.GetUnderlyingType(type) is Type nullableType)
{
type = nullableType;
}

if (typeof(IComparable).IsAssignableFrom(type))
{
var field = new SortFieldDescriptor(Context, property);
definition = field.CreateDefinition();
Expand Down

0 comments on commit 012c471

Please sign in to comment.