-
-
Notifications
You must be signed in to change notification settings - Fork 158
Add IN filter operation for array searching #288
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
|
||
namespace JsonApiDotNetCore.Internal | ||
|
@@ -54,5 +56,27 @@ public static T ConvertType<T>(object value) | |
{ | ||
return (T)ConvertType(value, typeof(T)); | ||
} | ||
|
||
/// <summary> | ||
/// Convert collection of query string params to Collection of concrete Type | ||
/// </summary> | ||
/// <param name="values">Collection like ["10","20","30"]</param> | ||
/// <param name="type">Non array type. For e.g. int</param> | ||
/// <returns>Collection of concrete type</returns> | ||
public static object ConvertListType(IEnumerable<string> values, Type type) | ||
{ | ||
var convertedArray = new List<object>(); | ||
foreach (var value in values) | ||
{ | ||
convertedArray.Add(ConvertType(value, type)); | ||
} | ||
var listType = typeof(List<>).MakeGenericType(type); | ||
IList list = (IList)Activator.CreateInstance(listType); | ||
foreach (var item in convertedArray) | ||
{ | ||
list.Add(item); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2 things:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This statement was hell of pain: Expression.Call does some unboxing during runtime and if there are uncompatible object types, it throws exception of Type mismatch. So idea of this is to convert boxed types in object to compatible IList of target property. I'll make some investigation later to simplify enumerations. |
||
return list; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,14 +82,23 @@ protected virtual List<FilterQuery> ParseFilterQuery(string key, string value) | |
// expected input = filter[id]=1 | ||
// expected input = filter[id]=eq:1 | ||
var queries = new List<FilterQuery>(); | ||
|
||
var propertyName = key.Split(QueryConstants.OPEN_BRACKET, QueryConstants.CLOSE_BRACKET)[1]; | ||
|
||
var values = value.Split(QueryConstants.COMMA); | ||
foreach (var val in values) | ||
// InArray case | ||
var op = GetFilterOperation(value); | ||
if (op == FilterOperations.@in.ToString()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should ignore case op.Equals(FilterOperations.@in.ToString(), StringComparer.OrdinalIgnoreCase) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, good point |
||
{ | ||
(var operation, var filterValue) = ParseFilterOperation(value); | ||
queries.Add(new FilterQuery(propertyName, filterValue, op)); | ||
} | ||
else | ||
{ | ||
(var operation, var filterValue) = ParseFilterOperation(val); | ||
queries.Add(new FilterQuery(propertyName, filterValue, operation)); | ||
var values = value.Split(QueryConstants.COMMA); | ||
foreach (var val in values) | ||
{ | ||
(var operation, var filterValue) = ParseFilterOperation(val); | ||
queries.Add(new FilterQuery(propertyName, filterValue, operation)); | ||
} | ||
} | ||
|
||
return queries; | ||
|
@@ -100,19 +109,15 @@ protected virtual (string operation, string value) ParseFilterOperation(string v | |
if (value.Length < 3) | ||
return (string.Empty, value); | ||
|
||
var operation = value.Split(QueryConstants.COLON); | ||
var operation = GetFilterOperation(value); | ||
var values = value.Split(QueryConstants.COLON); | ||
|
||
if (operation.Length == 1) | ||
return (string.Empty, value); | ||
|
||
// remove prefix from value | ||
if (Enum.TryParse(operation[0], out FilterOperations op) == false) | ||
if (string.IsNullOrEmpty(operation)) | ||
return (string.Empty, value); | ||
|
||
var prefix = operation[0]; | ||
value = string.Join(QueryConstants.COLON_STR, operation.Skip(1)); | ||
value = string.Join(QueryConstants.COLON_STR, values.Skip(1)); | ||
|
||
return (prefix, value); | ||
return (operation, value); | ||
} | ||
|
||
protected virtual PageQuery ParsePageQuery(PageQuery pageQuery, string key, string value) | ||
|
@@ -225,6 +230,20 @@ protected virtual AttrAttribute GetAttribute(string propertyName) | |
} | ||
} | ||
|
||
private string GetFilterOperation(string value) | ||
{ | ||
var operation = value.Split(QueryConstants.COLON); | ||
|
||
if (operation.Length == 1) | ||
return string.Empty; | ||
|
||
// remove prefix from value | ||
if (Enum.TryParse(operation[0], out FilterOperations op) == false) | ||
return string.Empty; | ||
|
||
return operation[0]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's assign There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, no problem |
||
} | ||
|
||
private FilterQuery BuildFilterQuery(ReadOnlySpan<char> query, string propertyName) | ||
{ | ||
var (operation, filterValue) = ParseFilterOperation(query.ToString()); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
|
@@ -8,6 +9,7 @@ | |
using JsonApiDotNetCore.Serialization; | ||
using JsonApiDotNetCoreExample.Data; | ||
using JsonApiDotNetCoreExample.Models; | ||
using Microsoft.EntityFrameworkCore; | ||
using Newtonsoft.Json; | ||
using Xunit; | ||
using Person = JsonApiDotNetCoreExample.Models.Person; | ||
|
@@ -131,5 +133,74 @@ public async Task Can_Filter_On_Not_Equal_Values() | |
Assert.Equal(HttpStatusCode.OK, response.StatusCode); | ||
Assert.False(deserializedTodoItems.Any(i => i.Ordinal == todoItem.Ordinal)); | ||
} | ||
|
||
[Fact] | ||
public async Task Can_Filter_On_In_Array_Values() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need a negative test, i.e. instances with property values that are not included in the array get excluded from the results There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I'll add this test case. |
||
{ | ||
// arrange | ||
var context = _fixture.GetService<AppDbContext>(); | ||
var todoItems = _todoItemFaker.Generate(3); | ||
var guids = new List<Guid>(); | ||
foreach (var item in todoItems) | ||
{ | ||
context.TodoItems.Add(item); | ||
guids.Add(item.GuidProperty); | ||
} | ||
context.SaveChanges(); | ||
|
||
var totalCount = context.TodoItems.Count(); | ||
var httpMethod = new HttpMethod("GET"); | ||
var route = $"/api/v1/todo-items?filter[guid-property]=in:{string.Join(",", guids)}"; | ||
var request = new HttpRequestMessage(httpMethod, route); | ||
|
||
// act | ||
var response = await _fixture.Client.SendAsync(request); | ||
var body = await response.Content.ReadAsStringAsync(); | ||
var deserializedTodoItems = _fixture | ||
.GetService<IJsonApiDeSerializer>() | ||
.DeserializeList<TodoItem>(body); | ||
|
||
// assert | ||
Assert.Equal(HttpStatusCode.OK, response.StatusCode); | ||
Assert.Equal(guids.Count(), deserializedTodoItems.Count()); | ||
foreach (var item in deserializedTodoItems) | ||
Assert.True(guids.Contains(item.GuidProperty)); | ||
} | ||
|
||
[Fact] | ||
public async Task Can_Filter_On_Related_In_Array_Values() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👏 👏 👏 |
||
{ | ||
// arrange | ||
var context = _fixture.GetService<AppDbContext>(); | ||
var todoItems = _todoItemFaker.Generate(3); | ||
var ownerFirstNames = new List<string>(); | ||
foreach (var item in todoItems) | ||
{ | ||
var person = _personFaker.Generate(); | ||
ownerFirstNames.Add(person.FirstName); | ||
item.Owner = person; | ||
context.TodoItems.Add(item); | ||
} | ||
context.SaveChanges(); | ||
|
||
var httpMethod = new HttpMethod("GET"); | ||
var route = $"/api/v1/todo-items?include=owner&filter[owner.first-name]=in:{string.Join(",", ownerFirstNames)}"; | ||
var request = new HttpRequestMessage(httpMethod, route); | ||
|
||
// act | ||
var response = await _fixture.Client.SendAsync(request); | ||
var body = await response.Content.ReadAsStringAsync(); | ||
var documents = JsonConvert.DeserializeObject<Documents>(await response.Content.ReadAsStringAsync()); | ||
var included = documents.Included; | ||
|
||
// assert | ||
Assert.Equal(HttpStatusCode.OK, response.StatusCode); | ||
Assert.Equal(ownerFirstNames.Count(), documents.Data.Count()); | ||
Assert.NotNull(included); | ||
Assert.NotEmpty(included); | ||
foreach (var item in included) | ||
Assert.True(ownerFirstNames.Contains(item.Attributes["first-name"])); | ||
|
||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I recommend consolidating this into:
Also, the result of this query never changes. It would probably be best to run it in a static constructor and store the
MethodInfo
in a private field so we don't do this on every invocation at runtime. I suspect there are several places in these extensions that could benefit from such a refactorThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uf, this is much better solution. And method will be singleton, good point.