Skip to content

Commit

Permalink
Fix non existant element access for arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
nikoraes committed Nov 5, 2024
1 parent c71b977 commit c82ad6f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
2 changes: 2 additions & 0 deletions JexlNet.Test/Evaluator/Evaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ public async void EvaluateExpression_Conditional(string input, decimal expected)
[Theory]
[InlineData(@"""foo"" ?: ""bar""", "foo")]
[InlineData(@""""" ?: ""bar""", "bar")]
[InlineData(@"{foo:'bar'}.baz ?: 'tek'", "tek")]
[InlineData(@"{foo:[]}.foo.bar ?: 'tek'", "tek")]
public async void EvaluateExpression_AllowsMissingConsequentInTernary(string input, string expected)
{
Evaluator _evaluator = new(new Grammar());
Expand Down
3 changes: 2 additions & 1 deletion JexlNet/Evaluator/Handlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public static async Task<JsonNode> IdentifierAsync(Evaluator evaluator, Node nod
nodeValue = node.Value.GetValue<string>();
if (fromResult is JsonArray list)
{
if (list.First() is JsonObject dict && !string.IsNullOrEmpty(nodeValue))
if (list.Count > 0 && list.First() is JsonObject dict && !string.IsNullOrEmpty(nodeValue))
{
// NOTE: we could also return a mapped list, like in JSONata, but then it won't align to the JS JEXL implementation
/* JsonArray result = new();
Expand All @@ -174,6 +174,7 @@ public static async Task<JsonNode> IdentifierAsync(Evaluator evaluator, Node nod
{
return list[index];
}
else return null;
}
else if (fromResult is JsonObject dict && !string.IsNullOrEmpty(nodeValue))
{
Expand Down

0 comments on commit c82ad6f

Please sign in to comment.