Skip to content

Additional filter operations: isnull and isnotnull #387

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
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
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,39 @@ public class Startup
}
}
```

### Development
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏 👏 👏


Restore all nuget packages with:

```bash
dotnet restore
```

#### Testing

Running tests locally requires access to a postgresql database.
If you have docker installed, this can be propped up via:

```bash
docker run --rm --name jsonapi-dotnet-core-testing \
-e POSTGRES_DB=JsonApiDotNetCoreExample \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASSWORD=postgres \
-p 5432:5432 \
postgres
```

And then to run the tests:

```bash
dotnet test
```

#### Cleaning

Sometimes the compiled files can be dirty / corrupt from other branches / failed builds.

```bash
dotnet clean
```
6 changes: 6 additions & 0 deletions src/Examples/JsonApiDotNetCoreExample/Models/TodoItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ public TodoItem()

[Attr("achieved-date", isFilterable: false, isSortable: false)]
public DateTime? AchievedDate { get; set; }


[Attr("updated-date")]
public DateTime? UpdatedDate { get; set; }



public int? OwnerId { get; set; }
public int? AssigneeId { get; set; }
Expand Down
30 changes: 27 additions & 3 deletions src/JsonApiDotNetCore/Extensions/IQueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,32 @@ public static IQueryable<TSource> Filter<TSource>(this IQueryable<TSource> sourc

var concreteType = typeof(TSource);
var property = concreteType.GetProperty(filterQuery.FilteredAttribute.InternalAttributeName);
var op = filterQuery.FilterOperation;

if (property == null)
throw new ArgumentException($"'{filterQuery.FilteredAttribute.InternalAttributeName}' is not a valid property of '{concreteType}'");

try
{
if (filterQuery.FilterOperation == FilterOperations.@in || filterQuery.FilterOperation == FilterOperations.nin)
if (op == FilterOperations.@in || op == FilterOperations.nin)
{
string[] propertyValues = filterQuery.PropertyValue.Split(',');
var lambdaIn = ArrayContainsPredicate<TSource>(propertyValues, property.Name, filterQuery.FilterOperation);
var lambdaIn = ArrayContainsPredicate<TSource>(propertyValues, property.Name, op);

return source.Where(lambdaIn);
}
else if (op == FilterOperations.isnotnull || op == FilterOperations.isnull) {
// {model}
var parameter = Expression.Parameter(concreteType, "model");
// {model.Id}
var left = Expression.PropertyOrField(parameter, property.Name);
var right = Expression.Constant(null);

var body = GetFilterExpressionLambda(left, right, op);
var lambda = Expression.Lambda<Func<TSource, bool>>(body, parameter);

return source.Where(lambda);
}
else
{ // convert the incoming value to the target value type
// "1" -> 1
Expand All @@ -137,7 +150,7 @@ public static IQueryable<TSource> Filter<TSource>(this IQueryable<TSource> sourc
// {1}
var right = Expression.Constant(convertedValue, property.PropertyType);

var body = GetFilterExpressionLambda(left, right, filterQuery.FilterOperation);
var body = GetFilterExpressionLambda(left, right, op);

var lambda = Expression.Lambda<Func<TSource, bool>>(body, parameter);

Expand Down Expand Up @@ -204,6 +217,9 @@ public static IQueryable<TSource> Filter<TSource>(this IQueryable<TSource> sourc
}
}

private static bool IsNullable(Type type) => type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);


private static Expression GetFilterExpressionLambda(Expression left, Expression right, FilterOperations operation)
{
Expression body;
Expand Down Expand Up @@ -236,6 +252,14 @@ private static Expression GetFilterExpressionLambda(Expression left, Expression
case FilterOperations.ne:
body = Expression.NotEqual(left, right);
break;
case FilterOperations.isnotnull:
// {model.Id != null}
body = Expression.NotEqual(left, right);
break;
case FilterOperations.isnull:
// {model.Id == null}
body = Expression.Equal(left, right);
break;
default:
throw new JsonApiException(500, $"Unknown filter operation {operation}");
}
Expand Down
4 changes: 3 additions & 1 deletion src/JsonApiDotNetCore/Internal/Query/FilterOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public enum FilterOperations
like = 5,
ne = 6,
@in = 7, // prefix with @ to use keyword
nin = 8
nin = 8,
isnull = 9,
isnotnull = 10
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
Expand Down Expand Up @@ -90,6 +91,66 @@ public async Task Can_Filter_TodoItems()
Assert.Equal(todoItem.Ordinal, todoItemResult.Ordinal);
}

[Fact]
public async Task Can_Filter_TodoItems_Using_IsNotNull_Operator()
{
// Arrange
var todoItem = _todoItemFaker.Generate();
todoItem.UpdatedDate = new DateTime();

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

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

var httpMethod = new HttpMethod("GET");
var route = $"/api/v1/todo-items?filter[updated-date]=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.UpdatedDate));
}

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

var otherTodoItem = _todoItemFaker.Generate();
otherTodoItem.UpdatedDate = new DateTime();

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

var httpMethod = new HttpMethod("GET");
var route = $"/api/v1/todo-items?filter[updated-date]=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.UpdatedDate));
}

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