Skip to content

Commit

Permalink
CSHARP-4731: Support assigning List<T> value to IList<T> property in …
Browse files Browse the repository at this point in the history
…Select.
  • Loading branch information
rstam committed Aug 3, 2023
1 parent 10b0a25 commit 0349851
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using MongoDB.Bson.Serialization;
using MongoDB.Driver.Linq.Linq3Implementation.Ast;
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions;
using MongoDB.Driver.Linq.Linq3Implementation.Misc;

namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators
{
Expand Down Expand Up @@ -57,7 +58,8 @@ public static AggregationExpression Translate(TranslationContext context, Member
var constructorArgumentSerializer = constructorArgumentTranslation.Serializer ?? BsonSerializer.LookupSerializer(constructorArgumentType);
var memberMap = EnsureMemberMap(expression, classMap, creatorMapParameter);
EnsureDefaultValue(memberMap);
memberMap.SetSerializer(constructorArgumentSerializer);
var memberSerializer = CoerceSourceSerializerToMemberSerializer(memberMap, constructorArgumentSerializer);
memberMap.SetSerializer(memberSerializer);
computedFields.Add(AstExpression.ComputedField(memberMap.ElementName, constructorArgumentTranslation.Ast));
}
}
Expand All @@ -69,7 +71,8 @@ public static AggregationExpression Translate(TranslationContext context, Member
var memberMap = FindMemberMap(expression, classMap, member.Name);
var valueExpression = memberAssignment.Expression;
var valueTranslation = ExpressionToAggregationExpressionTranslator.Translate(context, valueExpression);
memberMap.SetSerializer(valueTranslation.Serializer);
var memberSerializer = CoerceSourceSerializerToMemberSerializer(memberMap, valueTranslation.Serializer);
memberMap.SetSerializer(memberSerializer);
computedFields.Add(AstExpression.ComputedField(memberMap.ElementName, valueTranslation.Ast));
}

Expand Down Expand Up @@ -107,6 +110,27 @@ private static BsonClassMap CreateClassMap(Type classType, ConstructorInfo const
return classMap;
}

private static IBsonSerializer CoerceSourceSerializerToMemberSerializer(BsonMemberMap memberMap, IBsonSerializer sourceSerializer)
{
var memberType = memberMap.MemberType;
var memberSerializer = memberMap.GetSerializer();
var sourceType = sourceSerializer.ValueType;

if (memberType != sourceType &&
memberType.ImplementsIEnumerable(out var memberItemType) &&
sourceType.ImplementsIEnumerable(out var sourceItemType) &&
sourceItemType == memberItemType &&
sourceSerializer is IBsonArraySerializer sourceArraySerializer &&
sourceArraySerializer.TryGetItemSerializationInfo(out var sourceItemSerializationInfo) &&
memberSerializer is IChildSerializerConfigurable memberChildSerializerConfigurable)
{
var sourceItemSerializer = sourceItemSerializationInfo.Serializer;
return memberChildSerializerConfigurable.WithChildSerializer(sourceItemSerializer);
}

return sourceSerializer;
}

private static BsonMemberMap EnsureMemberMap(Expression expression, BsonClassMap classMap, MemberInfo creatorMapParameter)
{
var declaringClassMap = classMap;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/* 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.Collections.Generic;
using System.Linq;
using FluentAssertions;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver.Linq;
using MongoDB.TestHelpers.XunitExtensions;
using Xunit;

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

var queryable = collection
.AsQueryable()
.Select(x => new P { IList = x.List })
.Where(x => x.IList.Contains(E.A));

if (linqProvider == LinqProvider.V2)
{
var exception = Record.Exception(() => Translate(collection, queryable));
exception.Should().BeOfType<ArgumentException>();
}
else
{
var stages = Translate(collection, queryable);
AssertStages(
stages,
"{ $project : { IList : '$List', _id : 0 } }",
"{ $match : { IList : 'A' } }");

var result = queryable.Single();
result.IList.Should().Equal(E.A, E.B);
}
}

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

var queryable = collection
.AsQueryable()
.Select(x => new Q { IReadOnlyList = x.List })
.Where(x => x.IReadOnlyList.Contains(E.A));

if (linqProvider == LinqProvider.V2)
{
var exception = Record.Exception(() => Translate(collection, queryable));
exception.Should().BeOfType<ArgumentException>();
}
else
{
var stages = Translate(collection, queryable);
AssertStages(
stages,
"{ $project : { IReadOnlyList : '$List', _id : 0 } }",
"{ $match : { IReadOnlyList : 'A' } }");

var result = queryable.Single();
result.IReadOnlyList.Should().Equal(E.A, E.B);
}
}

private IMongoCollection<Test> GetCollection(LinqProvider linqProvider)
{
var collection = GetCollection<Test>("test", linqProvider);
CreateCollection(
collection,
new Test { Id = 1, List = new List<E> { E.A, E.B } },
new Test { Id = 2, List = new List<E> { E.C, E.D } });
return collection;
}

private class Test
{
public int Id { get; set; }
[BsonRepresentation(BsonType.String)]
public List<E> List { get; set; }
}

private class P
{
public IList<E> IList { get; set; }
}

private class Q
{
public IReadOnlyList<E> IReadOnlyList { get; set; }
}

private enum E { A, B, C, D };
}
}

0 comments on commit 0349851

Please sign in to comment.