[CSHARP-4524] Handle projections using constructors for classes that use public fields instead of public properties Created: 13/Feb/23  Updated: 28/Oct/23  Resolved: 28/Feb/23

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 2.19.0
Fix Version/s: 2.19.1

Type: Bug Priority: Major - P3
Reporter: Alkis Tsapanidis Assignee: Robert Stam
Resolution: Fixed Votes: 0
Labels: Bug
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: Text File MongoProjectionRegression.cs    
Issue Links:
Duplicate
is duplicated by CSHARP-4542 LINQ3 support for projecting to a Val... Closed
Related
is related to CSHARP-4550 NewExpression and MemberInitExpressio... Closed
Backwards Compatibility: Fully Compatible
Documentation Changes: Not Needed
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

Summary

Projections invoking non-default constructors with parameters passed into them no longer work as of 2.19.0 with the LINQ3 provider unless the constructor's parameters match the names of the properties being pulled from the database. This used to work on LINQ3 all the way until 2.18.0, so it's a regression.

MongoProjectionRegression.cs

How to Reproduce

Connect the provided repro to a test Mongo instance and run. It works on 2.18.0 with LINQ3, but not on 2.19.0 with LINQ3.

As a control, it works on LINQ2 on both versions.



 Comments   
Comment by Githook User [ 24/Mar/23 ]

Author:

{'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}

Message: CSHARP-4524: Handle projections using constructors for classes that use public fields instead of public properties.
Branch: v2.19.x
https://github.com/mongodb/mongo-csharp-driver/commit/fa6ec4a822668236bea5f9da69ce0b37d8018a73

Comment by Githook User [ 28/Feb/23 ]

Author:

{'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}

Message: CSHARP-4524: Handle projections using constructors for classes that use public fields instead of public properties.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/7eb0efff77c7819ee2d34fa6ee647acc69213f4c

Comment by Robert Stam [ 14/Feb/23 ]

Thanks for reporting this issue.

Note that LINQ2 would sometimes split a projection into two parts: the first executed on the server and the second on the client. LINQ3 no longer does that because we consider that to have been a bad idea as it hides potential performance issues. If you want to execute part of your projection client side you should do so explicitly.

However, as of server version 4.4 the server now supports the same projections in Find as in the aggregate $project stage, so Find can still use many projections as long as they can be translated to a $project projection.

In your case the projection is simple enough that it could be translated to a $project projection, but the LINQ3 code that does that neglected to handle classes with public fields instead of public properties.

I have implemented a fix that allows your sample to run using LINQ3, as long as you are using server 4.4 or newer.

Comment by Alkis Tsapanidis [ 13/Feb/23 ]

The only workaround I have found is to project to some anonymous type and then construct my own type from it afterwards, which is not really acceptable either in terms of the boilerplate produced, nor performance (extra allocations every time).

Generated at Wed Feb 07 21:48:29 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.