I have a custom string-serializer for an enum of type ushort. I also have a class with a property of that enum type. When I query the database using a simple linq expression then the custom serializer is ignored. If I remove the explicit ushort declaration of the enum then the custom serializer is invoked.
This is a LINQ3 problem, LINQ2 did not ignore the serializer.
Below is an example of a test that I ran on the latest version of the driver
[Fact]
public void TestCustomSerializer()
{
BsonSerializer.RegisterSerializer(typeof(MyEnum), new MyEnumTypeSerializer());
var client = DriverTestConfiguration.Client;
var db = client.GetDatabase("TestDb");
db.DropCollection("TestA");
var collection = db.GetCollection<TestA>("TestA");
var testA = new TestA
{
EnumProperty = MyEnum.A
};
collection.InsertOne(testA);
var result = collection.AsQueryable().FirstOrDefault(x => x.EnumProperty == testA.EnumProperty);
result.Should().NotBeNull();
}
public class TestA
{
[BsonId]
public ObjectId MyId { get; set; }
public MyEnum EnumProperty { get; set; }
}
public enum MyEnum : ushort // remove the explicit type and it will work
{
A = 1
}
public class MyEnumTypeSerializer : StructSerializerBase<MyEnum>
{
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, MyEnum value)
{
BsonSerializer.Serialize(context.Writer, value.ToString());
}
public override MyEnum Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
{
var measurementValueType = (MyEnum)Enum.Parse(args.NominalType, BsonSerializer.Deserialize<string>(context.Reader));
return measurementValueType;
}
}
When debugging I end up in ConvertExpressionToFilterFieldTranslator.Translate. Here the type of the expression seems to be int and the enum underlying type is ushort which takes us in a different path in ConvertExpressionToFilterFieldTranslator.Translate
Is there a way around this?
I cannot change the enum to be an int, nor modify my TestA class!!