Skip to content

CSHARP-3435: FilterDefinition Inject method should use root serializer #1686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
* limitations under the License.
*/

using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions;
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Filters;
using MongoDB.Driver.Linq.Linq3Implementation.ExtensionMethods;
using MongoDB.Driver.Linq.Linq3Implementation.Misc;
Expand Down Expand Up @@ -44,12 +46,21 @@ public static AstFilter Translate(TranslationContext context, MethodCallExpressi
var filterExpression = arguments[0];
var filterDefinition = filterExpression.GetConstantValue<object>(expression);
var filterDefinitionType = filterDefinition.GetType(); // we KNOW it's a FilterDefinition<TDocument> because of the Inject method signature
var documentType = filterDefinitionType.GetGenericArguments()[0];
var filterDefinitionDocumentType = filterDefinitionType.GetGenericArguments()[0];

var rootSymbol = context.SymbolTable.Symbols.SingleOrDefault(s => s.Ast.IsRootVar());
if (rootSymbol == null)
{
throw new ExpressionNotSupportedException(expression, because: "there is no current root symbol");
}
var documentSerializer = rootSymbol.Serializer;
if (filterDefinitionDocumentType != documentSerializer.ValueType)
{
throw new ExpressionNotSupportedException(expression, because: $"FilterDefinition TDocument type: {filterDefinitionDocumentType} does not match document type {documentSerializer.ValueType} ");
}
var serializerRegistry = BsonSerializer.SerializerRegistry;
var documentSerializer = serializerRegistry.GetSerializer(documentType); // TODO: is this the right serializer?

var renderFilterMethod = __renderFilterMethodInfo.MakeGenericMethod(documentType);
var renderFilterMethod = __renderFilterMethodInfo.MakeGenericMethod(filterDefinitionDocumentType);
var renderedFilter = (BsonDocument)renderFilterMethod.Invoke(null, new[] { filterDefinition, documentSerializer, serializerRegistry, context.TranslationOptions });

return AstFilter.Raw(renderedFilter);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/* 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 Xunit;

namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira;

public class CSharp3435Tests : LinqIntegrationTest<CSharp3435Tests.ClassFixture>
{
public CSharp3435Tests(ClassFixture fixture)
: base(fixture)
{
}

[Fact]
public void Where_should_work()
{
var queryable = CreateQueryable()
.Where(x => x.NormalizedUsername == "PAPLABROS");

var stages = Translate(Fixture.UserClaimCollection, queryable);
AssertStages(
stages,
"{ $project : { _outer : '$$ROOT', _id : 0 } }",
"{ $lookup : { from : 'Users', localField : '_outer.UserId', foreignField : '_id', as : '_inner' } }",
"{ $project : { claim : '$_outer', users : '$_inner', _id : 0 } }",
"{ $match : { 'claim.ClaimType' : 'Moderator' } }",
"{ $project : { _v : { $arrayElemAt : ['$users', 0] }, _id : 0 } }",
"{ $match : { '_v.NormalizedUsername' : 'PAPLABROS' } }");
}

[Fact]
public void Where_with_Inject_should_work()
{
var filter = Builders<User>.Filter.Eq(x => x.NormalizedUsername, "PAPLABROS");
var queryable = CreateQueryable()
.Where(x => filter.Inject());

var stages = Translate(Fixture.UserClaimCollection, queryable);
AssertStages(
stages,
"{ $project : { _outer : '$$ROOT', _id : 0 } }",
"{ $lookup : { from : 'Users', localField : '_outer.UserId', foreignField : '_id', as : '_inner' } }",
"{ $project : { claim : '$_outer', users : '$_inner', _id : 0 } }",
"{ $match : { 'claim.ClaimType' : 'Moderator' } }",
"{ $project : { _v : { $arrayElemAt : ['$users', 0] }, _id : 0 } }",
"{ $match : { '_v.NormalizedUsername' : 'PAPLABROS' } }");
}

public IQueryable<User> CreateQueryable()
{
var usersCollection = Fixture.UserCollection;
var userClaimsCollection = Fixture.UserClaimCollection;

var queryable =
from claim in userClaimsCollection.AsQueryable()
join user in usersCollection.AsQueryable() on claim.UserId equals user.Id into users
where claim.ClaimType == "Moderator"
select users.First();

// this is the equivalent method syntax
// var queryable = userClaimsCollection.AsQueryable()
// .GroupJoin(
// usersCollection.AsQueryable(),
// claim => claim.UserId,
// user => user.Id,
// (claim, users) => new { claim, users })
// .Where(x => x.claim.ClaimType == "Moderator")
// .Select(x => x.users.First());

return queryable;
}

public class User
{
public int Id { get; set; }
public string NormalizedUsername { get; set; }
}

public class UserClaim
{
public int Id { get; set; }
public int UserId { get; set; }
public string ClaimType { get; set; }
}

public sealed class ClassFixture : MongoDatabaseFixture
{
public IMongoCollection<User> UserCollection { get; private set; }
public IMongoCollection<UserClaim> UserClaimCollection { get; private set; }

protected override void InitializeFixture()
{
UserCollection = CreateCollection<User>("Users");
UserClaimCollection = CreateCollection<UserClaim>("UserClaims");
}
}
}