[CSHARP-2979] Builders<T>.Filter.And(filters) does not handle null values in filters Created: 26/Feb/20  Updated: 31/Mar/22

Status: Backlog
Project: C# Driver
Component/s: Builders
Affects Version/s: 2.10.0
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Lealand Vettleson Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

asp net core 3.1


Backwards Compatibility: Minor Change

 Description   

Our team has been working on building a collection of filter definitions to query against and we noticed in our tests that when adding a null value to the collection, we end up getting a NullReferenceException.

It appears that, while the collection is being Ensured that it's not null, the entities get no validation pass and are assumed to be correct.

public AndFilterDefinition(IEnumerable<FilterDefinition<TDocument>> filters)
{{ {}}
{{     _filters = Ensure.IsNotNull(filters, nameof(filters)).ToList();}}
{{ }}}

(https://github.com/mongodb/mongo-csharp-driver/blob/master/src/MongoDB.Driver/FilterDefinitionBuilder.cs#L1500)

If we look at the Render a little further down in the FilterDefinitionBuilder.cs, we see _filters iterated over:

foreach (var filter in _filters)
{{ {}}
{{ var renderedFilter = filter.Render(documentSerializer, serializerRegistry);}}

If _filters contains a null, this will trigger a NullReferenceException.



 Comments   
Comment by Lealand Vettleson [ 26/Feb/20 ]

The workaround here is to just make sure no nulls go into our filter collection before we add them to the builder.

Generated at Wed Feb 07 21:44:02 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.