Skip to content

Commit 61824fd

Browse files
committed
fix complex accumulator visitor bug
1 parent 2604f8a commit 61824fd

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Expressions/AstComplexAccumulatorExpression.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ namespace MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions
2323
internal sealed class AstComplexAccumulatorExpression : AstAccumulatorExpression
2424
{
2525
private readonly AstComplexAccumulatorOperator _operator;
26-
private readonly Dictionary<string, AstExpression> _args;
26+
private readonly IReadOnlyDictionary<string, AstExpression> _args;
2727

28-
public AstComplexAccumulatorExpression(AstComplexAccumulatorOperator @operator, Dictionary<string, AstExpression> args)
28+
public AstComplexAccumulatorExpression(AstComplexAccumulatorOperator @operator, IReadOnlyDictionary<string, AstExpression> args)
2929
{
3030
_operator = @operator;
3131
_args = Ensure.IsNotNull(args, nameof(args));
3232
}
3333

34-
public Dictionary<string, AstExpression> Args => _args;
34+
public IReadOnlyDictionary<string, AstExpression> Args => _args;
3535

3636
public override AstNodeType NodeType => AstNodeType.ComplexAccumulatorExpression;
3737

@@ -53,7 +53,7 @@ public override BsonValue Render()
5353
return new BsonDocument(_operator.Render(), document);
5454
}
5555

56-
public AstComplexAccumulatorExpression Update(Dictionary<string, AstExpression> args)
56+
public AstComplexAccumulatorExpression Update(IReadOnlyDictionary<string, AstExpression> args)
5757
{
5858
if (ReferenceEquals(args, _args))
5959
{

src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Visitors/AstNodeVisitor.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,26 @@ public virtual AstNode VisitComplexAccumulatorExpression(AstComplexAccumulatorEx
209209

210210
if (newArg != oldArg)
211211
{
212-
newArgs ??= new Dictionary<string, AstExpression>(node.Args);
212+
if (newArgs == null)
213+
{
214+
// First change detected - copy all processed entries
215+
newArgs = new Dictionary<string, AstExpression>();
216+
foreach (var processedKvp in node.Args)
217+
{
218+
if (processedKvp.Key == kvp.Key)
219+
{
220+
break; // Stop at current entry
221+
}
222+
newArgs[processedKvp.Key] = processedKvp.Value;
223+
}
224+
}
213225
newArgs[kvp.Key] = newArg;
214226
}
227+
else if (newArgs != null)
228+
{
229+
// We're building a new dictionary, so add unchanged entries too
230+
newArgs[kvp.Key] = oldArg;
231+
}
215232
}
216233

217234
return newArgs != null ? node.Update(newArgs) : node;

0 commit comments

Comments
 (0)