Skip to content

Commit 41604c9

Browse files
rstamDmitryLukyanov
authored andcommitted
CSHARP-4538: WrappedValueSerializer should handle empty documents.
1 parent 1369f83 commit 41604c9

File tree

3 files changed

+101
-2
lines changed

3 files changed

+101
-2
lines changed

src/MongoDB.Driver/Linq/Linq3Implementation/Serializers/WrappedValueSerializer.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
using System;
17+
using MongoDB.Bson;
1718
using MongoDB.Bson.IO;
1819
using MongoDB.Bson.Serialization;
1920
using MongoDB.Bson.Serialization.Serializers;
@@ -50,10 +51,18 @@ public WrappedValueSerializer(string fieldName, IBsonSerializer<TValue> valueSer
5051
// public methods
5152
public override TValue Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
5253
{
54+
TValue value;
5355
var reader = context.Reader;
5456
reader.ReadStartDocument();
55-
reader.ReadName(_fieldName);
56-
var value = _valueSerializer.Deserialize(context);
57+
if (reader.ReadBsonType() == BsonType.EndOfDocument)
58+
{
59+
value = default;
60+
}
61+
else
62+
{
63+
reader.ReadName(_fieldName);
64+
value = _valueSerializer.Deserialize(context);
65+
}
5766
reader.ReadEndDocument();
5867
return value;
5968
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 System.Linq;
17+
using FluentAssertions;
18+
using MongoDB.Bson;
19+
using Xunit;
20+
21+
namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira
22+
{
23+
public class CSharp4538Tests : Linq3IntegrationTest
24+
{
25+
[Fact]
26+
public void Project_with_nullable_value_should_work()
27+
{
28+
var collection = CreateCollection();
29+
30+
var find = collection
31+
.Find(_ => true)
32+
.Project(x => x.D);
33+
34+
var projection = TranslateFindProjection(collection, find);
35+
projection.Should().Be("{ _v : '$D', _id : 0 }");
36+
37+
var results = find.ToList();
38+
results.Should().Equal(1.0, null, null);
39+
}
40+
41+
[Fact]
42+
public void Project_with_anonymous_class_with_nullable_value_should_work()
43+
{
44+
var collection = CreateCollection();
45+
46+
var find = collection
47+
.Find(_ => true)
48+
.Project(x => new { V = x.D });
49+
50+
var projection = TranslateFindProjection(collection, find);
51+
projection.Should().Be("{ V : '$D', _id : 0 }");
52+
53+
var results = find.ToList();
54+
results.Select(x => x.V).Should().Equal(1.0, null, null);
55+
}
56+
57+
private IMongoCollection<C> CreateCollection()
58+
{
59+
var collection = GetCollection<C>("C");
60+
61+
var bsonDocumentCollection = collection.Database.GetCollection<BsonDocument>(collection.CollectionNamespace.CollectionName);
62+
CreateCollection(
63+
bsonDocumentCollection,
64+
new BsonDocument { { "_id", 1 }, { "D", 1.0 } },
65+
new BsonDocument { { "_id", 2 }, { "D", BsonNull.Value } },
66+
new BsonDocument { { "_id", 3 } });
67+
68+
return collection;
69+
}
70+
71+
private class C
72+
{
73+
public int Id { get; set; }
74+
public double? D { get; set; }
75+
}
76+
}
77+
}

tests/MongoDB.Driver.Tests/Linq/Linq3ImplementationTests/Linq3IntegrationTest.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,5 +159,18 @@ protected BsonDocument Translate<TDocument>(IMongoCollection<TDocument> collecti
159159
var linqProvider = collection.Database.Client.Settings.LinqProvider;
160160
return filterDefinition.Render(documentSerializer, serializerRegistry, linqProvider);
161161
}
162+
163+
protected BsonDocument TranslateFindProjection<TDocument, TProjection>(
164+
IMongoCollection<TDocument> collection,
165+
IFindFluent<TDocument, TProjection> find,
166+
LinqProvider linqProvider = LinqProvider.V3)
167+
{
168+
var findOptions = ((FindFluent<TDocument, TProjection>)find).Options;
169+
var projection = findOptions.Projection;
170+
var documentSerializer = collection.DocumentSerializer;
171+
var serializerRegistry = BsonSerializer.SerializerRegistry;
172+
var renderedProjection = projection.Render(documentSerializer, serializerRegistry, linqProvider);
173+
return renderedProjection.Document;
174+
}
162175
}
163176
}

0 commit comments

Comments
 (0)