Details
-
Bug
-
Resolution: Done
-
Major - P3
-
None
-
None
-
None
Description
Given the following example class:
public class Test
|
{
|
public Dictionary<TestEnum, double> TestField { get; set; } = new Dictionary<TestEnum, double>();
|
}
|
|
|
public enum TestEnum
|
{
|
A,
|
B
|
}
|
If you register an EnumRepresentationConvention using the following code and try to insert an instance of the class
var pack = new ConventionPack { new EnumRepresentationConvention(BsonType.String) };
|
ConventionRegistry.Register("EnumStringConvention", pack, t => true);
|
var client = new MongoClient("mongodb://localhost/");
|
var db = client.GetDatabase("test");
|
var col = db.GetCollection<Test>("test");
|
await col.InsertOneAsync(new Test { TestField = { { TestEnum.A, 25d } } });
|
The last line throws a BsonSerializationException with the message:
When using DictionaryRepresentation.Document key values must serialize as strings.
If I alter the code to include the following line anywhere before the Insert.
BsonSerializer.RegisterSerializer(new EnumSerializer<TestEnum>(BsonType.String));
|
The insert succeeds and the resulting doc in the database looks like this:
{
|
"_id" : ObjectId("5799282d945208a81ee8bfd2"),
|
"TestField" : {
|
"A" : 25
|
}
|
}
|
Attachments
Issue Links
- is duplicated by
-
CSHARP-2041 EnumRepresentationConvention does not apply to arrays
-
- Closed
-