From 1dc2ece826c4cd1959cdedbfbb47a7f08982ef46 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 2 Jul 2024 11:19:50 -0400 Subject: [PATCH] [BUG]: SelectExpressionFactory fails with empty string (#28) * Fix check for empty select expressions --------- Co-authored-by: Matt Edwards --- .../Expressions/SelectExpressionFactory.cs | 3 ++ .../Query/JsonPathFilterExpressionTests.cs | 38 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/Hyperbee.Json/Filters/Parser/Expressions/SelectExpressionFactory.cs b/src/Hyperbee.Json/Filters/Parser/Expressions/SelectExpressionFactory.cs index ac17d6d6..195545d5 100644 --- a/src/Hyperbee.Json/Filters/Parser/Expressions/SelectExpressionFactory.cs +++ b/src/Hyperbee.Json/Filters/Parser/Expressions/SelectExpressionFactory.cs @@ -16,6 +16,9 @@ static class ExpressionHelper public static Expression GetExpression( ReadOnlySpan item, FilterContext context ) { + if ( item.IsEmpty ) + return null; + if ( item[0] != '$' && item[0] != '@' ) return null; diff --git a/test/Hyperbee.Json.Tests/Query/JsonPathFilterExpressionTests.cs b/test/Hyperbee.Json.Tests/Query/JsonPathFilterExpressionTests.cs index 1ee3663a..f5d9de69 100644 --- a/test/Hyperbee.Json.Tests/Query/JsonPathFilterExpressionTests.cs +++ b/test/Hyperbee.Json.Tests/Query/JsonPathFilterExpressionTests.cs @@ -489,6 +489,44 @@ public void FilterExpressionWithEqualsArrayWithSingleQuotes( string query, Type _ = source.Select( query ).ToArray(); } + [DataTestMethod] + [DataRow( "$[?(@.a[?(@.price>10)])]", typeof( JsonDocument ) )] + [DataRow( "$[?(@.a[?(@.price>10)])]", typeof( JsonNode ) )] + [ExpectedException( typeof( NotSupportedException ) )] + public void FilterExpressionWithSubFilter( string query, Type sourceType ) + { + // consensus: NOT_SUPPORTED + + var json = + """ + [ + { + "a": [{"price": 1}, {"price": 3}] + }, + { + "a": [{"price": 11}] + }, + { + "a": [{"price": 8}, {"price": 12}, {"price": 3}] + }, + { + "a": [] + } + ] + """; + + var source = GetDocumentFromSource( sourceType, json ); + + var matches = source.Select( query ); + var expected = new[] + { + source.FromJsonPathPointer( "$[1]" ), + source.FromJsonPathPointer( "$[2]" ) + }; + + Assert.IsTrue( expected.SequenceEqual( matches ) ); + } + [DataTestMethod] [DataRow( "$[?((@.key<44)==false)]", typeof( JsonDocument ) )] [DataRow( "$[?((@.key<44)==false)]", typeof( JsonNode ) )]