Skip to content

Commit 0aa5f62

Browse files
committed
CSHARP-5403: Fix queries and projection on GridFSFileInfo Id property.
1 parent b0f02ee commit 0aa5f62

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

src/MongoDB.Driver/GridFS/GridFSFileInfoSerializerCompat.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public GridFSFileInfoSerializer()
4343
RegisterMember("ChunkSizeBytes", "chunkSize", new Int32Serializer());
4444
RegisterMember("ContentType", "contentType", new StringSerializer());
4545
RegisterMember("Filename", "filename", new StringSerializer());
46+
RegisterMember("Id", "_id", ObjectIdSerializer.Instance);
4647
RegisterMember("IdAsBsonValue", "_id", BsonValueSerializer.Instance);
4748
RegisterMember("Length", "length", new Int64Serializer());
4849
RegisterMember("Metadata", "metadata", BsonDocumentSerializer.Instance);
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/* Copyright 2010-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using FluentAssertions;
17+
using MongoDB.Bson;
18+
using MongoDB.Driver.GridFS;
19+
using Xunit;
20+
21+
namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira
22+
{
23+
public class CSharp5403Tests : Linq3IntegrationTest
24+
{
25+
[Fact]
26+
public void Project_Id_should_work()
27+
{
28+
var collection = GetCollection();
29+
30+
var find = collection
31+
.Find(Builders<GridFSFileInfo>.Filter.Where(x => x.Id == ObjectId.Parse("111111111111111111111111")))
32+
.Project(x => x.Id);
33+
34+
var filter = TranslateFindFilter(collection, find);
35+
filter.Should().Be("{ _id : { $oid : '111111111111111111111111' } }");
36+
37+
var projection = TranslateFindProjection(collection, find);
38+
projection.Should().Be("{ _id : 1 }");
39+
40+
var result = find.Single();
41+
result.Should().Be(ObjectId.Parse("111111111111111111111111"));
42+
}
43+
44+
#pragma warning disable CS0618 // Type or member is obsolete
45+
[Fact]
46+
public void Project_IdAsBsonValue_should_work()
47+
{
48+
var collection = GetCollection();
49+
50+
var find = collection
51+
.Find(Builders<GridFSFileInfo>.Filter.Where(x => x.IdAsBsonValue == new BsonObjectId(ObjectId.Parse("111111111111111111111111"))))
52+
.Project(x => x.IdAsBsonValue);
53+
54+
var filter = TranslateFindFilter(collection, find);
55+
filter.Should().Be("{ _id : { $oid : '111111111111111111111111' } }");
56+
57+
var projection = TranslateFindProjection(collection, find);
58+
projection.Should().Be("{ _id : 1 }");
59+
60+
var result = find.Single();
61+
result.Should().Be(ObjectId.Parse("111111111111111111111111"));
62+
}
63+
#pragma warning restore CS0618 // Type or member is obsolete
64+
65+
private IMongoCollection<GridFSFileInfo> GetCollection()
66+
{
67+
var collection = GetCollection<GridFSFileInfo>("test");
68+
CreateCollection(
69+
collection.Database.GetCollection<BsonDocument>(collection.CollectionNamespace.CollectionName),
70+
BsonDocument.Parse("{ _id : { $oid : '111111111111111111111111' }, filename : 'One' }"),
71+
BsonDocument.Parse("{ _id : { $oid : '222222222222222222222222' }, filename : 'Two' }"));
72+
return collection;
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)