[CSHARP-4748] ContainsKey throws an exception when key is not a string but is serialized as a string Created: 09/Aug/23  Updated: 28/Oct/23  Resolved: 17/Aug/23

Status: Closed
Project: C# Driver
Component/s: LINQ3
Affects Version/s: 2.20.0
Fix Version/s: 2.22.0

Type: Bug Priority: Unknown
Reporter: Brecht Vanhaesebrouck Assignee: Robert Stam
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible
Documentation Changes: Not Needed
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

Summary

Since changing the version of the LINQ driver to v3 we receive this error, in v2 everything was working fine. The main problem seems to be that the key of the dictionary is a enum (which is mapped to a string with a RegisterClassMap).

How to Reproduce

Query: 

Collection.AsQueryable<Translation>.Where(x => x.Languages.ContainsKey(language)).ToListAsync(token);

 

Mapping:

 

          BsonClassMap.RegisterClassMap<Translation>(doc =>
            {
                doc.AutoMap();
                doc.SetIgnoreExtraElements(true);
                doc.MapIdMember(a => a.Id)
                    .SetIdGenerator(new StringObjectIdGenerator())
                    .SetSerializer(new StringSerializer(BsonType.ObjectId));
                doc.MapMember(a => a.Languages)
                    .SetSerializer(new DictionaryInterfaceImplementerSerializer<Dictionary<Languages, string>>(
                        DictionaryRepresentation.Document, new EnumSerializer<Languages>(BsonType.String), new StringSerializer()
                    ));
            });

 

 



 Comments   
Comment by Githook User [ 17/Aug/23 ]

Author:

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

Message: CSHARP-4748: ContainsKey throws an exception when key is not a string but is serialized as a string.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/c3e7ae32f70d65a22ff76767d75ab67d52ef5013

Comment by Robert Stam [ 09/Aug/23 ]

Thank you for reporting this. I am able to reproduce it and am investigating how to address this.

Comment by Brecht Vanhaesebrouck [ 09/Aug/23 ]

Translation class:

 

 

    public class Translation 
    {
        public String Id { get; set; }
        public String Key { get; set; }
        public Dictionary<Languages, string> Languages { get; set; } = new();
        public String Service { get; set; }
        public String ExtraContext { get; set; }
    }
 

 

Languages enum:

 

  public enum Languages
  {
    en,
    nl,
    fr,
    de,
    pl,
  }

Comment by PM Bot [ 09/Aug/23 ]

Hi brecht.vanhaesebrouck@sea-invest.com, thank you for reporting this issue! The team will look into it and get back to you soon.

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