Skip to content

Added value types support to the filters isnull & isnotnull. #549

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
Aug 29, 2019
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
22 changes: 20 additions & 2 deletions src/JsonApiDotNetCore/Extensions/IQueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,29 @@ private static Expression GetFilterExpressionLambda(Expression left, Expression
break;
case FilterOperations.isnotnull:
// {model.Id != null}
body = Expression.NotEqual(left, right);
if (left.Type.IsValueType &&
!(left.Type.IsGenericType && left.Type.GetGenericTypeDefinition() == typeof(Nullable<>)))
{
var nullableType = typeof(Nullable<>).MakeGenericType(left.Type);
body = Expression.NotEqual(Expression.Convert(left, nullableType), right);
}
else
{
body = Expression.NotEqual(left, right);
}
break;
case FilterOperations.isnull:
// {model.Id == null}
body = Expression.Equal(left, right);
if (left.Type.IsValueType &&
!(left.Type.IsGenericType && left.Type.GetGenericTypeDefinition() == typeof(Nullable<>)))
{
var nullableType = typeof(Nullable<>).MakeGenericType(left.Type);
body = Expression.Equal(Expression.Convert(left, nullableType), right);
}
else
{
body = Expression.Equal(left, right);
}
break;
default:
throw new JsonApiException(500, $"Unknown filter operation {operation}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,36 @@ public async Task Can_Filter_TodoItems_Using_IsNotNull_Operator()
Assert.All(todoItems, t => Assert.NotNull(t.UpdatedDate));
}

[Fact]
public async Task Can_Filter_TodoItems_ByParent_Using_IsNotNull_Operator()
{
// Arrange
var todoItem = _todoItemFaker.Generate();
todoItem.Assignee = new Person();

var otherTodoItem = _todoItemFaker.Generate();
otherTodoItem.Assignee = null;

_context.TodoItems.AddRange(new[] { todoItem, otherTodoItem });
_context.SaveChanges();

var httpMethod = new HttpMethod("GET");
var route = $"/api/v1/todo-items?filter[assignee.id]=isnotnull:";
var request = new HttpRequestMessage(httpMethod, route);

// Act
var response = await _fixture.Client.SendAsync(request);

Assert.Equal(HttpStatusCode.OK, response.StatusCode);

var body = await response.Content.ReadAsStringAsync();
var todoItems = _fixture.GetService<IJsonApiDeSerializer>().DeserializeList<TodoItem>(body);

// Assert
Assert.NotEmpty(todoItems);
Assert.All(todoItems, t => Assert.NotNull(t.Assignee));
}

[Fact]
public async Task Can_Filter_TodoItems_Using_IsNull_Operator()
{
Expand Down Expand Up @@ -205,6 +235,36 @@ public async Task Can_Filter_TodoItems_Using_IsNull_Operator()
Assert.All(todoItems, t => Assert.Null(t.UpdatedDate));
}

[Fact]
public async Task Can_Filter_TodoItems_ByParent_Using_IsNull_Operator()
{
// Arrange
var todoItem = _todoItemFaker.Generate();
todoItem.Assignee = null;

var otherTodoItem = _todoItemFaker.Generate();
otherTodoItem.Assignee = new Person();

_context.TodoItems.AddRange(new[] { todoItem, otherTodoItem });
_context.SaveChanges();

var httpMethod = new HttpMethod("GET");
var route = $"/api/v1/todo-items?filter[assignee.id]=isnull:";
var request = new HttpRequestMessage(httpMethod, route);

// Act
var response = await _fixture.Client.SendAsync(request);

Assert.Equal(HttpStatusCode.OK, response.StatusCode);

var body = await response.Content.ReadAsStringAsync();
var todoItems = _fixture.GetService<IJsonApiDeSerializer>().DeserializeList<TodoItem>(body);

// Assert
Assert.NotEmpty(todoItems);
Assert.All(todoItems, t => Assert.Null(t.Assignee));
}

[Fact]
public async Task Can_Filter_TodoItems_Using_Like_Operator()
{
Expand Down