Skip to content

Commit d45b3a7

Browse files
committed
CSHARP-779: fixed issue with BsonRepresentation on arrays no propogating to items when used in the builders.
1 parent 182b592 commit d45b3a7

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

MongoDB.Driver/Linq/Utils/BsonSerializationInfoHelper.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using System.Linq.Expressions;
2020
using MongoDB.Bson;
2121
using MongoDB.Bson.Serialization;
22+
using MongoDB.Bson.Serialization.Options;
2223

2324
namespace MongoDB.Driver.Linq.Utils
2425
{
@@ -59,6 +60,18 @@ public BsonSerializationInfo GetItemSerializationInfo(string methodName, BsonSer
5960
var itemSerializationInfo = arraySerializer.GetItemSerializationInfo();
6061
if (itemSerializationInfo != null)
6162
{
63+
IBsonSerializationOptions itemSerializationOptions = null;
64+
var arrayOptions = serializationInfo.SerializationOptions as ArraySerializationOptions;
65+
if (arrayOptions != null)
66+
{
67+
itemSerializationOptions = arrayOptions.ItemSerializationOptions;
68+
return new BsonSerializationInfo(
69+
itemSerializationInfo.ElementName,
70+
itemSerializationInfo.Serializer,
71+
itemSerializationInfo.NominalType,
72+
itemSerializationOptions);
73+
}
74+
6275
return itemSerializationInfo;
6376
}
6477
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/* Copyright 2010-2013 10gen 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;
17+
using System.Collections.Generic;
18+
using MongoDB.Bson;
19+
using MongoDB.Bson.Serialization.Attributes;
20+
using MongoDB.Driver.Builders;
21+
using NUnit.Framework;
22+
23+
namespace MongoDB.DriverUnitTests.Jira
24+
{
25+
[TestFixture]
26+
public class CSharp779
27+
{
28+
public class RepresentationExample
29+
{
30+
[BsonRepresentation(BsonType.String)]
31+
public Guid[] MultipleEntry { get; set; }
32+
33+
[BsonRepresentation(BsonType.String)]
34+
public Guid SingleEntry { get; set; }
35+
}
36+
37+
[Test]
38+
public void InRepresentationIssue()
39+
{
40+
var guid = Guid.NewGuid();
41+
42+
var singleUpdate = Query<RepresentationExample>
43+
.In(x => x.SingleEntry, new[] { guid });
44+
45+
Assert.That(singleUpdate.ToJson().Contains(guid.ToString()));
46+
Assert.That(!singleUpdate.ToJson().Contains("CSUUID"));
47+
48+
var multipleUpdate = Query<RepresentationExample>
49+
.In(x => x.MultipleEntry, new[] { guid });
50+
51+
Assert.That(multipleUpdate.ToJson(), Contains.Substring(guid.ToString()));
52+
Assert.That(!multipleUpdate.ToJson().Contains("CSUUID"));
53+
}
54+
55+
[Test]
56+
public void UpdateRepresentationIssue()
57+
{
58+
var guid = Guid.NewGuid();
59+
60+
var singleUpdate = Update<RepresentationExample>
61+
.Set(x => x.SingleEntry, guid);
62+
63+
Assert.That(singleUpdate.ToJson().Contains(guid.ToString()));
64+
Assert.That(!singleUpdate.ToJson().Contains("CSUUID"));
65+
66+
var multipleUpdate = Update<RepresentationExample>
67+
.PullAll(x => x.MultipleEntry, new[] { guid });
68+
69+
Assert.That(multipleUpdate.ToJson(), Contains.Substring(guid.ToString()));
70+
Assert.That(!multipleUpdate.ToJson().Contains("CSUUID"));
71+
}
72+
}
73+
}

MongoDB.DriverUnitTests/MongoDB.DriverUnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
<Compile Include="Jira\CSharp653Tests.cs" />
122122
<Compile Include="Jira\CSharp714Tests.cs" />
123123
<Compile Include="Jira\CSharp718Tests.cs" />
124+
<Compile Include="Jira\CSharp779Tests.cs" />
124125
<Compile Include="MongoClientSettingsTests.cs" />
125126
<Compile Include="ReadPreferenceTests.cs" />
126127
<Compile Include="MongoCollectionSettingsTests.cs" />

0 commit comments

Comments
 (0)