Skip to content

Commit

Permalink
CSHARP-4957: Fix issue with new array expression.
Browse files Browse the repository at this point in the history
  • Loading branch information
rstam committed Jul 18, 2024
1 parent 46f39d5 commit 74fba13
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
* limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions;

namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators
Expand All @@ -24,14 +27,29 @@ internal static class NewArrayInitExpressionToAggregationExpressionTranslator
public static AggregationExpression Translate(TranslationContext context, NewArrayExpression expression)
{
var items = new List<AstExpression>();
IBsonSerializer itemSerializer = null;
foreach (var itemExpression in expression.Expressions)
{
var itemTranslation = ExpressionToAggregationExpressionTranslator.Translate(context, itemExpression);
items.Add(itemTranslation.Ast);
itemSerializer ??= itemTranslation.Serializer;

// make sure all items are serialized using the same serializer
if (!itemTranslation.Serializer.Equals(itemSerializer))
{
throw new ExpressionNotSupportedException(expression, because: "all items in the array must be serialized using the same serializer");
}
}

var ast = AstExpression.ComputedArray(items);
var serializer = context.KnownSerializersRegistry.GetSerializer(expression);
return new AggregationExpression(expression, ast, serializer);

var arrayType = expression.Type;
var itemType = arrayType.GetElementType();
itemSerializer ??= BsonSerializer.LookupSerializer(itemType); // if the array is empty itemSerializer will be null
var arraySerializerType = typeof(ArraySerializer<>).MakeGenericType(itemType);
var arraySerializer = (IBsonSerializer)Activator.CreateInstance(arraySerializerType, itemSerializer);

return new AggregationExpression(expression, ast, arraySerializer);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/* 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;
using System.Linq;
using FluentAssertions;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Driver.Linq;
using MongoDB.TestHelpers.XunitExtensions;
using Xunit;

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

var queryable = collection.AsQueryable()
.Select(x => (new int[] { }));

var stages = Translate(collection, queryable);
if (linqProvider == LinqProvider.V2)
{
AssertStages(stages, "{ $project : { __fld0 : [], _id : 0 } }");
}
else
{
AssertStages(stages, "{ $project : { _v : [], _id : 0 } }");

}

var results = queryable.ToArray();
results.Should().HaveCount(1);
results[0].Should().Equal();
}

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

var queryable = collection.AsQueryable()
.Select(x => (new[] { x.X }));

var stages = Translate(collection, queryable);
if (linqProvider == LinqProvider.V2)
{
AssertStages(stages, "{ $project : { __fld0 : ['$X'], _id : 0 } }");
}
else
{
AssertStages(stages, "{ $project : { _v : ['$X'], _id : 0 } }");

}

var results = queryable.ToArray();
results.Should().HaveCount(1);
results[0].Should().Equal(1);
}

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

var queryable = collection.AsQueryable()
.Select(x => (new[] { x.X, x.X + 1 }));

var stages = Translate(collection, queryable);
if (linqProvider == LinqProvider.V2)
{
AssertStages(stages, "{ $project : { __fld0 : ['$X', { $add : ['$X', 1] }], _id : 0 } }");
}
else
{
AssertStages(stages, "{ $project : { _v : ['$X', { $add : ['$X', 1] }], _id : 0 } }");

}

var results = queryable.ToArray();
results.Should().HaveCount(1);
results[0].Should().Equal(1, 2);
}

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

var queryable = collection.AsQueryable()
.Select(x => (new[] { x.X, x.Y }));

if (linqProvider == LinqProvider.V2)
{
var stages = Translate(collection, queryable);
AssertStages(stages, "{ $project : { __fld0 : ['$X', '$Y'], _id : 0 } }");

var exception = Record.Exception(() => queryable.ToArray());
exception.Should().BeOfType<FormatException>(); // LINQ2 doesn't fail until deserialization
}
else
{
var exception = Record.Exception(() => Translate(collection, queryable));
exception.Should().BeOfType<ExpressionNotSupportedException>();
exception.Message.Should().Contain("all items in the array must be serialized using the same serializer");
}
}

private IMongoCollection<C> GetCollection(LinqProvider linqProvider)
{
var collection = GetCollection<C>("test", linqProvider);
CreateCollection(
collection,
new C { Id = 1, X = 1, Y = 2 });
return collection;
}

private class C
{
public int Id { get; set; }
public int X { get; set; }
[BsonSerializer(typeof(YSerializer))] public int Y { get; set; }
}

private class YSerializer : StructSerializerBase<int>
{
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, int value)
{
var writer = context.Writer;
writer.WriteString($"<{value}>"); // not parsable by int.Parse
}
}
}
}

0 comments on commit 74fba13

Please sign in to comment.