diff --git a/Bson/DefaultSerializer/Conventions/DiscriminatorConventions.cs b/Bson/DefaultSerializer/Conventions/DiscriminatorConventions.cs
index 3b1e0f7117..b5344cd27f 100644
--- a/Bson/DefaultSerializer/Conventions/DiscriminatorConventions.cs
+++ b/Bson/DefaultSerializer/Conventions/DiscriminatorConventions.cs
@@ -68,34 +68,38 @@ namespace MongoDB.Bson.DefaultSerializer.Conventions {
BsonReader bsonReader,
Type nominalType
) {
- var bsonType = bsonReader.CurrentBsonType;
-
- Type primitiveType = null;
- switch (bsonType) {
- case BsonType.Boolean: primitiveType = typeof(bool); break;
- case BsonType.Binary:
- var bookmark = bsonReader.GetBookmark();
- byte[] bytes;
- BsonBinarySubType subType;
- bsonReader.ReadBinaryData(out bytes, out subType);
- if (subType == BsonBinarySubType.Uuid && bytes.Length == 16) {
- primitiveType = typeof(Guid);
- }
- bsonReader.ReturnToBookmark(bookmark);
- break;
- case BsonType.DateTime: primitiveType = typeof(DateTime); break;
- case BsonType.Double: primitiveType = typeof(double); break;
- case BsonType.Int32: primitiveType = typeof(int); break;
- case BsonType.Int64: primitiveType = typeof(long); break;
- case BsonType.ObjectId: primitiveType = typeof(ObjectId); break;
- case BsonType.String: primitiveType = typeof(string); break;
- }
+ if (bsonReader.ReadState == BsonReadState.Value) {
+ Type primitiveType = null;
+ switch (bsonReader.CurrentBsonType) {
+ case BsonType.Boolean: primitiveType = typeof(bool); break;
+ case BsonType.Binary:
+ var bookmark = bsonReader.GetBookmark();
+ byte[] bytes;
+ BsonBinarySubType subType;
+ bsonReader.ReadBinaryData(out bytes, out subType);
+ if (subType == BsonBinarySubType.Uuid && bytes.Length == 16) {
+ primitiveType = typeof(Guid);
+ }
+ bsonReader.ReturnToBookmark(bookmark);
+ break;
+ case BsonType.DateTime: primitiveType = typeof(DateTime); break;
+ case BsonType.Double: primitiveType = typeof(double); break;
+ case BsonType.Int32: primitiveType = typeof(int); break;
+ case BsonType.Int64: primitiveType = typeof(long); break;
+ case BsonType.ObjectId: primitiveType = typeof(ObjectId); break;
+ case BsonType.String: primitiveType = typeof(string); break;
+ }
- if (primitiveType != null && nominalType.IsAssignableFrom(primitiveType)) {
- return primitiveType;
+ if (primitiveType != null && nominalType.IsAssignableFrom(primitiveType)) {
+ return primitiveType;
+ }
}
- if (bsonType == BsonType.Document) {
+ if (
+ bsonReader.ReadState == BsonReadState.Initial ||
+ bsonReader.ReadState == BsonReadState.Done ||
+ (bsonReader.ReadState == BsonReadState.Value && bsonReader.CurrentBsonType == BsonType.Document)
+ ) {
var bookmark = bsonReader.GetBookmark();
bsonReader.ReadStartDocument();
var actualType = nominalType;
diff --git a/DriverOnlineTests/DriverOnlineTests.csproj b/DriverOnlineTests/DriverOnlineTests.csproj
index 39f86e5193..98e892c5c3 100644
--- a/DriverOnlineTests/DriverOnlineTests.csproj
+++ b/DriverOnlineTests/DriverOnlineTests.csproj
@@ -76,6 +76,7 @@
+
diff --git a/DriverOnlineTests/Jira/CSharp98Tests.cs b/DriverOnlineTests/Jira/CSharp98Tests.cs
new file mode 100644
index 0000000000..3c184de9a3
--- /dev/null
+++ b/DriverOnlineTests/Jira/CSharp98Tests.cs
@@ -0,0 +1,56 @@
+/* Copyright 2010 10gen 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 System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using NUnit.Framework;
+
+using MongoDB.Bson;
+using MongoDB.Bson.DefaultSerializer;
+using MongoDB.Driver;
+using MongoDB.Driver.Builders;
+
+namespace MongoDB.DriverOnlineTests.Jira.CSharp98 {
+ [TestFixture]
+ public class CSharp98Tests {
+ private class A {
+ [BsonId]
+ public ObjectId Id { get; set; }
+ public int PA { get; set; }
+ }
+
+ private class B : A {
+ public int PB { get; set; }
+ }
+
+ [Test]
+ public void TestDeserializationOfTwoBs() {
+ var server = MongoServer.Create("mongodb://localhost/?safe=true");
+ var database = server["onlinetests"];
+ var collection = database.GetCollection("csharp98");
+
+ collection.RemoveAll();
+ var b1 = new B { PA = 1, PB = 2 };
+ var b2 = new B { PA = 3, PB = 4 };
+ collection.Insert(b1);
+ collection.Insert(b2);
+
+ var docs = collection.FindAll().ToList();
+ }
+ }
+}