Skip to content

Commit

Permalink
CSHARP-4658: NullReferenceException when using certain projections.
Browse files Browse the repository at this point in the history
  • Loading branch information
rstam committed May 24, 2023
1 parent 513d0ce commit 7746b0f
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,21 +190,17 @@ public override Expression Visit(Expression expression)
protected override Expression VisitMemberInit(MemberInitExpression node)
{
// Bindings must be visited before NewExpression
foreach (var binding in node.Bindings)
{
switch (binding.BindingType)
{
case MemberBindingType.Assignment:
var memberAssignment = (MemberAssignment)binding;
base.Visit(memberAssignment.Expression);
break;
Visit(node.Bindings, VisitMemberBinding);

default:
throw new InvalidOperationException($"Unexpected binding type: {binding.BindingType}.");
}
if (_cannotBeEvaluated)
{
// visit only the arguments if any MemberBindings cannot be partially evaluated
Visit(node.NewExpression.Arguments);
}
else
{
Visit(node.NewExpression);
}

base.Visit(node.NewExpression);

return node;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System.Linq;
using MongoDB.Driver.Linq;
using MongoDB.TestHelpers.XunitExtensions;
using Xunit;

namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira
{
public class CSharp4658Tests : Linq3IntegrationTest
{
[Theory]
[ParameterAttributeData]
public void Select_new_Model_should_work(
[Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider)
{
var collection = GetCollection(linqProvider);

var queryable = collection.AsQueryable()
.GroupBy(x => x.Name)
.Select(x =>
new Model
{
NotId = string.Empty,
Name = x.Key
});

var stages = Translate(collection, queryable);
AssertStages(
stages,
"{ $group : { _id : '$Name' } }",
"{ $project : { NotId : '', Name : '$_id', _id : 0 } }");
}

[Theory]
[ParameterAttributeData]
public void Project_new_ModelAggregated_should_work(
[Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider)
{
var collection = GetCollection(linqProvider);

var aggregate = collection.Aggregate()
.Group(
x => "",
g => new { Count = g.Count()}
)
.Project(
x => new ModelAggregated
{
NotId = string.Empty,
Count = x.Count
});

var stages = Translate(collection, aggregate);
if (linqProvider == LinqProvider.V2)
{
AssertStages(
stages,
"{ $group : { _id : '', Count : { $sum : 1 } } }",
"{ $project : { NotId : '', Count : '$Count', _id : 0 } }");
}
else
{
AssertStages(
stages,
"{ $group : { _id : '', __agg0 : { $sum : 1 } } }",
"{ $project : { Count : '$__agg0', _id : 0 } }",
"{ $project : { NotId : '', Count : 1, _id : 0 } }");
}
}

private IMongoCollection<Model> GetCollection(LinqProvider linqProvider)
{
var collection = GetCollection<Model>("test", linqProvider);
return collection;
}

private class Model
{
public string NotId { get; set; }
public string Name { get; set; }
}

private class ModelAggregated
{
public string NotId { get; set; }
public int Count { get; set; }
}
}
}

0 comments on commit 7746b0f

Please sign in to comment.