From f2a312992107006557e81bd46d25d57812932282 Mon Sep 17 00:00:00 2001 From: Ken Dale Date: Tue, 25 Sep 2018 08:57:44 -0400 Subject: [PATCH] Fix NullReferenceException when null value in string array filter --- src/Filter.Nest/FilterLogic.cs | 6 ++++-- ...QueryContainerDescriptorExtensionsTests.cs | 20 +++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/Filter.Nest/FilterLogic.cs b/src/Filter.Nest/FilterLogic.cs index abcfa2a..7f11907 100644 --- a/src/Filter.Nest/FilterLogic.cs +++ b/src/Filter.Nest/FilterLogic.cs @@ -138,12 +138,14 @@ internal static List, QueryContainer>> Generate throw new InvalidOperationException($"NEST filtering does not work on `Range` where `T` is `{filterProperty.PropertyType.Name}`."); } } - else - if (typeof(IEnumerable).IsAssignableFrom(filterProperty.PropertyType) + else if (typeof(IEnumerable).IsAssignableFrom(filterProperty.PropertyType) && filterProperty.PropertyType != typeof(string)) { foreach (var item in (IEnumerable)filterPropertyValue) { + if (item == null) + continue; + if (aliasAttribute != null) { queries.Add(x => diff --git a/tests/Filter.Nest.Tests/QueryContainerDescriptorExtensionsTests.cs b/tests/Filter.Nest.Tests/QueryContainerDescriptorExtensionsTests.cs index 04248ce..2cc6090 100644 --- a/tests/Filter.Nest.Tests/QueryContainerDescriptorExtensionsTests.cs +++ b/tests/Filter.Nest.Tests/QueryContainerDescriptorExtensionsTests.cs @@ -99,6 +99,26 @@ public void Multiple_filter_properties_queried_as_collection_of_and_operators() } } + [Fact] + public void Null_string_array_item_does_not_throw_NullReferenceException() + { + using (var elasticsearch = new ElasticsearchInside.Elasticsearch()) + { + var elasticClient = new ElasticClient(new ConnectionSettings(elasticsearch.Url)); + + var camaro = new Car { Name = "Camaro", IsElectric = false }; + var volt = new Car { Name = "Volt", IsElectric = true }; + + elasticClient.Index(camaro, x => x.Index("vehicles")); + elasticClient.Index(volt, x => x.Index("vehicles")); + + elasticClient.Refresh("vehicles"); + + elasticClient.Search(s => s.Index("vehicles").Query( + q => q.MatchAll() && q.Filter(new { Name = new string[] { null } }))); + } + } + [Fact] public void Nullable_boolean_omitted_returns_expected_results() {