Skip to content

Commit

Permalink
Fix JArray filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine BIDAULT committed Dec 3, 2023
1 parent 14520a1 commit 39ee2f4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
13 changes: 11 additions & 2 deletions JsonRuleEngine.Net.Tests/Evaluate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,22 @@ public void DictionaryList()
var conditions = JsonConvert.DeserializeObject<ConditionRuleSet>("{\"operator\":\"in\",\"field\":\"Test\"}");
conditions.Value = new List<string>()
{
"1",
"2"
"1"
};
bool result = JsonRuleEngine.Evaluate(
dict,
conditions
);

conditions = JsonConvert.DeserializeObject<ConditionRuleSet>("{\"operator\":\"in\",\"field\":\"Test\"}");
conditions.Value = new JArray
{
"2"
};
result = JsonRuleEngine.Evaluate(
dict,
conditions
);
Assert.True(result); // Return true
}

Expand Down
31 changes: 30 additions & 1 deletion JsonRuleEngine.Net/Engine/JsonRuleEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,38 @@ private static Type GetDictionaryType(object value, ConditionRuleOperator op)
}
}

if (value is JArray)
return typeof(IEnumerable<>).MakeGenericType(((JArray)value).GetJArrayType());

return value.GetType();
}

private static Type GetJArrayType(this JArray array)
{
var firstValue = array.FirstOrDefault();
if (firstValue != null)
{
switch (firstValue.Type)
{
case JTokenType.String:
return typeof(string);
case JTokenType.Float:
return typeof(double);
case JTokenType.Object:
return typeof(object);
case JTokenType.Date:
return typeof(DateTime);
case JTokenType.Boolean:
return typeof(Boolean);
case JTokenType.Integer:
return typeof(int);
case JTokenType.Array:
return typeof(JArray);
}
}
return null;
}


/// <summary>
/// Handle table rule
Expand All @@ -453,7 +482,7 @@ private static Expression HandleTableRule(Expression array, Expression param, Co
{

var currentField = remainingFields.First();
var childType = array.Type == typeof(JArray) ? ((JArray)value).FirstOrDefault()?.GetType() : array.Type.GetGenericArguments().First();
var childType = array.Type == typeof(JArray) ? ((JArray)value).GetJArrayType() : array.Type.GetGenericArguments().First();

// Contains methods
// Need a conversion to an array of string
Expand Down

0 comments on commit 39ee2f4

Please sign in to comment.