Skip to content

WIP: Support Filtering using ne:null on nullable attribute #386

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

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

### Development

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.
If your bash prompt supports the globstar, you can recursively delete the `bin` and `obj` directories:

```bash
shopt -s globstar
rm -rf **/bin && rm -rf **/obj
```
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
12 changes: 10 additions & 2 deletions src/JsonApiDotNetCore/Extensions/IQueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,14 @@ public static IQueryable<TSource> Filter<TSource>(this IQueryable<TSource> sourc
return source.Where(lambdaIn);
}
else
{ // convert the incoming value to the target value type
{
var isNullabe = IsNullable(property.PropertyType);
var propertyValue = filterQuery.PropertyValue;
var value = isNullabe && propertyValue == "" ? null : propertyValue;

// convert the incoming value to the target value type
// "1" -> 1
var convertedValue = TypeHelper.ConvertType(filterQuery.PropertyValue, property.PropertyType);
var convertedValue = TypeHelper.ConvertType(value, property.PropertyType);
// {model}
var parameter = Expression.Parameter(concreteType, "model");
// {model.Id}
Expand Down Expand Up @@ -204,6 +209,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
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,29 @@ public async Task Can_Filter_TodoItems()
Assert.Equal(todoItem.Ordinal, todoItemResult.Ordinal);
}

[Fact]
public async Task Can_Filter_TodoItems_Using_NotEqual_Operator()
{
// Arrange
var todoItem = _todoItemFaker.Generate();
todoItem.UpdatedDate = null;
_context.TodoItems.Add(todoItem);
_context.SaveChanges();

var httpMethod = new HttpMethod("GET");
var route = $"/api/v1/todo-items?filter[updated-date]=ne:";
var request = new HttpRequestMessage(httpMethod, route);

// Act
var response = await _fixture.Client.SendAsync(request);
var body = await response.Content.ReadAsStringAsync();
var deserializedBody = _fixture.GetService<IJsonApiDeSerializer>().DeserializeList<TodoItem>(body);

// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Empty(deserializedBody);
}

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