[CSHARP-4261] LinqProvider.V3 breaks custom BsonSerializerAttribute Created: 19/Jul/22 Updated: 27/Oct/23 Resolved: 18/Oct/22 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | LINQ3, Serialization |
| Affects Version/s: | 2.17.0 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Unknown |
| Reporter: | Damith G | Assignee: | Robert Stam |
| Resolution: | Works as Designed | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Description |
|
i have the following custom BsonSerializerAttribute that is used to decorate string properties of entities. the serializer implementation simply stores the property value as ObjectId if it's a valid objectid or stores it as a string if not a valid objectid.
the following works with LinqProvider.V2 but does not work with LinqProvider.V3
i believe the underlying cause is that LINQ3 translate the filter as
while LINQ2 correctly translates the query to:
i've also tried implelemting IBsonDocumentSerializer on the serializer but it doesn't seem to make a difference. LINQ2 does however work without implementing the interface. any advise would be highly appreciated in order to migrate my existing code to LINQ3. thanks! |
| Comments |
| Comment by Robert Stam [ 18/Oct/22 ] | |||||||||||
|
LINQ3 assumes unless told otherwise that a string property is stored as a string in the database, and has special handling for string filters (for example sometimes using regular expressions). If a string field is NOT stored as a string in the database you can let the LINQ3 translator know that by implementing the `IRepresentationConfigurable` interface in your custom serializer:
https://github.com/rstam/mongo-csharp-driver/tree/csharp4261 | |||||||||||
| Comment by James Kovacs [ 12/Aug/22 ] | |||||||||||
|
Thank you for your patience as we investigated. We have been able to reproduce the problem with the provided code. Some additional investigation is required to determine how best to fix the issue. In the meantime you can work around the issue by implementing IRepresentationConfigurable<T> for your custom serializer to inform the serialization infrastructure that the database representation is different than the C# representation.
Alternatively you can annotate your Id property with [BsonRepresentation(BsonType.ObjectId)]. If you look at the built-in ObjectIdSerializer, it already performs much the same work as your custom one does.
Please let us know if you have any additional questions. Sincerely, | |||||||||||
| Comment by Dmitry Lukyanov (Inactive) [ 19/Jul/22 ] | |||||||||||
|
Hey djnitehawk83@gmail.com, thanks for your report, we will look at it and come back to you. |