[CSHARP-2113] Expression based queries don't work for properties on nested interface types Created: 27/Nov/17 Updated: 28/Oct/23 Resolved: 22/Nov/22 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | LINQ3 |
| Affects Version/s: | None |
| Fix Version/s: | 2.19.0 |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | Daniel Hegener | Assignee: | Robert Stam |
| Resolution: | Fixed | Votes: | 1 |
| Labels: | triaged | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Description |
|
When using the expression based query API against a property on a (nested) interface type the driver fails with an exception. The below code demonstrates the issue:
It all comes down to the fact that the nested property "Inner" will be automatically assigned a serializer of type DiscriminatedInterfaceSerializer which does not implement IBsonDocumentSerializer which again is fair enough since it shouldn't really know about the concretes mappings for properties on an unknown implementor of its covered interface... However, for querying purposes it would appear desirable to have that functionality in place. The problem starts when SerializationBinder.VisitMember(MemberExpression node) performs the following type check:
As a result of this check, in the above example, this will cause the filter expression to be translated into "{( {document}.I == 1)}" as opposed to "{({document}.*{I}* == 1)}" for the working case which again causes PredicateTranslator.GetFieldExpression(Expression expression) to not find a matching field and fail:
|
| Comments |
| Comment by Robert Stam [ 22/Nov/22 ] | |||||||||||||||||||||||||||||||||||||||||||
|
This issue has been fixed in the new LINQ provider (known as LINQ3), which was introduced 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
| |||||||||||||||||||||||||||||||||||||||||||
| Comment by Githook User [ 22/Nov/22 ] | |||||||||||||||||||||||||||||||||||||||||||
|
Author: {'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}Message: | |||||||||||||||||||||||||||||||||||||||||||
| Comment by James Kovacs [ 10/Feb/22 ] | |||||||||||||||||||||||||||||||||||||||||||
|
Repro'd with LINQ3:
Output is:
| |||||||||||||||||||||||||||||||||||||||||||
| Comment by Daniel Hegener [ 27/Nov/17 ] | |||||||||||||||||||||||||||||||||||||||||||
|
One "fix" could be similar to the following change (in SerializationBinder.VisitMember(MemberExpression node)) - this "fix", however, causes problems in other locations of the code (at least some other failing unit tests):
| |||||||||||||||||||||||||||||||||||||||||||
| Comment by Daniel Hegener [ 27/Nov/17 ] | |||||||||||||||||||||||||||||||||||||||||||
|
I'm sorry, I cannot edit my above issue anymore but the error I posted is a little bit misleading since it's for an older version of my test code. The real exception message for the test as posted above will not be {document}{Inner}.I is not supported.but {document} .I is not supported. |