Skip to content

Commit

Permalink
CSHARP-4681: InvalidCastException when rendering projections (#1146)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanych-sun committed Aug 2, 2023
1 parent 26fa40a commit ff22548
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/MongoDB.Driver/IFindFluentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static class IFindFluentExtensions
Ensure.IsNotNull(find, nameof(find));
Ensure.IsNotNull(projection, nameof(projection));

return find.Project<TNewProjection>(new ExpressionProjectionDefinition<TDocument, TNewProjection>(projection, null));
return find.Project<TNewProjection>(new FindExpressionProjectionDefinition<TDocument, TNewProjection>(projection));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* 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 FluentAssertions;
using MongoDB.Bson.Serialization;
using MongoDB.Driver.Linq;
using MongoDB.TestHelpers.XunitExtensions;
using Xunit;

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

var fluentFind = collection.Find(a => a.Id == "1").Project(a => a.Id);

var documentSerializer = collection.DocumentSerializer;
var serializerRegistry = BsonSerializer.SerializerRegistry;
var renderedProjection = fluentFind.Options.Projection.Render(documentSerializer, serializerRegistry, linqProvider);

renderedProjection.Document.Should().Be("{ _id : 1 }");

var result = fluentFind.Single();
result.Should().Be("1");
}

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

private class A
{
public string Id { get; set; }
}
}
}

0 comments on commit ff22548

Please sign in to comment.