[CSHARP-1919] Filter builder Where method with nullable Enum value doesn't render correctly Created: 13/Feb/17  Updated: 01/May/17  Resolved: 14/Feb/17

Status: Closed
Project: C# Driver
Component/s: API, Linq
Affects Version/s: 2.4.2
Fix Version/s: 2.4.3

Type: Bug Priority: Major - P3
Reporter: Robert Stam Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
is duplicated by CSHARP-1932 Issue with nullable parameter Closed
Related
related to CSHARP-1885 Don't cast nullable types (int?, sbyt... Closed
Backwards Compatibility: Minor Change

 Description   

When a non-nullable enum field is compared to a nullable value the filter does not render correctly.

To reproduce use these types:

public enum E { A, B }
 
public class C
{
    public int Id { get; set; }
    [BsonRepresentation(BsonType.String)]
    public E E { get; set; }
}

And this code:

var builder = Builders<C>.Filter;
var value = (E?)E.A;
var filter = builder.Where(x => x.E == value);
var registry = BsonSerializer.SerializerRegistry;
var serializer = registry.GetSerializer<C>();
var rendered = filter.Render(serializer, registry).ToJson();

The filter renders as:

"{ \"E\" : 0 }"

but should have rendered as:

"{ \"E\" : \"A\" }"

because the underlying field serializer uses String representation.



 Comments   
Comment by Matthew O&#39;Connell [ 16/Feb/17 ]

Thanks for the fix. Please see also very similar issue with the same driver version - CSHARP-1922

Comment by Githook User [ 15/Feb/17 ]

Author:

{u'username': u'rstam', u'name': u'rstam', u'email': u'robert@robertstam.org'}

Message: CSHARP-1919: Added similar tests for nullable and non-nullable ints.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/7c55afa8e535099f1f4b645f7613cbee0d5b2765

Comment by Githook User [ 14/Feb/17 ]

Author:

{u'username': u'rstam', u'name': u'rstam', u'email': u'robert@robertstam.org'}

Message: CSHARP-1919: Render filters using underlying field serializer when non-nullable field is compared to nullable enum.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/61f70773b5e1ff0816fb8e3650be231817e00236

Comment by Robert Stam [ 13/Feb/17 ]

When this fix is implemented we need to verify that the more complex scenario reported by moc777 in a comment to CSHARP-1885 is also fixed.

I believe that scenario involves the same underlying cause.

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