Skip to content

Commit f4b7fb1

Browse files
committed
CSHARP-1255: fixed issue with prefixed mapped fields interferring with other unmapped and ignored fields.
1 parent 94fc89f commit f4b7fb1

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/* Copyright 2010-2014 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.IO;
17+
using FluentAssertions;
18+
using MongoDB.Bson.IO;
19+
using NUnit.Framework;
20+
21+
namespace MongoDB.Bson.Tests.IO
22+
{
23+
public class TrieNameDecoderTests
24+
{
25+
[Test]
26+
public void Should_read_name_when_trie_does_not_know_about_the_name()
27+
{
28+
var trie = new BsonTrie<int>();
29+
trie.Add("known", 10);
30+
31+
Assert(trie, "different");
32+
}
33+
34+
[Test]
35+
public void Should_read_name_when_trie_holds_a_longer_version_of_the_name()
36+
{
37+
var trie = new BsonTrie<int>();
38+
trie.Add("longer", 10);
39+
40+
Assert(trie, "long");
41+
}
42+
43+
[Test]
44+
public void Should_read_name_when_trie_knows_about_the_name()
45+
{
46+
var trie = new BsonTrie<int>();
47+
trie.Add("known", 10);
48+
49+
Assert(trie, "known");
50+
}
51+
52+
private void Assert(BsonTrie<int> trie, string name)
53+
{
54+
var subject = new TrieNameDecoder<int>(trie);
55+
56+
using (var memoryStream = new MemoryStream())
57+
using (var bsonStream = new BsonStreamAdapter(memoryStream))
58+
{
59+
bsonStream.WriteCString(name);
60+
bsonStream.WriteInt32(20);
61+
bsonStream.Position = 0;
62+
63+
var result = subject.Decode(bsonStream, Utf8Encodings.Strict);
64+
65+
result.Should().Be(name);
66+
}
67+
}
68+
}
69+
}

src/MongoDB.Bson.Tests/MongoDB.Bson.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
<Compile Include="IO\MultiChunkBufferTests.cs" />
9898
<Compile Include="IO\OutputBufferChunkSourceTests.cs" />
9999
<Compile Include="IO\SingleChunkBufferTests.cs" />
100+
<Compile Include="IO\TrieNameDecoderTests.cs" />
100101
<Compile Include="Jira\CSharp728Tests.cs" />
101102
<Compile Include="Jira\CSharp708Tests.cs" />
102103
<Compile Include="Jira\CSharp476Tests.cs" />

src/MongoDB.Bson/IO/TrieNameDecoder.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public TValue Value
7777
public string Decode(BsonStream stream, UTF8Encoding encoding)
7878
{
7979
BsonTrieNode<TValue> node;
80+
var oldPosition = stream.Position;
8081
if (_trie.TryGetNode(stream, out node))
8182
{
8283
if (node.HasValue)
@@ -85,6 +86,8 @@ public string Decode(BsonStream stream, UTF8Encoding encoding)
8586
_value = node.Value;
8687
return node.ElementName;
8788
}
89+
90+
stream.Position = oldPosition;
8891
}
8992

9093
return stream.ReadCString(encoding);

0 commit comments

Comments
 (0)