[CSHARP-3449] custom serializers sometimes not used in aggregation Created: 04/Mar/21 Updated: 28/Oct/23 Resolved: 17/Feb/22 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | Linq, LINQ3 |
| Affects Version/s: | 2.11.3 |
| Fix Version/s: | 2.14.0 |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | Felix König | Assignee: | Dmitry Lukyanov (Inactive) |
| Resolution: | Fixed | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Attachments: |
|
||||||||
| Issue Links: |
|
||||||||
| Epic Link: | CSHARP-3615 | ||||||||
| Description |
|
I am encountering a problem where custom serializers I have previously registered are not being used to convert types I use in an aggregation pipeline. Assume I have these types:
where TestEnum will be my custom type. I am registering the type as follows:
TestEnumSerializer is a SerializerBase<TestEnum> that represents the enum values as the strings "my_foo", "my_bar" and "my_baz". I am inserting some entities as follows:
and I can confirm that the entities get persisted as I expected:
If I now perform the following aggregation:
I can see that the produced query looks as follows:
Note that it compares the $enum_value to 1 instead of "my_bar", which makes the match fail and return a wrong result.
The 1 is the enum's ordinal value, which probably gets used because of the implicit conversion added in https://jira.mongodb.org/browse/CSHARP-131 Since it appears to be using BsonType mappers instead of registered serializers, it is possible to add custom type mappers for types that do not have an implicit conversion using BsonTypeMapper.RegisterCustomTypeMapper. However, since the enum gets implicitly converted to an ordinal value before any custom type mappers are checked, I was unable to find a workaround. I have added the full source code of the test class I used as an attachment. |
| Comments |
| Comment by Felix König [ 19/Feb/22 ] | |||||||||||||||||||||||||||||||||||||||||||||||||
|
Thank you! I tried using the new LINQ3 driver and have opened a follow-up issue | |||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by James Kovacs [ 17/Feb/22 ] | |||||||||||||||||||||||||||||||||||||||||||||||||
|
This issue has been fixed in the new LINQ provider (known as LINQ3), which is included in the 2.14 release. Configure your MongoClientSettings to use LinqProvider.V3 if you want to use this functionality. To configure a client to use the LINQ3 provider use code like the following
The following is a self-contained repro:
This produces the output using the configured TestEnumSerializer. Note that due to
| |||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Felix König [ 09/Mar/21 ] | |||||||||||||||||||||||||||||||||||||||||||||||||
|
Thank you very much. That workaround, in addition to providing custom BSON mappers using BsonTypeMapper.RegisterCustomTypeMapper for some other types I use, is working nicely. | |||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Dmitry Lukyanov (Inactive) [ 08/Mar/21 ] | |||||||||||||||||||||||||||||||||||||||||||||||||
|
Hello de.felix.koenig@gmail.com , thanks for your report. Currently, we're actively working on the new LINQ implementation and will address this case there. As a workaround, currently, you can use this code with manual mapping "TestEnum.Bar" to the expected mapped value:
| |||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Felix König [ 04/Mar/21 ] | |||||||||||||||||||||||||||||||||||||||||||||||||
|
Note that registering the serializer globally like this instead of just for the entity's property causes no change in behaviour:
|