Skip to content

Commit

Permalink
Merge pull request #57 from kendaleiv/fix-null-reference-exception
Browse files Browse the repository at this point in the history
Fix NullReferenceException when null value in string array filter
  • Loading branch information
khalidabuhakmeh authored Sep 25, 2018
2 parents 9fbdda2 + f2a3129 commit c379099
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Filter.Nest/FilterLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,14 @@ internal static List<Func<QueryContainerDescriptor<T>, QueryContainer>> Generate
throw new InvalidOperationException($"NEST filtering does not work on `Range<T>` 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 =>
Expand Down
20 changes: 20 additions & 0 deletions tests/Filter.Nest.Tests/QueryContainerDescriptorExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Car>(s => s.Index("vehicles").Query(
q => q.MatchAll() && q.Filter(new { Name = new string[] { null } })));
}
}

[Fact]
public void Nullable_boolean_omitted_returns_expected_results()
{
Expand Down

0 comments on commit c379099

Please sign in to comment.