[CSHARP-4126] Replace parameter names that aren't valid server variable names with generated variable names Created: 04/Apr/22  Updated: 28/Oct/23  Resolved: 15/Apr/22

Status: Closed
Project: C# Driver
Component/s: LINQ3
Affects Version/s: None
Fix Version/s: 2.15.1

Type: Bug Priority: Unknown
Reporter: John Youngers Assignee: Robert Stam
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Summary

When provided an expression that was generated by a tool (such as `Mapster`), the driver appears to alias unnamed parameters with underscores, which results in a mongodb query exception such as `Command aggregate failed: '_p1' starts with an invalid character for a user variable name.`

Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

Driver 2.15.0

How to Reproduce

Manually creating an Expression against a `IMongoCollection.AsQueryable()` should result in the error.

Additional Background

General usage works (normally `Select(i => new Child { Id = i.Id})` defined lambdas):

 

.Select(Param_0 => new ItemFacade() {
	Id = Param_0.Id.ToString()
	Children = Param_0.Children.Select(i => new ChildFacade() {ChildId = i.ChildId})
 
{ "$map" : { "input" : "$Children", "as" : "i", "in" : { "ChildId" : "$$i.ChildId" } } }

 

But if the expression was generated by a tool that doesn't name parameters, it results in that mongodb error:

 

.Select(Param_0 => new ItemFacade() {
	Id = Param_0.Id.ToString(), 
	Children = Param_0.Children.Select(Param_1 => new ChildFacade() {ChildId = Param_1.ChildId})
})
 
{ "$map" : { "input" : "$Children", "as" : "_p1", "in" : { "ChildId" : "$$_p1.ChildId" } } }

 



 Comments   
Comment by Githook User [ 04/May/22 ]

Author:

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

Message: CSHARP-4126: Replace parameter names that aren't valid server variable names with generated variable names.
Branch: v2.15.x
https://github.com/mongodb/mongo-csharp-driver/commit/389ae9a9b3ad3ed2a080580d2a85534d4baee158

Comment by John Youngers [ 22/Apr/22 ]

Perfect; thanks Robert! Do you know if there's currently an ETA for when the 2.15.1 version would be published?

Comment by Githook User [ 15/Apr/22 ]

Author:

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

Message: CSHARP-4126: Replace parameter names that aren't valid server variable names with generated variable names.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/88769e2bd8866abd40c274552e38127400a0e731

Comment by Robert Stam [ 05/Apr/22 ]

Thank you for reporting this. We use the parameter name as the name of a corresponding server variable name, which fails when the parameter name is not a valid server variable name.

I expect we can work around this without too much difficulty.

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